Opened 15 years ago

Closed 12 years ago

#1377 closed Patches (wontfix)

Add ability to save RGBA image as TIFF with associated-alpha (premultiplied) transparency channel.

Reported by: william@… Owned by: Hailin Jin
Milestone: To Be Determined Component: gil USE GITHUB
Version: Boost Development Trunk Severity: Not Applicable
Keywords: GIL Cc: mateusz@…

Description

At the moment, GIL cannot save an image with an "A" channel to a TIFF file. This patch adds this ability.

I added "GIL" as a keyword, since there's still no "GIL" component in the drop-down list.

Attachments (3)

GIL-TIFF-SaveRGBA.patch (4.2 KB ) - added by william@… 15 years ago.
Patch which adds ability to save alpha channel as premultiplied alpha in TIFF file.
test_read_tiff_rgba.cpp (1.8 KB ) - added by john.femiani@… 14 years ago.
A testcase for reading rgba
rgba.tif (676 bytes ) - added by john.femiani@… 14 years ago.
A file to use with the testcase I attached

Download all attachments as: .zip

Change History (8)

by william@…, 15 years ago

Attachment: GIL-TIFF-SaveRGBA.patch added

Patch which adds ability to save alpha channel as premultiplied alpha in TIFF file.

comment:1 by Marshall Clow, 15 years ago

Component: NoneGIL
Owner: set to Hailin Jin

comment:2 by john.femiani@…, 14 years ago

You can not read TIFF images with alpha either.

I posted something to the mailing list: http://lists.boost.org/boost-users/2008/08/39259.php

by john.femiani@…, 14 years ago

Attachment: test_read_tiff_rgba.cpp added

A testcase for reading rgba

by john.femiani@…, 14 years ago

Attachment: rgba.tif added

A file to use with the testcase I attached

comment:3 by john.femiani@…, 14 years ago

I could not attach the dang patch, "Akismet" says it is spam.

Here is is cut-and-pasted into the wiki:

  • png_io_private.hpp

    =================================================================== 
     
    291291            default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
    292292            }
    293293            break;
     294 case PNG_COLOR_TYPE_PALETTE:
     295 io_error("png_reader_color_convert::apply(): cannot read png images with a pallette (yet)");
     296 
    294297        default: io_error("png_reader_color_convert::apply(): unknown color type");
    295298        }
    296299        png_read_end(_png_ptr,NULL);
  • tiff_dynamic_io.hpp

    =================================================================== 
     
    5656class tiff_type_format_checker {
    5757    int _bit_depth;
    5858    int _color_type;
     59 unsigned short _samples_per_pixel;
    5960public:
    60  tiff_type_format_checker(int bit_depth_in,int color_type_in) :
    61  _bit_depth(bit_depth_in),_color_type(color_type_in) {}
     61 tiff_type_format_checker( int bit_depth_in
     62 , int color_type_in
     63 , unsigned short samples_per_pixel_in
     64 )
     65 : _bit_depth(bit_depth_in)
     66 , _color_type(color_type_in)
     67 , _samples_per_pixel(samples_per_pixel_in)
     68 {}
    6269    template <typename Image>
    6370    bool apply() {
    64  return tiff_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
    65  tiff_read_support<typename Image::view_t>::color_type==_color_type;
     71 typedef tiff_read_support<typename Image::view_t> traits;
     72 
     73 int my_samples_per_pixel = size<typename Image::value_type>();
     74 
     75 bool same_bit_depth = traits::bit_depth == _bit_depth;
     76 bool same_color_type = traits::color_type == _color_type;
     77 bool same_samples_per_pixel = my_samples_per_pixel == _samples_per_pixel;
     78 
     79 bool result = same_bit_depth && same_color_type;
     80 
     81 if (_samples_per_pixel)
     82 result = result && same_samples_per_pixel;
     83 
     84 return result;
    6685    }
    6786};
    6887 
     
    7796 
    7897    template <typename Images>
    7998    void read_image(any_image<Images>& im) {
    80  int width,height;
    81  unsigned short bps,photometric;
    82  TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH,&width);
    83  TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&height);
    84  TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps);
    85  TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric);
    86  if (!construct_matched(im,tiff_type_format_checker(bps,photometric))) {
     99 int width;
     100 int height;
     101 unsigned short bps=1;
     102 unsigned short photometric = 1;
     103 unsigned short samples_per_pixel = 0;
     104 TIFFGetField(_tp, TIFFTAG_IMAGEWIDTH, &width);
     105 TIFFGetField(_tp, TIFFTAG_IMAGELENGTH, &height);
     106 TIFFGetField(_tp, TIFFTAG_BITSPERSAMPLE,&bps);
     107 TIFFGetField(_tp, TIFFTAG_PHOTOMETRIC, &photometric);
     108 TIFFGetField(_tp, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
     109 if (!construct_matched(im, tiff_type_format_checker(bps,photometric, samples_per_pixel))) {
    87110            io_error("tiff_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
    88111        } else {
    89112            im.recreate(width,height);
  • tiff_io.hpp

    =================================================================== 
     
    11/*
    22    Copyright 2005-2007 Adobe Systems Incorporated
    3  
     3 
    44    Use, modification and distribution are subject to the Boost Software License,
    55    Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    66    http://www.boost.org/LICENSE_1_0.txt).
     
    5151    BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
    5252};
    5353template <>
     54struct tiff_read_support_private<bits8,rgba_t> {
     55 BOOST_STATIC_CONSTANT(bool,is_supported=true);
     56 BOOST_STATIC_CONSTANT(int,bit_depth=8);
     57 BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
     58};
     59template <>
    5460struct tiff_read_support_private<bits16,gray_t> {
    5561    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    5662    BOOST_STATIC_CONSTANT(int,bit_depth=16);
     
    94100    BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
    95101};
    96102template <>
     103struct tiff_write_support_private<bits8,rgba_t> {
     104 BOOST_STATIC_CONSTANT(bool,is_supported=true);
     105 BOOST_STATIC_CONSTANT(int,bit_depth=8);
     106 BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
     107};
     108template <>
    97109struct tiff_write_support_private<bits16,gray_t> {
    98110    BOOST_STATIC_CONSTANT(bool,is_supported=true);
    99111    BOOST_STATIC_CONSTANT(int,bit_depth=16);
     
    129141    ~tiff_reader() { TIFFClose(_tp); }
    130142    template <typename View>
    131143    void apply(const View& view) {
    132  unsigned short bps,photometric;
     144 unsigned short bps,spp, photometric;
    133145        point2<std::ptrdiff_t> dims=get_dimensions();
    134146        io_error_if(TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps)!=1);
    135147        io_error_if(TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric)!=1);
     148 io_error_if(TIFFGetField(_tp,TIFFTAG_SAMPLESPERPIXEL,&spp)!=1);
    136149        io_error_if(dims!=view.dimensions(),
    137150                    "tiff_read_view: input view size does not match TIFF file size");
    138151        io_error_if(tiff_read_support_private<typename channel_type<View>::type,
    139152                                              typename color_space_type<View>::type>::bit_depth!=bps ||
    140153                    tiff_read_support_private<typename channel_type<View>::type,
    141  typename color_space_type<View>::type>::color_type!=photometric,
     154 typename color_space_type<View>::type>::color_type!=photometric ||
     155 mpl::size<typename color_space_type<View>::type>::value != spp,
    142156                    "tiff_read_view: input view type is incompatible with the image type");
    143157        std::size_t element_size=sizeof(pixel<typename channel_type<View>::type,
    144158                                              layout<typename color_space_type<View>::type> >);
     
    166180};
    167181 
    168182// This code will be simplified...
    169 template <typename CC>
     183template <typename CC>
    170184class tiff_reader_color_convert : public tiff_reader {
    171185private:
    172186    CC _cc;
    173187public:
    174  tiff_reader_color_convert(const char* filename) :
     188 tiff_reader_color_convert(const char* filename) :
    175189        tiff_reader(filename) {}
    176  tiff_reader_color_convert(const char* filename,CC cc_in) :
     190 tiff_reader_color_convert(const char* filename,CC cc_in) :
    177191        tiff_reader(filename),_cc(cc_in) {}
    178192    template <typename View>
    179193    void apply(const View& view) {
     
    273287        default: {
    274288            // reads an image in incompatible format via TIFFReadRGBAImage
    275289            rgba8_image_t rgbaImg(dims);
    276  io_error_if(!TIFFReadRGBAImage(_tp, dims.x, dims.y, (uint32*)&gil::view(rgbaImg)(0,0), 0),
     290 io_error_if(!TIFFReadRGBAImage(_tp, dims.x, dims.y, (uint32*)&gil::view(rgbaImg)(0,0), 0),
    277291                "tiff_reader_color_convert::unsupported image format");
    278292            copy_and_convert_pixels(flipped_up_down_view(const_view(rgbaImg)), view, _cc);
    279293        }
     
    353367/// \ingroup TIFF_IO
    354368/// \brief Loads the image specified by the given tiff image file name into the given view.
    355369/// Triggers a compile assert if the view color space and channel depth are not supported by the TIFF library or by the I/O extension.
    356 /// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not
     370/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not
    357371/// compatible with the ones specified by View, or if its dimensions don't match the ones of the view.
    358372template <typename View>
    359373inline void tiff_read_view(const char* filename,const View& view) {
     
    372386/// \ingroup TIFF_IO
    373387/// \brief Allocates a new image whose dimensions are determined by the given tiff image file, and loads the pixels into it.
    374388/// Triggers a compile assert if the image color space or channel depth are not supported by the TIFF library or by the I/O extension.
    375 /// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not
     389/// Throws std::ios_base::failure if the file is not a valid TIFF file, or if its color space or channel depth are not
    376390/// compatible with the ones specified by Image
    377391template <typename Image>
    378392void tiff_read_image(const char* filename,Image& im) {

comment:4 by mloskot <mateusz@…>, 13 years ago

Cc: mateusz@… added

comment:5 by chhenning, 12 years ago

Resolution: wontfix
Status: newclosed

Please ise the new io extension. It provides you with all the features asked for in this ticket.

Note: See TracTickets for help on using tickets.