I found a bug in GIL, and I think I have a patch for it. Basically the
tiff reading code in GIL fails to handle alpha channels properly.
Basically an RGBA tiff can be read into an RGB image, causing garbage.
The source of the problem seems to be that TIFFTAG_PHOTOMETRIC is the
only thing used to decide which pixel format to read. One should also
read TIFFTAG_SAMPLESPERPIXEL to see if there are any extra fields, and
TIFFTAG_EXTRASAMPLES to figure out what they mean. (I did not do all
that in this little patch).
On a related note:
1) Is GIL updating the boost/SVN repository? It seems to have been a
long time (6 months) since the last change (removing tabs)
2) What is the status of the new_io library (Christian Henning was
working on it I think).
I think that this is the best hope for dealing with the IO issues I tend
to have with GIL so far (it does not read RGBA jpegs either, it can not
handle palettes, certain kinds of tiff compression, etc.)
--John
Index: png_io_private.hpp
===================================================================
--- png_io_private.hpp (revision 48136)
+++ png_io_private.hpp (working copy)
@@ -291,6 +291,9 @@
default: io_error("png_reader_color_convert::apply(): unknown combination of color type and bit depth");
}
break;
+ case PNG_COLOR_TYPE_PALETTE:
+ io_error("png_reader_color_convert::apply(): cannot read png images with a pallette (yet)");
+
default: io_error("png_reader_color_convert::apply(): unknown color type");
}
png_read_end(_png_ptr,NULL);
Index: tiff_dynamic_io.hpp
===================================================================
--- tiff_dynamic_io.hpp (revision 48136)
+++ tiff_dynamic_io.hpp (working copy)
@@ -56,13 +56,32 @@
class tiff_type_format_checker {
int _bit_depth;
int _color_type;
+ unsigned short _samples_per_pixel;
public:
- tiff_type_format_checker(int bit_depth_in,int color_type_in) :
- _bit_depth(bit_depth_in),_color_type(color_type_in) {}
+ tiff_type_format_checker( int bit_depth_in
+ , int color_type_in
+ , unsigned short samples_per_pixel_in
+ )
+ : _bit_depth(bit_depth_in)
+ , _color_type(color_type_in)
+ , _samples_per_pixel(samples_per_pixel_in)
+ {}
template <typename Image>
bool apply() {
- return tiff_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
- tiff_read_support<typename Image::view_t>::color_type==_color_type;
+ typedef tiff_read_support<typename Image::view_t> traits;
+
+ int my_samples_per_pixel = size<typename Image::value_type>();
+
+ bool same_bit_depth = traits::bit_depth == _bit_depth;
+ bool same_color_type = traits::color_type == _color_type;
+ bool same_samples_per_pixel = my_samples_per_pixel == _samples_per_pixel;
+
+ bool result = same_bit_depth && same_color_type;
+
+ if (_samples_per_pixel)
+ result = result && same_samples_per_pixel;
+
+ return result;
}
};
@@ -77,13 +96,17 @@
template <typename Images>
void read_image(any_image<Images>& im) {
- int width,height;
- unsigned short bps,photometric;
- TIFFGetField(_tp,TIFFTAG_IMAGEWIDTH,&width);
- TIFFGetField(_tp,TIFFTAG_IMAGELENGTH,&height);
- TIFFGetField(_tp,TIFFTAG_BITSPERSAMPLE,&bps);
- TIFFGetField(_tp,TIFFTAG_PHOTOMETRIC,&photometric);
- if (!construct_matched(im,tiff_type_format_checker(bps,photometric))) {
+ int width;
+ int height;
+ unsigned short bps=1;
+ unsigned short photometric = 1;
+ unsigned short samples_per_pixel = 0;
+ TIFFGetField(_tp, TIFFTAG_IMAGEWIDTH, &width);
+ TIFFGetField(_tp, TIFFTAG_IMAGELENGTH, &height);
+ TIFFGetField(_tp, TIFFTAG_BITSPERSAMPLE,&bps);
+ TIFFGetField(_tp, TIFFTAG_PHOTOMETRIC, &photometric);
+ TIFFGetField(_tp, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
+ if (!construct_matched(im, tiff_type_format_checker(bps,photometric, samples_per_pixel))) {
io_error("tiff_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
} else {
im.recreate(width,height);
Index: tiff_io.hpp
===================================================================
--- tiff_io.hpp (revision 48136)
+++ tiff_io.hpp (working copy)
@@ -1,6 +1,6 @@
/*
Copyright 2005-2007 Adobe Systems Incorporated
-
+
Use, modification and distribution are subject to the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt).
@@ -51,6 +51,12 @@
BOOST_STATIC_CONSTANT(int,color_type=PHOTOMETRIC_RGB);
};
template <>
+struct tiff_read_support_private
participants (1)
-
John C. Femiani