Ticket #35995: patch-libpng15.diff

File patch-libpng15.diff, 11.0 KB (added by raphael-st (Raphael Straub), 12 years ago)
  • src/vigra_impex/png.cxx

    old new  
    6565#include <png.h>
    6666}
    6767
     68#define STRINGIFY(m_argument) #m_argument
     69
    6870#if PNG_LIBPNG_VER < 10201
    6971#error "please update your libpng to at least 1.2.1"
    7072#endif
    7173
     74#if PNG_LIBPNG_VER >= 10400
     75#define PNG_SET_EXPAND_GRAY_1_2_4_TO_8_NAME png_set_expand_gray_1_2_4_to_8
     76#else
     77#define PNG_SET_EXPAND_GRAY_1_2_4_TO_8_NAME png_set_gray_1_2_4_to_8
     78#endif
     79#define PNG_SET_EXPAND_GRAY_1_2_4_TO_8_FUNCTION_NAME STRINGIFY(PNG_SET_EXPAND_GRAY_1_2_4_TO_8_NAME)
     80#define PNG_SET_EXPAND_GRAY_1_2_4_TO_8(m_image) PNG_SET_EXPAND_GRAY_1_2_4_TO_8_NAME(m_image)
     81
    7282// TODO: per-scanline reading/writing
    7383
    7484namespace {
     
    8191static void PngError( png_structp png_ptr, png_const_charp error_msg )
    8292{
    8393    png_error_message = std::string(error_msg);
    84     longjmp( png_ptr->jmpbuf, 1 );
     94    longjmp( png_jmpbuf(png_ptr), 1 );
    8595}
    8696
    8797// called on non-fatal errors
     
    203213        // check if the file is a png file
    204214        const unsigned int sig_size = 8;
    205215        png_byte sig[sig_size];
    206         std::fread( sig, sig_size, 1, file.get() );
     216        std::size_t readCount = std::fread( sig, sig_size, 1, file.get() );
    207217        const int no_png = png_sig_cmp( sig, 0, sig_size );
    208         vigra_precondition( !no_png, "given file is not a png file.");
     218        vigra_precondition( (readCount == 1) && !no_png, "given file is not a png file.");
    209219
    210220        // create png read struct with user defined handlers
    211221        png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL,
     
    213223        vigra_postcondition( png != 0, "could not create the read struct." );
    214224
    215225        // create info struct
    216         if (setjmp(png->jmpbuf)) {
     226        if (setjmp(png_jmpbuf(png))) {
    217227            png_destroy_read_struct( &png, &info, NULL );
    218228            vigra_postcondition( false, png_error_message.insert(0, "error in png_create_info_struct(): ").c_str() );
    219229        }
     
    221231        vigra_postcondition( info != 0, "could not create the info struct." );
    222232
    223233        // init png i/o
    224         if (setjmp(png->jmpbuf)) {
     234        if (setjmp(png_jmpbuf(png))) {
    225235            png_destroy_read_struct( &png, &info, NULL );
    226236            vigra_postcondition( false, png_error_message.insert(0, "error in png_init_io(): ").c_str() );
    227237        }
    228238        png_init_io( png, file.get() );
    229239
    230240        // specify that the signature was already read
    231         if (setjmp(png->jmpbuf)) {
     241        if (setjmp(png_jmpbuf(png))) {
    232242            png_destroy_read_struct( &png, &info, NULL );
    233243            vigra_postcondition( false, png_error_message.insert(0, "error in png_set_sig_bytes(): ").c_str() );
    234244        }
     
    244254    void PngDecoderImpl::init()
    245255    {
    246256        // read all chunks up to the image data
    247         if (setjmp(png->jmpbuf))
     257        if (setjmp(png_jmpbuf(png)))
    248258            vigra_postcondition( false, png_error_message.insert(0, "error in png_read_info(): ").c_str() );
    249259        png_read_info( png, info );
    250260
    251261        // pull over the header fields
    252262        int interlace_method, compression_method, filter_method;
    253         if (setjmp(png->jmpbuf))
     263        if (setjmp(png_jmpbuf(png)))
    254264            vigra_postcondition( false, png_error_message.insert(0, "error in png_get_IHDR(): ").c_str() );
    255265        png_get_IHDR( png, info, &width, &height, &bit_depth, &color_type,
    256266                      &interlace_method, &compression_method, &filter_method );
     
    264274
    265275        // transform palette to rgb
    266276        if ( color_type == PNG_COLOR_TYPE_PALETTE) {
    267             if (setjmp(png->jmpbuf))
     277            if (setjmp(png_jmpbuf(png)))
    268278                vigra_postcondition( false, png_error_message.insert(0, "error in png_palette_to_rgb(): ").c_str() );
    269279            png_set_palette_to_rgb(png);
    270280            color_type = PNG_COLOR_TYPE_RGB;
     
    273283
    274284        // expand gray values to at least one byte size
    275285        if ( color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8 ) {
    276             if (setjmp(png->jmpbuf))
    277                 vigra_postcondition( false,png_error_message.insert(0, "error in png_set_gray_1_2_4_to_8(): ").c_str());
    278             png_set_gray_1_2_4_to_8(png);
     286            if (setjmp(png_jmpbuf(png)))
     287                vigra_postcondition( false, png_error_message.insert(0, "error in " PNG_SET_EXPAND_GRAY_1_2_4_TO_8_FUNCTION_NAME " (): ").c_str());
     288            PNG_SET_EXPAND_GRAY_1_2_4_TO_8(png);
    279289            bit_depth = 8;
    280290        }
    281291
     
    283293#if 0
    284294        // strip alpha channel
    285295        if ( color_type & PNG_COLOR_MASK_ALPHA ) {
    286             if (setjmp(png->jmpbuf))
     296            if (setjmp(png_jmpbuf(png)))
    287297                vigra_postcondition( false, png_error_message.insert(0, "error in png_set_strip_alpha(): ").c_str() );
    288298            png_set_strip_alpha(png);
    289299            color_type ^= PNG_COLOR_MASK_ALPHA;
     
    325335#if (PNG_LIBPNG_VER > 10008) && defined(PNG_READ_iCCP_SUPPORTED)
    326336        char * dummyName;
    327337        int dummyCompType;
     338#if (PNG_LIBPNG_VER < 10500)
    328339        char * profilePtr;
     340#else
     341        png_byte * profilePtr;
     342#endif
    329343        png_uint_32 profileLen;
    330         if (info->valid & PNG_INFO_iCCP) {
     344        if (png_get_valid( png, info, PNG_INFO_iCCP )) {
    331345            png_get_iCCP(png, info, &dummyName, &dummyCompType, &profilePtr, &profileLen) ;
    332346            iccProfilePtr = (unsigned char *) profilePtr;
    333347            iccProfileLength = profileLen;
     
    340354        // image gamma
    341355        double image_gamma = 0.45455;
    342356        if ( png_get_valid( png, info, PNG_INFO_gAMA ) ) {
    343             if (setjmp(png->jmpbuf))
     357            if (setjmp(png_jmpbuf(png)))
    344358                vigra_postcondition( false, png_error_message.insert(0, "error in png_get_gAMA(): ").c_str() );
    345359            png_get_gAMA( png, info, &image_gamma );
    346360        }
     
    349363        double screen_gamma = 2.2;
    350364
    351365        // set gamma correction
    352         if (setjmp(png->jmpbuf))
     366        if (setjmp(png_jmpbuf(png)))
    353367            vigra_postcondition( false, png_error_message.insert(0, "error in png_set_gamma(): ").c_str() );
    354368        png_set_gamma( png, screen_gamma, image_gamma );
    355369#endif
    356370
    357371        // interlace handling, get number of read passes needed
    358         if (setjmp(png->jmpbuf))
     372        if (setjmp(png_jmpbuf(png)))
    359373            vigra_postcondition( false,png_error_message.insert(0, "error in png_set_interlace_handling(): ").c_str());
    360374        n_interlace_passes = png_set_interlace_handling(png);
    361375
    362376        // update png library state to reflect any changes that were made
    363         if (setjmp(png->jmpbuf))
     377        if (setjmp(png_jmpbuf(png)))
    364378            vigra_postcondition( false, png_error_message.insert(0, "error in png_read_update_info(): ").c_str() );
    365379        png_read_update_info( png, info );
    366380
    367         if (setjmp(png->jmpbuf))
     381        if (setjmp(png_jmpbuf(png)))
    368382            vigra_postcondition( false,png_error_message.insert(0, "error in png_get_channels(): ").c_str());
    369383        n_channels = png_get_channels(png, info);
    370384
    371         if (setjmp(png->jmpbuf))
     385        if (setjmp(png_jmpbuf(png)))
    372386            vigra_postcondition( false,png_error_message.insert(0, "error in png_get_rowbytes(): ").c_str());
    373387        rowsize = png_get_rowbytes(png, info);
    374388
     
    378392
    379393    void PngDecoderImpl::nextScanline()
    380394    {
    381         for (int i=0; i < n_interlace_passes; i++) {
    382         if (setjmp(png->jmpbuf))
    383                 vigra_postcondition( false,png_error_message.insert(0, "error in png_read_row(): ").c_str());
     395        if (setjmp(png_jmpbuf(png)))
     396            vigra_postcondition( false,png_error_message.insert(0, "error in png_read_row(): ").c_str());       
     397        for (int i=0; i < n_interlace_passes; i++)
     398        {
    384399            png_read_row(png, row_data.begin(), NULL);
    385400        }
    386401    }
     
    545560        vigra_postcondition( png != 0, "could not create the write struct." );
    546561
    547562        // create info struct
    548         if (setjmp(png->jmpbuf)) {
     563        if (setjmp(png_jmpbuf(png))) {
    549564            png_destroy_write_struct( &png, &info );
    550565            vigra_postcondition( false, png_error_message.insert(0, "error in png_info_struct(): ").c_str() );
    551566        }
     
    556571        }
    557572
    558573        // init png i/o
    559         if (setjmp(png->jmpbuf)) {
     574        if (setjmp(png_jmpbuf(png))) {
    560575            png_destroy_write_struct( &png, &info );
    561576            vigra_postcondition( false, png_error_message.insert(0, "error in png_init_io(): ").c_str() );
    562577        }
     
    571586    void PngEncoderImpl::finalize()
    572587    {
    573588        // write the IHDR
    574         if (setjmp(png->jmpbuf))
     589        if (setjmp(png_jmpbuf(png)))
    575590            vigra_postcondition( false, png_error_message.insert(0, "error in png_set_IHDR(): ").c_str() );
    576591        png_set_IHDR( png, info, width, height, bit_depth, color_type,
    577592                      PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
     
    579594
    580595        // set resolution
    581596        if (x_resolution > 0 && y_resolution > 0) {
    582             if (setjmp(png->jmpbuf))
     597            if (setjmp(png_jmpbuf(png)))
    583598                vigra_postcondition( false, png_error_message.insert(0, "error in png_set_pHYs(): ").c_str() );
    584599            png_set_pHYs(png, info, (png_uint_32) (x_resolution / 0.0254 + 0.5),
    585600                         (png_uint_32) (y_resolution / 0.0254 + 0.5),
     
    588603
    589604        // set offset
    590605        if (position.x > 0 && position.y > 0) {
    591             if (setjmp(png->jmpbuf))
     606            if (setjmp(png_jmpbuf(png)))
    592607                vigra_postcondition( false, png_error_message.insert(0, "error in png_set_oFFs(): ").c_str() );
    593608            png_set_oFFs(png, info, position.x, position.y, PNG_OFFSET_PIXEL);
    594609        }
     
    596611#if (PNG_LIBPNG_VER > 10008) && defined(PNG_WRITE_iCCP_SUPPORTED)
    597612        // set icc profile
    598613        if (iccProfile.size() > 0) {
    599             png_set_iCCP(png, info, "icc", 0,
    600                          (char *)iccProfile.begin(), iccProfile.size());
     614            png_set_iCCP(png, info, (png_charp)("icc"), 0,
     615#if (PNG_LIBPNG_VER < 10500)
     616                         (png_charp)iccProfile.begin(), (png_uint_32)iccProfile.size());
     617#else
     618                         (png_byte*)iccProfile.begin(), (png_uint_32)iccProfile.size());
     619#endif
    601620        }
    602621#endif
    603622
    604623        // write the info struct
    605         if (setjmp(png->jmpbuf))
     624        if (setjmp(png_jmpbuf(png)))
    606625            vigra_postcondition( false, png_error_message.insert(0, "error in png_write_info(): ").c_str() );
    607626        png_write_info( png, info );
    608627
     
    634653        }
    635654
    636655        // write the whole image
    637         if (setjmp(png->jmpbuf))
     656        if (setjmp(png_jmpbuf(png)))
    638657            vigra_postcondition( false, png_error_message.insert(0, "error in png_write_image(): ").c_str() );
    639658        png_write_image( png, row_pointers.begin() );
    640         if (setjmp(png->jmpbuf))
     659        if (setjmp(png_jmpbuf(png)))
    641660            vigra_postcondition( false, png_error_message.insert(0, "error in png_write_end(): ").c_str() );
    642661        png_write_end(png, info);
    643662    }