예제 #1
0
파일: fileformat.c 프로젝트: kanzure/brlcad
HIDDEN int
png_write(icv_image_t *bif, const char *filename)
{
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    int i = 0;
    int png_color_type = PNG_COLOR_TYPE_RGB;
    unsigned char *data;
    FILE *fh;

    fh = fopen(filename, "wb");
    if (UNLIKELY(fh==NULL)) {
	perror("fdopen");
	bu_log("ERROR: png_write failed to get a FILE pointer\n");
	return 0;
    }

    data = data2uchar(bif);

    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (UNLIKELY(png_ptr == NULL)) {
	fclose(fh);
	return 0;
    }

    info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL || setjmp(png_jmpbuf(png_ptr))) {
	png_destroy_read_struct(&png_ptr, info_ptr ? &info_ptr : NULL, NULL);
	bu_log("ERROR: Unable to create png header\n");
	fclose(fh);
	return 0;
    }

    png_init_io(png_ptr, fh);
    png_set_IHDR(png_ptr, info_ptr, (unsigned)bif->width, (unsigned)bif->height, 8, png_color_type,
		 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
		 PNG_FILTER_TYPE_DEFAULT);
    png_write_info(png_ptr, info_ptr);
    for (i = bif->height-1; i >= 0; --i)
	png_write_row(png_ptr, (png_bytep) (data + bif->width*bif->channels*i));
    png_write_end(png_ptr, info_ptr);

    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(fh);
    return 1;
}
예제 #2
0
int
bw_write(icv_image_t *bif, const char *filename)
{

    unsigned char *data;
    FILE *fp;
    size_t ret, size;

    if (bif->color_space == ICV_COLOR_SPACE_RGB) {
	icv_rgb2gray_ntsc(bif);
    } else if (bif->color_space != ICV_COLOR_SPACE_GRAY) {
	bu_log("bw_write : Color Space conflict");
	return -1;
    }
    data = data2uchar(bif);
    size = bif->height*bif->width;

    if (filename == NULL) {
	fp = stdout;
    } else {
	fp = fopen(filename, "wb");
	if (fp == NULL) {
	    bu_log("bw_write: Cannot open file for saving\n");
	    return -1;
	}
    }

    ret = fwrite(data, 1, size, fp);
    fclose(fp);
    bu_free(data, "bw_write : Unsigned Char data");

    if (ret != size) {
	bu_log("bw_write : Short Write\n");
	return -1;
    }

    return 0;
}