Finish grey picture re-encoding for PNG output

This commit is contained in:
JackCarterSmith 2019-07-20 18:39:44 +02:00
parent a34a5d5337
commit 6df772741b
4 changed files with 37 additions and 20 deletions

View File

@ -109,9 +109,6 @@ void purgeHMTFromMemory(HMT_FILE *_f) {
if (_f->textures_list->image.pixels != NULL) free(_f->textures_list->image.pixels);
if (_f->textures_list->image.samples != NULL) free(_f->textures_list->image.samples);
}
if (_f->textures_list != NULL) {
if (_f->textures_list->image.pixels != NULL) free(_f->textures_list->image.pixels);
}
free(_f->textures_list);
if (_f->materials_list != NULL) free(_f->materials_list);

View File

@ -6,10 +6,11 @@ int saveToPNG(RS_IMAGE *img, char *tex_name) {
FILE *_png_f = NULL;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
//size_t x,y;
size_t x,y;
png_byte **row_ptrs = NULL;
PIXEL *pixel = NULL;
//int pixel_size = 3;
//int depth = 8;
//int depth = 8; //bit par color channel (RGB)
strcpy(tex_path,tex_name);
strcat(tex_path, ".png");
@ -31,23 +32,33 @@ int saveToPNG(RS_IMAGE *img, char *tex_name) {
// 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(unsigned char)*3);
for (y=0; y<img->height; y++) {
png_byte *row = png_malloc(png_ptr, img->width*sizeof(PIXEL));
row_ptrs[y] = row;
for (x=0; x<img->width; x++) {
unsigned char pixel[3] = pixelAt(img->pixels);
pixel = pixelAt(img, x , y);
if(pixel == NULL) return EXIT_FAILURE;
*row++ = pixel->_red;
*row++ = pixel->_green;
*row++ = pixel->_blue;
}
}*/
}
/*
if (img->paletteEntries == 0 || img->sampleBits == 32 || img->sampleBits == 16) {
memcpy(row_ptrs, img->pixels, sizeof(*img->pixels)*img->height*img->width);
} else {
memcpy(row_ptrs, img->pixels, sizeof(*img->pixels)*3*img->height*img->width);
}
*/
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);

View File

@ -35,7 +35,7 @@ int getSamplesFromFile(RS_IMAGE *img, FILE *f) {
void decodePixels(RS_IMAGE *img) {
int size;
img->pixels = NULL;
//img->pixels = NULL;
if (!(img->type_ == 0 ||
img->type_ == 1 ||
img->type_ == 2 ||
@ -51,9 +51,9 @@ void decodePixels(RS_IMAGE *img) {
break;
case 4:
if (img->paletteEntries == 0) {
size = img->width * img->height;
size = sizeof(PIXEL) * img->height * img->width;
img->pixels = calloc(1, size);
unpack4To8bits(img->samples, img->pixels, size);
convert4bitsGreyto8bitsRGB(img->samples, img->pixels, div(size,3).quot);
} else if (img->paletteEntries == 16) {
size = img->width * img->height;
img->pixels = calloc(1, size*3);
@ -81,14 +81,18 @@ void decodePixels(RS_IMAGE *img) {
}
}
void unpack4To8bits(unsigned char *samples_tab, unsigned char *pixels_tab, int sampling) {
void convert4bitsGreyto8bitsRGB(unsigned char *samples_tab, PIXEL *pixels_tab, int sampling) {
int i;
char v;
unsigned char v;
for(i=0; i<div(sampling,2).quot; i++) {
v = samples_tab[i];
pixels_tab[i*2] = ((v >> 4) & 0xF) << 4;
pixels_tab[i*2+1] = (v & 0xF) << 4;
pixels_tab[i*2]._red = div((v >> 4 & 0xF) * 256, 16).quot;
pixels_tab[i*2]._green = div((v >> 4 & 0xF) * 256, 16).quot;
pixels_tab[i*2]._blue = div((v >> 4 & 0xF) * 256, 16).quot;
pixels_tab[i*2+1]._red = div((v & 0xF) * 256, 16).quot;
pixels_tab[i*2+1]._green = div((v & 0xF) * 256, 16).quot;
pixels_tab[i*2+1]._blue = div((v & 0xF) * 256, 16).quot;
}
}
@ -119,6 +123,10 @@ void useOddBytes(unsigned char *src, unsigned char *dst, int size) {
}
}
PIXEL *pixelAt(RS_IMAGE *img, int posX , int posY) {
return img->pixels + img->width * posY + posX;
}
RS_IMAGE_DESC getImageDescFromType(unsigned char type) {
RS_IMAGE_DESC desc;

View File

@ -21,9 +21,9 @@
* @brief RGB pixel structure, used to store datas for implementation inside PNG files
*/
typedef struct PixelRGB {
char _red;
char _green;
char _blue;
unsigned char _red;
unsigned char _green;
unsigned char _blue;
}PIXEL;
/**
@ -51,11 +51,12 @@ typedef struct RSImage_desc {
///// Declare functions /////
/////////////////////////////
RS_IMAGE_DESC getImageDescFromType(unsigned char type); //TODO: Optimise function
void unpack4To8bits(unsigned char *samples_tab, unsigned char *pixels_tab, int sampling);
void convert4bitsGreyto8bitsRGB(unsigned char *samples_tab, PIXEL *pixels_tab, int sampling);
void unpack4To24bitsRGB(unsigned char *samples_tab, unsigned char *pixels_tab, int size, unsigned char pal[256][3]);
void unpack8To24bitsRGB(unsigned char *samples_tab, unsigned char *pixels_tab, int size, unsigned char pal[256][3]);
void useOddBytes(unsigned char *src, unsigned char *dst, int size);
void decodePixels(RS_IMAGE *img);
PIXEL *pixelAt(RS_IMAGE *img, int posX , int posY);
int getPaletteFromFile(RS_IMAGE *img, FILE *f);
int getSamplesFromFile(RS_IMAGE *img, FILE *f);