예제 #1
0
void png_reader::read(unsigned x0, unsigned y0,image_data_32& image) 
{
    FILE *fp=fopen(fileName_.c_str(),"rb");
    if (!fp) throw image_reader_exception("cannot open image file "+fileName_);

    png_structp png_ptr = png_create_read_struct
        (PNG_LIBPNG_VER_STRING,0,0,0);

    if (!png_ptr) 
    {
        fclose(fp);
        throw image_reader_exception("failed to allocate png_ptr");
    }

    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
    {
        png_destroy_read_struct(&png_ptr,0,0);
        fclose(fp);
        throw image_reader_exception("failed to create info_ptr");
    }

    png_set_read_fn(png_ptr, (png_voidp)fp, png_read_data);
    png_read_info(png_ptr, info_ptr);

    if (color_type_ == PNG_COLOR_TYPE_PALETTE)
        png_set_expand(png_ptr);
    if (color_type_ == PNG_COLOR_TYPE_GRAY && bit_depth_ < 8)
        png_set_expand(png_ptr);
    if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
        png_set_expand(png_ptr);
    if (bit_depth_ == 16)
        png_set_strip_16(png_ptr);
    if (color_type_ == PNG_COLOR_TYPE_GRAY ||
        color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA)
        png_set_gray_to_rgb(png_ptr);

    // quick hack -- only work in >=libpng 1.2.7
    png_set_add_alpha(png_ptr,0xff,PNG_FILLER_AFTER); //rgba
        
    double gamma;
    if (png_get_gAMA(png_ptr, info_ptr, &gamma))
        png_set_gamma(png_ptr, 2.2, gamma);

    png_read_update_info(png_ptr, info_ptr);

    //START read image rows
    unsigned w=std::min(unsigned(image.width()),width_);
    unsigned h=std::min(unsigned(image.height()),height_);
    unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
    boost::scoped_array<png_byte> row(new png_byte[rowbytes]);
    for (unsigned i=0;i<height_;++i)
    {
        png_read_row(png_ptr,row.get(),0);
        if (i>=y0 && i<h) 
        {
            image.setRow(i-y0,reinterpret_cast<unsigned*>(&row[x0]),w);
        } 
    }
    //END
    png_read_end(png_ptr,0);
    png_destroy_read_struct(&png_ptr, &info_ptr,0);
    fclose(fp);
}
예제 #2
0
    void JpegReader::read(unsigned x0, unsigned y0, image_data_32& image) 
    {
        struct jpeg_decompress_struct cinfo;

        FILE *fp = fopen(fileName_.c_str(),"rb");
        if (!fp) throw image_reader_exception("JPEG Reader: cannot open image file " + fileName_);

        struct jpeg_error_mgr jerr;
        cinfo.err = jpeg_std_error(&jerr);

        jpeg_create_decompress(&cinfo);
        jpeg_stdio_src(&cinfo, fp);

        jpeg_read_header(&cinfo, TRUE);
        if (cinfo.out_color_space == JCS_UNKNOWN)
            throw image_reader_exception("JPEG Reader: failed to read unknown color space in " + fileName_);
        
        jpeg_start_decompress(&cinfo);

        if (cinfo.output_width == 0) {
          jpeg_destroy_decompress (&cinfo);
          fclose(fp);
          throw image_reader_exception("JPEG Reader: failed to read image size of " + fileName_);
        }

        JSAMPARRAY buffer;
        int row_stride;
        unsigned char a,r,g,b;
        row_stride = cinfo.output_width * cinfo.output_components;
        buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

        unsigned w = std::min(unsigned(image.width()),width_);
        unsigned h = std::min(unsigned(image.height()),height_);
        
        boost::scoped_array<unsigned int> out_row(new unsigned int[w]);
        // TODO - handle x0
        for (unsigned i=0;i<h;++i)
        {
            jpeg_read_scanlines(&cinfo, buffer, 1);
            if (i>=y0 && i<h) 
            {
                for (unsigned int x=0; x<w; x++)
                {
                    a = 255; // alpha not supported in jpg
                    r = buffer[0][cinfo.output_components * x];
                    if (cinfo.output_components > 2)
                    {
                        g = buffer[0][cinfo.output_components*x+1];
                        b = buffer[0][cinfo.output_components*x+2];
                    } else {
                        g = r;
                        b = r;
                    }
                    out_row[x] = color(r, g, b, a).rgba();
                }
                image.setRow(i-y0, out_row.get(), w);
            } 
        }
        jpeg_finish_decompress(&cinfo);
        jpeg_destroy_decompress(&cinfo);
        fclose(fp);
    }
예제 #3
0
파일: png_reader.cpp 프로젝트: rory/mapnik
void png_reader::read(unsigned x0, unsigned y0,image_data_32& image)
{
    FILE *fp=fopen(fileName_.c_str(),"rb");
    if (!fp) throw image_reader_exception("cannot open image file "+fileName_);

    png_structp png_ptr = png_create_read_struct
        (PNG_LIBPNG_VER_STRING,0,0,0);

    if (!png_ptr)
    {
        fclose(fp);
        throw image_reader_exception("failed to allocate png_ptr");
    }

    // catch errors in a custom way to avoid the need for setjmp
    png_set_error_fn(png_ptr, png_get_error_ptr(png_ptr), user_error_fn, user_warning_fn);

    png_infop info_ptr;
    try
    {
        info_ptr = png_create_info_struct(png_ptr);
        if (!info_ptr)
        {
            png_destroy_read_struct(&png_ptr,0,0);
            fclose(fp);
            throw image_reader_exception("failed to create info_ptr");
        }
    }
    catch (std::exception const& ex)
    {
        png_destroy_read_struct(&png_ptr,0,0);
        fclose(fp);
        throw;
    }

    png_set_read_fn(png_ptr, (png_voidp)fp, png_read_data);
    png_read_info(png_ptr, info_ptr);

    if (color_type_ == PNG_COLOR_TYPE_PALETTE)
        png_set_expand(png_ptr);
    if (color_type_ == PNG_COLOR_TYPE_GRAY && bit_depth_ < 8)
        png_set_expand(png_ptr);
    if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
        png_set_expand(png_ptr);
    if (bit_depth_ == 16)
        png_set_strip_16(png_ptr);
    if (color_type_ == PNG_COLOR_TYPE_GRAY ||
        color_type_ == PNG_COLOR_TYPE_GRAY_ALPHA)
        png_set_gray_to_rgb(png_ptr);

    // quick hack -- only work in >=libpng 1.2.7
    png_set_add_alpha(png_ptr,0xff,PNG_FILLER_AFTER); //rgba

    double gamma;
    if (png_get_gAMA(png_ptr, info_ptr, &gamma))
        png_set_gamma(png_ptr, 2.2, gamma);

    if (x0 == 0 && y0 == 0 && image.width() >= width_ && image.height() >= height_)
    {

        if (png_get_interlace_type(png_ptr,info_ptr) == PNG_INTERLACE_ADAM7)
        {
            png_set_interlace_handling(png_ptr); // FIXME: libpng bug?
            // according to docs png_read_image
            // "..automatically handles interlacing,
            // so you don't need to call png_set_interlace_handling()"
        }
        png_read_update_info(png_ptr, info_ptr);
        // we can read whole image at once
        // alloc row pointers
        boost::scoped_array<png_byte*> rows(new png_bytep[height_]);
        for (unsigned i=0; i<height_; ++i)
            rows[i] = (png_bytep)image.getRow(i);
        png_read_image(png_ptr, rows.get());
    }
    else
    {
        png_read_update_info(png_ptr, info_ptr);
        unsigned w=std::min(unsigned(image.width()),width_);
        unsigned h=std::min(unsigned(image.height()),height_);
        unsigned rowbytes=png_get_rowbytes(png_ptr, info_ptr);
        boost::scoped_array<png_byte> row(new png_byte[rowbytes]);
        //START read image rows
        for (unsigned i=0;i<height_;++i)
        {
            png_read_row(png_ptr,row.get(),0);
            if (i>=y0 && i<h)
            {
                image.setRow(i-y0,reinterpret_cast<unsigned*>(&row[x0]),w);
            }
        }
        //END
    }

    png_read_end(png_ptr,0);
    png_destroy_read_struct(&png_ptr, &info_ptr,0);
    fclose(fp);
}