Blender V2.27

png.c

00001 
00031 #include "png.h"
00032 
00033 #include "BLI_blenlib.h"
00034 #include "MEM_guardedalloc.h"
00035 
00036 #include "imbuf.h"
00037 
00038 #include "IMB_imbuf_types.h"
00039 #include "IMB_imbuf.h"
00040 
00041 #include "IMB_allocimbuf.h"
00042 #include "IMB_metadata.h"
00043 #include "IMB_filetype.h"
00044 
00045 typedef struct PNGReadStruct {
00046         unsigned char *data;
00047         unsigned int size;
00048         unsigned int seek;
00049 }PNGReadStruct;
00050 
00051 static void ReadData( png_structp png_ptr, png_bytep data, png_size_t length);
00052 static void WriteData( png_structp png_ptr, png_bytep data, png_size_t length);
00053 static void Flush( png_structp png_ptr);
00054 
00055 int imb_is_a_png(unsigned char *mem)
00056 {
00057         int ret_val = 0;
00058 
00059         if (mem) ret_val = !png_sig_cmp(mem, 0, 8);
00060         return(ret_val);
00061 }
00062 
00063 static void Flush(png_structp png_ptr) 
00064 {
00065         (void)png_ptr;
00066 }
00067 
00068 static void WriteData( png_structp png_ptr, png_bytep data, png_size_t length)
00069 {
00070         ImBuf *ibuf = (ImBuf *) png_get_io_ptr(png_ptr);
00071 
00072         // if buffer is to small increase it.
00073         while (ibuf->encodedsize + length > ibuf->encodedbuffersize) {
00074                 imb_enlargeencodedbufferImBuf(ibuf);
00075         }
00076 
00077         memcpy(ibuf->encodedbuffer + ibuf->encodedsize, data, length);
00078         ibuf->encodedsize += length;
00079 }
00080 
00081 static void ReadData( png_structp png_ptr, png_bytep data, png_size_t length)
00082 {
00083         PNGReadStruct *rs= (PNGReadStruct *) png_get_io_ptr(png_ptr);
00084 
00085         if (rs) {
00086                 if (length <= rs->size - rs->seek) {
00087                         memcpy(data, rs->data + rs->seek, length);
00088                         rs->seek += length;
00089                         return;
00090                 }
00091         }
00092 
00093         printf("Reached EOF while decoding PNG\n");
00094         longjmp(png_jmpbuf(png_ptr), 1);
00095 }
00096 
00097 int imb_savepng(struct ImBuf *ibuf, const char *name, int flags)
00098 {
00099         png_structp png_ptr;
00100         png_infop info_ptr;
00101 
00102         unsigned char *pixels = 0;
00103         unsigned char *from, *to;
00104         png_bytepp row_pointers = 0;
00105         int i, bytesperpixel, color_type = PNG_COLOR_TYPE_GRAY;
00106         FILE *fp = 0;
00107 
00108         /* use the jpeg quality setting for compression */
00109         int compression;
00110         compression= (int)(((float)(ibuf->ftype & 0xff) / 11.1111f));
00111         compression= compression < 0 ? 0 : (compression > 9 ? 9 : compression);
00112 
00113         /* for prints */
00114         if(flags & IB_mem)
00115                 name= "<memory>";
00116 
00117         bytesperpixel = (ibuf->depth + 7) >> 3;
00118         if ((bytesperpixel > 4) || (bytesperpixel == 2)) {
00119                 printf("imb_savepng: Cunsupported bytes per pixel: %d for file: '%s'\n", bytesperpixel, name);
00120                 return (0);
00121         }
00122 
00123         png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
00124                 NULL, NULL, NULL);
00125         if (png_ptr == NULL) {
00126                 printf("imb_savepng: Cannot png_create_write_struct for file: '%s'\n", name);
00127                 return 0;
00128         }
00129 
00130         info_ptr = png_create_info_struct(png_ptr);
00131         if (info_ptr == NULL) {
00132                 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
00133                 printf("imb_savepng: Cannot png_create_info_struct for file: '%s'\n", name);
00134                 return 0;
00135         }
00136 
00137         if (setjmp(png_jmpbuf(png_ptr))) {
00138                 png_destroy_write_struct(&png_ptr, &info_ptr);
00139                 printf("imb_savepng: Cannot setjmp for file: '%s'\n", name);
00140                 return 0;
00141         }
00142 
00143         // copy image data
00144 
00145         pixels = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(unsigned char), "pixels");
00146         if (pixels == NULL) {
00147                 png_destroy_write_struct(&png_ptr, &info_ptr);
00148                 printf("imb_savepng: Cannot allocate pixels array of %dx%d, %d bytes per pixel for file: '%s'\n", ibuf->x, ibuf->y, bytesperpixel, name);
00149                 return 0;
00150         }
00151 
00152         from = (unsigned char *) ibuf->rect;
00153         to = pixels;
00154 
00155         switch (bytesperpixel) {
00156         case 4:
00157                 color_type = PNG_COLOR_TYPE_RGBA;
00158                 for (i = ibuf->x * ibuf->y; i > 0; i--) {
00159                         to[0] = from[0];
00160                         to[1] = from[1];
00161                         to[2] = from[2];
00162                         to[3] = from[3];
00163                         to += 4; from += 4;
00164                 }
00165                 break;
00166         case 3:
00167                 color_type = PNG_COLOR_TYPE_RGB;
00168                 for (i = ibuf->x * ibuf->y; i > 0; i--) {
00169                         to[0] = from[0];
00170                         to[1] = from[1];
00171                         to[2] = from[2];
00172                         to += 3; from += 4;
00173                 }
00174                 break;
00175         case 1:
00176                 color_type = PNG_COLOR_TYPE_GRAY;
00177                 for (i = ibuf->x * ibuf->y; i > 0; i--) {
00178                         to[0] = from[0];
00179                         to++; from += 4;
00180                 }
00181                 break;
00182         }
00183 
00184         if (flags & IB_mem) {
00185                 // create image in memory
00186                 imb_addencodedbufferImBuf(ibuf);
00187                 ibuf->encodedsize = 0;
00188 
00189                 png_set_write_fn(png_ptr,
00190                          (png_voidp) ibuf,
00191                          WriteData,
00192                          Flush);
00193         } else {
00194                 fp = fopen(name, "wb");
00195                 if (!fp) {
00196                         png_destroy_write_struct(&png_ptr, &info_ptr);
00197                         MEM_freeN(pixels);
00198                         printf("imb_savepng: Cannot open file for writing: '%s'\n", name);
00199                         return 0;
00200                 }
00201                 png_init_io(png_ptr, fp);
00202         }
00203 
00204         /*
00205         png_set_filter(png_ptr, 0,
00206                 PNG_FILTER_NONE  | PNG_FILTER_VALUE_NONE |
00207                 PNG_FILTER_SUB   | PNG_FILTER_VALUE_SUB  |
00208                 PNG_FILTER_UP    | PNG_FILTER_VALUE_UP   |
00209                 PNG_FILTER_AVG   | PNG_FILTER_VALUE_AVG  |
00210                 PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH|
00211                 PNG_ALL_FILTERS);
00212         */
00213 
00214         png_set_compression_level(png_ptr, compression);
00215 
00216         // png image settings
00217         png_set_IHDR(png_ptr,
00218                  info_ptr,
00219                  ibuf->x,
00220                  ibuf->y,
00221                  8,
00222                  color_type,
00223                  PNG_INTERLACE_NONE,
00224                  PNG_COMPRESSION_TYPE_DEFAULT,
00225                  PNG_FILTER_TYPE_DEFAULT);
00226 
00227         /* image text info */
00228         if (ibuf->metadata) {
00229                 png_text*  metadata;
00230                 ImMetaData* iptr;
00231                 int  num_text = 0;
00232                 iptr = ibuf->metadata;
00233                 while (iptr) {
00234                         num_text++;
00235                         iptr = iptr->next;
00236                 }
00237                 
00238                 metadata = MEM_callocN(num_text*sizeof(png_text), "png_metadata");
00239                 iptr = ibuf->metadata;
00240                 num_text = 0;
00241                 while (iptr) {
00242                         
00243                         metadata[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
00244                         metadata[num_text].key = iptr->key;
00245                         metadata[num_text].text = iptr->value;
00246                         num_text++;
00247                         iptr = iptr->next;
00248                 }
00249                 
00250                 png_set_text(png_ptr, info_ptr, metadata, num_text);
00251                 MEM_freeN(metadata);
00252 
00253         }
00254 
00255         // write the file header information
00256         png_write_info(png_ptr, info_ptr);
00257 
00258         // allocate memory for an array of row-pointers
00259         row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_bytep), "row_pointers");
00260         if (row_pointers == NULL) {
00261                 printf("imb_savepng: Cannot allocate row-pointers array for file '%s'\n", name);
00262                 png_destroy_write_struct(&png_ptr, &info_ptr);
00263                 MEM_freeN(pixels);
00264                 if (fp) {
00265                         fclose(fp);
00266                 }
00267                 return 0;
00268         }
00269 
00270         // set the individual row-pointers to point at the correct offsets
00271         for (i = 0; i < ibuf->y; i++) {
00272                 row_pointers[ibuf->y-1-i] = (png_bytep)
00273                         ((unsigned char *)pixels + (i * ibuf->x) * bytesperpixel * sizeof(unsigned char));
00274         }
00275 
00276         // write out the entire image data in one call
00277         png_write_image(png_ptr, row_pointers);
00278 
00279         // write the additional chunks to the PNG file (not really needed)
00280         png_write_end(png_ptr, info_ptr);
00281 
00282         // clean up
00283         MEM_freeN(pixels);
00284         MEM_freeN(row_pointers);
00285         png_destroy_write_struct(&png_ptr, &info_ptr);
00286 
00287         if (fp) {
00288                 fflush(fp);
00289                 fclose(fp);
00290         }
00291 
00292         return(1);
00293 }
00294 
00295 struct ImBuf *imb_loadpng(unsigned char *mem, size_t size, int flags)
00296 {
00297         struct ImBuf *ibuf = 0;
00298         png_structp png_ptr;
00299         png_infop info_ptr;
00300         unsigned char *pixels = 0;
00301         png_bytepp row_pointers = 0;
00302         png_uint_32 width, height;
00303         int bit_depth, color_type;
00304         PNGReadStruct ps;
00305 
00306         unsigned char *from, *to;
00307         int i, bytesperpixel;
00308 
00309         if (imb_is_a_png(mem) == 0) return(0);
00310 
00311         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
00312                 NULL, NULL, NULL);
00313         if (png_ptr == NULL) {
00314                 printf("Cannot png_create_read_struct\n");
00315                 return 0;
00316         }
00317 
00318         info_ptr = png_create_info_struct(png_ptr);
00319         if (info_ptr == NULL) {
00320                 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, 
00321                         (png_infopp)NULL);
00322                 printf("Cannot png_create_info_struct\n");
00323                 return 0;
00324         }
00325 
00326         ps.size = size; /* XXX, 4gig limit! */
00327         ps.data = mem;
00328         ps.seek = 0;
00329 
00330         png_set_read_fn(png_ptr, (void *) &ps, ReadData);
00331 
00332         if (setjmp(png_jmpbuf(png_ptr))) {
00333                 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
00334                 if (pixels) MEM_freeN(pixels);
00335                 if (row_pointers) MEM_freeN(row_pointers);
00336                 if (ibuf) IMB_freeImBuf(ibuf);
00337                 return 0;
00338         }
00339 
00340         // png_set_sig_bytes(png_ptr, 8);
00341 
00342         png_read_info(png_ptr, info_ptr);
00343         png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, 
00344                 &color_type, NULL, NULL, NULL);
00345 
00346         if (bit_depth == 16) {
00347                 png_set_strip_16(png_ptr);
00348                 bit_depth = 8;
00349         }
00350 
00351         bytesperpixel = png_get_channels(png_ptr, info_ptr);
00352 
00353         switch(color_type) {
00354         case PNG_COLOR_TYPE_RGB:
00355         case PNG_COLOR_TYPE_RGB_ALPHA:
00356                 break;
00357         case PNG_COLOR_TYPE_PALETTE:
00358                 png_set_palette_to_rgb(png_ptr);
00359                 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
00360                         bytesperpixel = 4;
00361                 } else {
00362                         bytesperpixel = 3;
00363                 }
00364                 break;
00365         case PNG_COLOR_TYPE_GRAY:
00366         case PNG_COLOR_TYPE_GRAY_ALPHA:
00367                 if (bit_depth < 8) {
00368                         png_set_expand(png_ptr);
00369                         bit_depth = 8;
00370                 }
00371                 break;
00372         default:
00373                 printf("PNG format not supported\n");
00374                 longjmp(png_jmpbuf(png_ptr), 1);
00375         }
00376         
00377         ibuf = IMB_allocImBuf(width, height, 8 * bytesperpixel, 0);
00378 
00379         if (ibuf) {
00380                 ibuf->ftype = PNG;
00381                 ibuf->profile = IB_PROFILE_SRGB;
00382         } else {
00383                 printf("Couldn't allocate memory for PNG image\n");
00384         }
00385 
00386         if (ibuf && ((flags & IB_test) == 0)) {
00387                 imb_addrectImBuf(ibuf);
00388 
00389                 pixels = MEM_mallocN(ibuf->x * ibuf->y * bytesperpixel * sizeof(unsigned char), "pixels");
00390                 if (pixels == NULL) {
00391                         printf("Cannot allocate pixels array\n");
00392                         longjmp(png_jmpbuf(png_ptr), 1);
00393                 }
00394 
00395                 // allocate memory for an array of row-pointers
00396                 row_pointers = (png_bytepp) MEM_mallocN(ibuf->y * sizeof(png_bytep), "row_pointers");
00397                 if (row_pointers == NULL) {
00398                         printf("Cannot allocate row-pointers array\n");
00399                         longjmp(png_jmpbuf(png_ptr), 1);
00400                 }
00401 
00402                 // set the individual row-pointers to point at the correct offsets
00403                 for (i = 0; i < ibuf->y; i++) {
00404                         row_pointers[ibuf->y-1-i] = (png_bytep)
00405                         ((unsigned char *)pixels + (i * ibuf->x) * bytesperpixel * sizeof(unsigned char));
00406                 }
00407 
00408                 png_read_image(png_ptr, row_pointers);
00409 
00410                 // copy image data
00411 
00412                 to = (unsigned char *) ibuf->rect;
00413                 from = pixels;
00414 
00415                 switch (bytesperpixel) {
00416                 case 4:
00417                         for (i = ibuf->x * ibuf->y; i > 0; i--) {
00418                                 to[0] = from[0];
00419                                 to[1] = from[1];
00420                                 to[2] = from[2];
00421                                 to[3] = from[3];
00422                                 to += 4; from += 4;
00423                         }
00424                         break;
00425                 case 3:
00426                         for (i = ibuf->x * ibuf->y; i > 0; i--) {
00427                                 to[0] = from[0];
00428                                 to[1] = from[1];
00429                                 to[2] = from[2];
00430                                 to[3] = 0xff;
00431                                 to += 4; from += 3;
00432                         }
00433                         break;
00434                 case 2:
00435                         for (i = ibuf->x * ibuf->y; i > 0; i--) {
00436                                 to[0] = to[1] = to[2] = from[0];
00437                                 to[3] = from[1];
00438                                 to += 4; from += 2;
00439                         }
00440                         break;
00441                 case 1:
00442                         for (i = ibuf->x * ibuf->y; i > 0; i--) {
00443                                 to[0] = to[1] = to[2] = from[0];
00444                                 to[3] = 0xff;
00445                                 to += 4; from++;
00446                         }
00447                         break;
00448                 }
00449 
00450                 if (flags & IB_metadata) {
00451                         png_text* text_chunks;
00452                         int count = png_get_text(png_ptr, info_ptr, &text_chunks, NULL);
00453                         for(i = 0; i < count; i++) {
00454                                 IMB_metadata_add_field(ibuf, text_chunks[i].key, text_chunks[i].text);
00455                                 ibuf->flags |= IB_metadata;                             
00456                          }
00457                 }
00458 
00459                 png_read_end(png_ptr, info_ptr);
00460         }
00461 
00462         // clean up
00463         MEM_freeN(pixels);
00464         MEM_freeN(row_pointers);
00465         png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
00466 
00467         return(ibuf);
00468 }
00469