71 lines
1.9 KiB
C
71 lines
1.9 KiB
C
#include "Image_Exporter.h"
|
|
|
|
|
|
//extern int _options; // Global options settings variable
|
|
|
|
int saveToPNG(RS_IMAGE *img, char *tex_path, char *hmt_fileName) {
|
|
if (tex_path == NULL || img == NULL) return EXIT_FAILURE;
|
|
char export_path[128];
|
|
FILE *_png_f = NULL;
|
|
png_structp png_ptr = NULL;
|
|
png_infop info_ptr = NULL;
|
|
size_t x,y;
|
|
png_byte **row_ptrs = NULL;
|
|
PIXEL_A *pixel = NULL;
|
|
//int pixel_size = 3;
|
|
//int depth = 8; //bit par color channel (RGB)
|
|
|
|
strcpy(export_path, hmt_fileName);
|
|
#ifdef _WIN32
|
|
strcat(export_path, "-out\\");
|
|
#else
|
|
strcat(export_path, "-out/");
|
|
#endif
|
|
strcat(export_path, tex_path);
|
|
strcat(export_path, ".png");
|
|
_png_f = fopen(export_path, "wb");
|
|
if (_png_f == NULL) return EXIT_FAILURE;
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
if (png_ptr == NULL) {
|
|
fclose(_png_f);
|
|
return EXIT_FAILURE;
|
|
}
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
if (info_ptr == NULL) {
|
|
fclose(_png_f);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
// Set image attributes
|
|
png_set_IHDR(png_ptr, info_ptr, img->width, img->height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
// Init PNG datas
|
|
row_ptrs = png_malloc(png_ptr, img->height * sizeof(png_byte *));
|
|
for (y=0; y<img->height; y++) {
|
|
png_byte *row = png_malloc(png_ptr, img->width*sizeof(PIXEL_A));
|
|
row_ptrs[y] = row;
|
|
for (x=0; x<img->width; x++) {
|
|
pixel = pixelAt(img, x , y);
|
|
if(pixel == NULL) return EXIT_FAILURE;
|
|
|
|
*row++ = pixel->_red;
|
|
*row++ = pixel->_green;
|
|
*row++ = pixel->_blue;
|
|
*row++ = pixel->_alpha;
|
|
}
|
|
}
|
|
|
|
png_init_io(png_ptr, _png_f);
|
|
png_set_rows(png_ptr, info_ptr, row_ptrs);
|
|
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
|
|
|
|
for (y=0; y<img->height; y++) {
|
|
png_free(png_ptr, row_ptrs[y]);
|
|
}
|
|
png_free(png_ptr, row_ptrs);
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
fclose(_png_f);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|