comparison libinterp/dldfcn/__magick_read__.cc @ 17240:d757c98636d8

Use first image to be read rather than first image in file to set defaults. * __magick_read__.cc (read_indexed_images, read_images, __magick_read__): use the index for the first image that will be actually read to infer the bitdepth and type of image, instead of the first image in the file. * private/__imread__.m: separate finding the image index from the rest of all other options since we need to know it before calling imfinfo which will be used to set defaults.
author Carnë Draug <carandraug@octave.org>
date Wed, 14 Aug 2013 01:54:34 +0100
parents d6467d6dfb83
children eb7e9a991ffb
comparison
equal deleted inserted replaced
17239:d6467d6dfb83 17240:d757c98636d8
89 std::map<std::string, octave_idx_type> region = calculate_region (options); 89 std::map<std::string, octave_idx_type> region = calculate_region (options);
90 const octave_idx_type nFrames = frameidx.length (); 90 const octave_idx_type nFrames = frameidx.length ();
91 const octave_idx_type nRows = region["row_out"]; 91 const octave_idx_type nRows = region["row_out"];
92 const octave_idx_type nCols = region["col_out"]; 92 const octave_idx_type nCols = region["col_out"];
93 93
94 // imvec has all of the pages of a file, even the ones we are not
95 // interested in. We will use the first image that we will be actually
96 // reading to get information about the image.
97 const octave_idx_type def_elem = frameidx(0);
98
94 T img = T (dim_vector (nRows, nCols, 1, nFrames)); 99 T img = T (dim_vector (nRows, nCols, 1, nFrames));
95 P* img_fvec = img.fortran_vec (); 100 P* img_fvec = img.fortran_vec ();
96 101
97 const octave_idx_type row_start = region["row_start"]; 102 const octave_idx_type row_start = region["row_start"];
98 const octave_idx_type col_start = region["col_start"]; 103 const octave_idx_type col_start = region["col_start"];
126 retval(0) = octave_value (img); 131 retval(0) = octave_value (img);
127 132
128 // Do we need to get the colormap to interpret the image and alpha channel? 133 // Do we need to get the colormap to interpret the image and alpha channel?
129 if (nargout > 1) 134 if (nargout > 1)
130 { 135 {
131 const octave_idx_type mapsize = imvec[0].colorMapSize (); 136 const octave_idx_type mapsize = imvec[def_elem].colorMapSize ();
132 Matrix cmap = Matrix (mapsize, 3); 137 Matrix cmap = Matrix (mapsize, 3);
133 138
134 // In theory, it should be possible for each frame of an image to 139 // In theory, it should be possible for each frame of an image to
135 // have different colormaps but for Matlab compatibility, we only 140 // have different colormaps but for Matlab compatibility, we only
136 // return the colormap of the first frame. 141 // return the colormap of the first frame.
137 142
138 // only get alpha channel if it exists and was requested as output 143 // only get alpha channel if it exists and was requested as output
139 if (imvec[0].matte () && nargout >= 3) 144 if (imvec[def_elem].matte () && nargout >= 3)
140 { 145 {
141 Matrix amap = Matrix (mapsize, 1); 146 Matrix amap = Matrix (mapsize, 1);
142 for (octave_idx_type i = 0; i < mapsize; i++) 147 for (octave_idx_type i = 0; i < mapsize; i++)
143 { 148 {
144 const Magick::ColorRGB c = imvec[0].colorMap (i); 149 const Magick::ColorRGB c = imvec[def_elem].colorMap (i);
145 cmap(i,0) = c.red (); 150 cmap(i,0) = c.red ();
146 cmap(i,1) = c.green (); 151 cmap(i,1) = c.green ();
147 cmap(i,2) = c.blue (); 152 cmap(i,2) = c.blue ();
148 amap(i,0) = c.alpha (); 153 amap(i,0) = c.alpha ();
149 } 154 }
166 171
167 else 172 else
168 { 173 {
169 for (octave_idx_type i = 0; i < mapsize; i++) 174 for (octave_idx_type i = 0; i < mapsize; i++)
170 { 175 {
171 const Magick::ColorRGB c = imvec[0].colorMap (i); 176 const Magick::ColorRGB c = imvec[def_elem].colorMap (i);
172 cmap(i,0) = c.red (); 177 cmap(i,0) = c.red ();
173 cmap(i,1) = c.green (); 178 cmap(i,1) = c.green ();
174 cmap(i,2) = c.blue (); 179 cmap(i,2) = c.blue ();
175 } 180 }
176 } 181 }
199 std::map<std::string, octave_idx_type> region = calculate_region (options); 204 std::map<std::string, octave_idx_type> region = calculate_region (options);
200 const octave_idx_type nFrames = frameidx.length (); 205 const octave_idx_type nFrames = frameidx.length ();
201 const octave_idx_type nRows = region["row_out"]; 206 const octave_idx_type nRows = region["row_out"];
202 const octave_idx_type nCols = region["col_out"]; 207 const octave_idx_type nCols = region["col_out"];
203 T img; 208 T img;
209
210 // imvec has all of the pages of a file, even the ones we are not
211 // interested in. We will use the first image that we will be actually
212 // reading to get information about the image.
213 const octave_idx_type def_elem = frameidx(0);
204 214
205 const octave_idx_type row_start = region["row_start"]; 215 const octave_idx_type row_start = region["row_start"];
206 const octave_idx_type col_start = region["col_start"]; 216 const octave_idx_type col_start = region["col_start"];
207 const octave_idx_type row_shift = region["row_shift"]; 217 const octave_idx_type row_shift = region["row_shift"];
208 const octave_idx_type col_shift = region["col_shift"]; 218 const octave_idx_type col_shift = region["col_shift"];
226 // uint64_t is used in expression because default 32-bit value overflows 236 // uint64_t is used in expression because default 32-bit value overflows
227 // when depth() is 32. 237 // when depth() is 32.
228 // TODO in the next release of GraphicsMagick, MaxRGB should be replaced 238 // TODO in the next release of GraphicsMagick, MaxRGB should be replaced
229 // with QuantumRange since MaxRGB is already deprecated in ImageMagick. 239 // with QuantumRange since MaxRGB is already deprecated in ImageMagick.
230 double divisor; 240 double divisor;
231 if (imvec[0].depth () == 32) 241 if (imvec[def_elem].depth () == 32)
232 divisor = std::numeric_limits<uint32_t>::max (); 242 divisor = std::numeric_limits<uint32_t>::max ();
233 else 243 else
234 divisor = MaxRGB / ((uint64_t (1) << imvec[0].depth ()) - 1); 244 divisor = MaxRGB / ((uint64_t (1) << imvec[def_elem].depth ()) - 1);
235 245
236 // FIXME: this workaround should probably be fixed in GM by creating a 246 // FIXME: this workaround should probably be fixed in GM by creating a
237 // new ImageType BilevelMatteType 247 // new ImageType BilevelMatteType
238 // Despite what GM documentation claims, opacity is not only on the types 248 // Despite what GM documentation claims, opacity is not only on the types
239 // with Matte on the name. It is possible that an image is completely 249 // with Matte on the name. It is possible that an image is completely
240 // black (1 color), and have a second channel set for transparency (2nd 250 // black (1 color), and have a second channel set for transparency (2nd
241 // color). Its type will be bilevel since there is no BilevelMatte. The 251 // color). Its type will be bilevel since there is no BilevelMatte. The
242 // only way to check for this seems to be by checking matte (). 252 // only way to check for this seems to be by checking matte ().
243 Magick::ImageType type = imvec[0].type (); 253 Magick::ImageType type = imvec[def_elem].type ();
244 if (type == Magick::BilevelType && imvec[0].matte ()) 254 if (type == Magick::BilevelType && imvec[def_elem].matte ())
245 type = Magick::GrayscaleMatteType; 255 type = Magick::GrayscaleMatteType;
246 256
247 // FIXME: ImageType is the type being used to represent the image in memory 257 // FIXME: ImageType is the type being used to represent the image in memory
248 // by GM. The real type may be different (see among others bug #36820). For 258 // by GM. The real type may be different (see among others bug #36820). For
249 // example, a png file where all channels are equal may report being 259 // example, a png file where all channels are equal may report being
656 return output; 666 return output;
657 } 667 }
658 } 668 }
659 } 669 }
660 670
661 const Magick::ClassType klass = imvec[0].classType (); 671 const Magick::ClassType klass = imvec[frameidx(0)].classType ();
662 const octave_idx_type depth = imvec[0].depth (); 672 const octave_idx_type depth = imvec[frameidx(0)].depth ();
663 673
664 // Magick::ClassType 674 // Magick::ClassType
665 // PseudoClass: 675 // PseudoClass:
666 // Image is composed of pixels which specify an index in a color palette. 676 // Image is composed of pixels which specify an index in a color palette.
667 // DirectClass: 677 // DirectClass: