Home | History | Annotate | Download | only in gregbook
      1 /*---------------------------------------------------------------------------
      2 
      3    wpng - simple PNG-writing program                             writepng.c
      4 
      5   ---------------------------------------------------------------------------
      6 
      7       Copyright (c) 1998-2007, 2017 Greg Roelofs.  All rights reserved.
      8 
      9       This software is provided "as is," without warranty of any kind,
     10       express or implied.  In no event shall the author or contributors
     11       be held liable for any damages arising in any way from the use of
     12       this software.
     13 
     14       The contents of this file are DUAL-LICENSED.  You may modify and/or
     15       redistribute this software according to the terms of one of the
     16       following two licenses (at your option):
     17 
     18 
     19       LICENSE 1 ("BSD-like with advertising clause"):
     20 
     21       Permission is granted to anyone to use this software for any purpose,
     22       including commercial applications, and to alter it and redistribute
     23       it freely, subject to the following restrictions:
     24 
     25       1. Redistributions of source code must retain the above copyright
     26          notice, disclaimer, and this list of conditions.
     27       2. Redistributions in binary form must reproduce the above copyright
     28          notice, disclaimer, and this list of conditions in the documenta-
     29          tion and/or other materials provided with the distribution.
     30       3. All advertising materials mentioning features or use of this
     31          software must display the following acknowledgment:
     32 
     33             This product includes software developed by Greg Roelofs
     34             and contributors for the book, "PNG: The Definitive Guide,"
     35             published by O'Reilly and Associates.
     36 
     37 
     38       LICENSE 2 (GNU GPL v2 or later):
     39 
     40       This program is free software; you can redistribute it and/or modify
     41       it under the terms of the GNU General Public License as published by
     42       the Free Software Foundation; either version 2 of the License, or
     43       (at your option) any later version.
     44 
     45       This program is distributed in the hope that it will be useful,
     46       but WITHOUT ANY WARRANTY; without even the implied warranty of
     47       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     48       GNU General Public License for more details.
     49 
     50       You should have received a copy of the GNU General Public License
     51       along with this program; if not, write to the Free Software Foundation,
     52       Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     53 
     54   ---------------------------------------------------------------------------*/
     55 
     56 
     57 #include <stdlib.h>     /* for exit() prototype */
     58 #include <zlib.h>
     59 
     60 #include "png.h"        /* libpng header, includes setjmp.h */
     61 #include "writepng.h"   /* typedefs, common macros, public prototypes */
     62 
     63 
     64 /* local prototype */
     65 
     66 static void writepng_error_handler(png_structp png_ptr, png_const_charp msg);
     67 
     68 
     69 
     70 void writepng_version_info(void)
     71 {
     72   fprintf(stderr, "   Compiled with libpng %s; using libpng %s.\n",
     73     PNG_LIBPNG_VER_STRING, png_libpng_ver);
     74   fprintf(stderr, "   Compiled with zlib %s; using zlib %s.\n",
     75     ZLIB_VERSION, zlib_version);
     76 }
     77 
     78 
     79 
     80 
     81 /* returns 0 for success, 2 for libpng problem, 4 for out of memory, 11 for
     82  *  unexpected pnmtype; note that outfile might be stdout */
     83 
     84 int writepng_init(mainprog_info *mainprog_ptr)
     85 {
     86     png_structp  png_ptr;       /* note:  temporary variables! */
     87     png_infop  info_ptr;
     88     int color_type, interlace_type;
     89 
     90 
     91     /* could also replace libpng warning-handler (final NULL), but no need: */
     92 
     93     png_ptr = png_create_write_struct(png_get_libpng_ver(NULL), mainprog_ptr,
     94       writepng_error_handler, NULL);
     95     if (!png_ptr)
     96         return 4;   /* out of memory */
     97 
     98     info_ptr = png_create_info_struct(png_ptr);
     99     if (!info_ptr) {
    100         png_destroy_write_struct(&png_ptr, NULL);
    101         return 4;   /* out of memory */
    102     }
    103 
    104 
    105     /* setjmp() must be called in every function that calls a PNG-writing
    106      * libpng function, unless an alternate error handler was installed--
    107      * but compatible error handlers must either use longjmp() themselves
    108      * (as in this program) or some other method to return control to
    109      * application code, so here we go: */
    110 
    111     if (setjmp(mainprog_ptr->jmpbuf)) {
    112         png_destroy_write_struct(&png_ptr, &info_ptr);
    113         return 2;
    114     }
    115 
    116 
    117     /* make sure outfile is (re)opened in BINARY mode */
    118 
    119     png_init_io(png_ptr, mainprog_ptr->outfile);
    120 
    121 
    122     /* set the compression levels--in general, always want to leave filtering
    123      * turned on (except for palette images) and allow all of the filters,
    124      * which is the default; want 32K zlib window, unless entire image buffer
    125      * is 16K or smaller (unknown here)--also the default; usually want max
    126      * compression (NOT the default); and remaining compression flags should
    127      * be left alone */
    128 
    129     png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
    130 /*
    131     >> this is default for no filtering; Z_FILTERED is default otherwise:
    132     png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
    133     >> these are all defaults:
    134     png_set_compression_mem_level(png_ptr, 8);
    135     png_set_compression_window_bits(png_ptr, 15);
    136     png_set_compression_method(png_ptr, 8);
    137  */
    138 
    139 
    140     /* set the image parameters appropriately */
    141 
    142     if (mainprog_ptr->pnmtype == 5)
    143         color_type = PNG_COLOR_TYPE_GRAY;
    144     else if (mainprog_ptr->pnmtype == 6)
    145         color_type = PNG_COLOR_TYPE_RGB;
    146     else if (mainprog_ptr->pnmtype == 8)
    147         color_type = PNG_COLOR_TYPE_RGB_ALPHA;
    148     else {
    149         png_destroy_write_struct(&png_ptr, &info_ptr);
    150         return 11;
    151     }
    152 
    153     interlace_type = mainprog_ptr->interlaced? PNG_INTERLACE_ADAM7 :
    154                                                PNG_INTERLACE_NONE;
    155 
    156     png_set_IHDR(png_ptr, info_ptr, mainprog_ptr->width, mainprog_ptr->height,
    157       mainprog_ptr->sample_depth, color_type, interlace_type,
    158       PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
    159 
    160     if (mainprog_ptr->gamma > 0.0)
    161         png_set_gAMA(png_ptr, info_ptr, mainprog_ptr->gamma);
    162 
    163     if (mainprog_ptr->have_bg) {   /* we know it's RGBA, not gray+alpha */
    164         png_color_16  background;
    165 
    166         background.red = mainprog_ptr->bg_red;
    167         background.green = mainprog_ptr->bg_green;
    168         background.blue = mainprog_ptr->bg_blue;
    169         png_set_bKGD(png_ptr, info_ptr, &background);
    170     }
    171 
    172     if (mainprog_ptr->have_time) {
    173         png_time  modtime;
    174 
    175         png_convert_from_time_t(&modtime, mainprog_ptr->modtime);
    176         png_set_tIME(png_ptr, info_ptr, &modtime);
    177     }
    178 
    179     if (mainprog_ptr->have_text) {
    180         png_text  text[6];
    181         int  num_text = 0;
    182 
    183         if (mainprog_ptr->have_text & TEXT_TITLE) {
    184             text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
    185             text[num_text].key = "Title";
    186             text[num_text].text = mainprog_ptr->title;
    187             ++num_text;
    188         }
    189         if (mainprog_ptr->have_text & TEXT_AUTHOR) {
    190             text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
    191             text[num_text].key = "Author";
    192             text[num_text].text = mainprog_ptr->author;
    193             ++num_text;
    194         }
    195         if (mainprog_ptr->have_text & TEXT_DESC) {
    196             text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
    197             text[num_text].key = "Description";
    198             text[num_text].text = mainprog_ptr->desc;
    199             ++num_text;
    200         }
    201         if (mainprog_ptr->have_text & TEXT_COPY) {
    202             text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
    203             text[num_text].key = "Copyright";
    204             text[num_text].text = mainprog_ptr->copyright;
    205             ++num_text;
    206         }
    207         if (mainprog_ptr->have_text & TEXT_EMAIL) {
    208             text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
    209             text[num_text].key = "E-mail";
    210             text[num_text].text = mainprog_ptr->email;
    211             ++num_text;
    212         }
    213         if (mainprog_ptr->have_text & TEXT_URL) {
    214             text[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
    215             text[num_text].key = "URL";
    216             text[num_text].text = mainprog_ptr->url;
    217             ++num_text;
    218         }
    219         png_set_text(png_ptr, info_ptr, text, num_text);
    220     }
    221 
    222 
    223     /* write all chunks up to (but not including) first IDAT */
    224 
    225     png_write_info(png_ptr, info_ptr);
    226 
    227 
    228     /* if we wanted to write any more text info *after* the image data, we
    229      * would set up text struct(s) here and call png_set_text() again, with
    230      * just the new data; png_set_tIME() could also go here, but it would
    231      * have no effect since we already called it above (only one tIME chunk
    232      * allowed) */
    233 
    234 
    235     /* set up the transformations:  for now, just pack low-bit-depth pixels
    236      * into bytes (one, two or four pixels per byte) */
    237 
    238     png_set_packing(png_ptr);
    239 /*  png_set_shift(png_ptr, &sig_bit);  to scale low-bit-depth values */
    240 
    241 
    242     /* make sure we save our pointers for use in writepng_encode_image() */
    243 
    244     mainprog_ptr->png_ptr = png_ptr;
    245     mainprog_ptr->info_ptr = info_ptr;
    246 
    247 
    248     /* OK, that's all we need to do for now; return happy */
    249 
    250     return 0;
    251 }
    252 
    253 
    254 
    255 
    256 
    257 /* returns 0 for success, 2 for libpng (longjmp) problem */
    258 
    259 int writepng_encode_image(mainprog_info *mainprog_ptr)
    260 {
    261     png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
    262     png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
    263 
    264 
    265     /* as always, setjmp() must be called in every function that calls a
    266      * PNG-writing libpng function */
    267 
    268     if (setjmp(mainprog_ptr->jmpbuf)) {
    269         png_destroy_write_struct(&png_ptr, &info_ptr);
    270         mainprog_ptr->png_ptr = NULL;
    271         mainprog_ptr->info_ptr = NULL;
    272         return 2;
    273     }
    274 
    275 
    276     /* and now we just write the whole image; libpng takes care of interlacing
    277      * for us */
    278 
    279     png_write_image(png_ptr, mainprog_ptr->row_pointers);
    280 
    281 
    282     /* since that's it, we also close out the end of the PNG file now--if we
    283      * had any text or time info to write after the IDATs, second argument
    284      * would be info_ptr, but we optimize slightly by sending NULL pointer: */
    285 
    286     png_write_end(png_ptr, NULL);
    287 
    288     return 0;
    289 }
    290 
    291 
    292 
    293 
    294 
    295 /* returns 0 if succeeds, 2 if libpng problem */
    296 
    297 int writepng_encode_row(mainprog_info *mainprog_ptr)  /* NON-interlaced only! */
    298 {
    299     png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
    300     png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
    301 
    302 
    303     /* as always, setjmp() must be called in every function that calls a
    304      * PNG-writing libpng function */
    305 
    306     if (setjmp(mainprog_ptr->jmpbuf)) {
    307         png_destroy_write_struct(&png_ptr, &info_ptr);
    308         mainprog_ptr->png_ptr = NULL;
    309         mainprog_ptr->info_ptr = NULL;
    310         return 2;
    311     }
    312 
    313 
    314     /* image_data points at our one row of image data */
    315 
    316     png_write_row(png_ptr, mainprog_ptr->image_data);
    317 
    318     return 0;
    319 }
    320 
    321 
    322 
    323 
    324 
    325 /* returns 0 if succeeds, 2 if libpng problem */
    326 
    327 int writepng_encode_finish(mainprog_info *mainprog_ptr)   /* NON-interlaced! */
    328 {
    329     png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
    330     png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
    331 
    332 
    333     /* as always, setjmp() must be called in every function that calls a
    334      * PNG-writing libpng function */
    335 
    336     if (setjmp(mainprog_ptr->jmpbuf)) {
    337         png_destroy_write_struct(&png_ptr, &info_ptr);
    338         mainprog_ptr->png_ptr = NULL;
    339         mainprog_ptr->info_ptr = NULL;
    340         return 2;
    341     }
    342 
    343 
    344     /* close out PNG file; if we had any text or time info to write after
    345      * the IDATs, second argument would be info_ptr: */
    346 
    347     png_write_end(png_ptr, NULL);
    348 
    349     return 0;
    350 }
    351 
    352 
    353 
    354 
    355 
    356 void writepng_cleanup(mainprog_info *mainprog_ptr)
    357 {
    358     png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
    359     png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
    360 
    361     if (png_ptr && info_ptr)
    362         png_destroy_write_struct(&png_ptr, &info_ptr);
    363 }
    364 
    365 
    366 
    367 
    368 
    369 static void writepng_error_handler(png_structp png_ptr, png_const_charp msg)
    370 {
    371     mainprog_info  *mainprog_ptr;
    372 
    373     /* This function, aside from the extra step of retrieving the "error
    374      * pointer" (below) and the fact that it exists within the application
    375      * rather than within libpng, is essentially identical to libpng's
    376      * default error handler.  The second point is critical:  since both
    377      * setjmp() and longjmp() are called from the same code, they are
    378      * guaranteed to have compatible notions of how big a jmp_buf is,
    379      * regardless of whether _BSD_SOURCE or anything else has (or has not)
    380      * been defined. */
    381 
    382     fprintf(stderr, "writepng libpng error: %s\n", msg);
    383     fflush(stderr);
    384 
    385     mainprog_ptr = png_get_error_ptr(png_ptr);
    386     if (mainprog_ptr == NULL) {         /* we are completely hosed now */
    387         fprintf(stderr,
    388           "writepng severe error:  jmpbuf not recoverable; terminating.\n");
    389         fflush(stderr);
    390         exit(99);
    391     }
    392 
    393     /* Now we have our data structure we can use the information in it
    394      * to return control to our own higher level code (all the points
    395      * where 'setjmp' is called in this file.)  This will work with other
    396      * error handling mechanisms as well - libpng always calls png_error
    397      * when it can proceed no further, thus, so long as the error handler
    398      * is intercepted, application code can do its own error recovery.
    399      */
    400     longjmp(mainprog_ptr->jmpbuf, 1);
    401 }
    402