cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % GGGG RRRR AAA Y Y % |
| 7 | % G R R A A Y Y % |
| 8 | % G GG RRRR AAAAA Y % |
| 9 | % G G R R A A Y % |
| 10 | % GGG R R A A Y % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write RAW Gray Image Format % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 20 | % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
cristy | d9ecd04 | 2012-06-17 18:26:12 +0000 | [diff] [blame] | 43 | #include "MagickCore/attribute.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #include "MagickCore/blob.h" |
| 45 | #include "MagickCore/blob-private.h" |
| 46 | #include "MagickCore/cache.h" |
| 47 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 48 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 49 | #include "MagickCore/constitute.h" |
| 50 | #include "MagickCore/exception.h" |
| 51 | #include "MagickCore/exception-private.h" |
| 52 | #include "MagickCore/image.h" |
| 53 | #include "MagickCore/image-private.h" |
| 54 | #include "MagickCore/list.h" |
| 55 | #include "MagickCore/magick.h" |
| 56 | #include "MagickCore/memory_.h" |
| 57 | #include "MagickCore/monitor.h" |
| 58 | #include "MagickCore/monitor-private.h" |
| 59 | #include "MagickCore/pixel.h" |
| 60 | #include "MagickCore/pixel-accessor.h" |
| 61 | #include "MagickCore/quantum-private.h" |
| 62 | #include "MagickCore/static.h" |
| 63 | #include "MagickCore/statistic.h" |
| 64 | #include "MagickCore/string_.h" |
| 65 | #include "MagickCore/module.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | Forward declarations. |
| 69 | */ |
| 70 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 71 | WriteGRAYImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 75 | % % |
| 76 | % % |
| 77 | % % |
| 78 | % R e a d G R A Y I m a g e % |
| 79 | % % |
| 80 | % % |
| 81 | % % |
| 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 83 | % |
| 84 | % ReadGRAYImage() reads an image of raw grayscale samples and returns |
| 85 | % it. It allocates the memory necessary for the new Image structure and |
cristy | bee0093 | 2011-01-15 20:28:27 +0000 | [diff] [blame] | 86 | % returns a pointer to the new image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 87 | % |
| 88 | % The format of the ReadGRAYImage method is: |
| 89 | % |
| 90 | % Image *ReadGRAYImage(const ImageInfo *image_info, |
| 91 | % ExceptionInfo *exception) |
| 92 | % |
| 93 | % A description of each parameter follows: |
| 94 | % |
| 95 | % o image_info: the image info. |
| 96 | % |
| 97 | % o exception: return any errors or warnings in this structure. |
| 98 | % |
| 99 | */ |
| 100 | static Image *ReadGRAYImage(const ImageInfo *image_info, |
| 101 | ExceptionInfo *exception) |
| 102 | { |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 103 | const unsigned char |
cristy | bd797f1 | 2015-01-24 20:42:32 +0000 | [diff] [blame] | 104 | *pixels; |
| 105 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 106 | Image |
| 107 | *canvas_image, |
| 108 | *image; |
| 109 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 110 | MagickBooleanType |
| 111 | status; |
| 112 | |
| 113 | MagickOffsetType |
| 114 | scene; |
| 115 | |
| 116 | QuantumInfo |
| 117 | *quantum_info; |
| 118 | |
| 119 | QuantumType |
| 120 | quantum_type; |
| 121 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 122 | size_t |
| 123 | length; |
| 124 | |
cristy | bee0093 | 2011-01-15 20:28:27 +0000 | [diff] [blame] | 125 | ssize_t |
cristy | ebc891a | 2011-04-24 23:04:16 +0000 | [diff] [blame] | 126 | count, |
| 127 | y; |
cristy | bee0093 | 2011-01-15 20:28:27 +0000 | [diff] [blame] | 128 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 129 | /* |
| 130 | Open image file. |
| 131 | */ |
| 132 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 133 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 134 | if (image_info->debug != MagickFalse) |
| 135 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 136 | image_info->filename); |
| 137 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 138 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 139 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 140 | if ((image->columns == 0) || (image->rows == 0)) |
| 141 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
| 142 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 143 | if (status == MagickFalse) |
| 144 | { |
| 145 | image=DestroyImageList(image); |
| 146 | return((Image *) NULL); |
| 147 | } |
cristy | 55a91cd | 2010-12-01 00:57:40 +0000 | [diff] [blame] | 148 | if (DiscardBlobBytes(image,(size_t) image->offset) == MagickFalse) |
cristy | d429702 | 2010-09-16 22:59:09 +0000 | [diff] [blame] | 149 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 150 | image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 151 | /* |
| 152 | Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]). |
| 153 | */ |
cristy | e2c4f18 | 2012-05-12 14:11:53 +0000 | [diff] [blame] | 154 | SetImageColorspace(image,GRAYColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse, |
| 156 | exception); |
cristy | 387430f | 2012-02-07 13:09:46 +0000 | [diff] [blame] | 157 | (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod, |
| 158 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | quantum_type=GrayQuantum; |
cristy | 5f766ef | 2014-12-14 21:12:47 +0000 | [diff] [blame] | 160 | quantum_info=AcquireQuantumInfo(image_info,canvas_image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | if (quantum_info == (QuantumInfo *) NULL) |
| 162 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 163 | pixels=(const unsigned char *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 164 | if (image_info->number_scenes != 0) |
| 165 | while (image->scene < image_info->scene) |
| 166 | { |
| 167 | /* |
| 168 | Skip to next image. |
| 169 | */ |
| 170 | image->scene++; |
| 171 | length=GetQuantumExtent(canvas_image,quantum_info,quantum_type); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 172 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | { |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 174 | pixels=(const unsigned char *) ReadBlobStream(image,length, |
| 175 | GetQuantumPixels(quantum_info),&count); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 176 | if (count != (ssize_t) length) |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | scene=0; |
| 181 | count=0; |
| 182 | length=0; |
| 183 | do |
| 184 | { |
| 185 | /* |
| 186 | Read pixels to virtual canvas image then push to image. |
| 187 | */ |
| 188 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 189 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 190 | break; |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 191 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 192 | if (status == MagickFalse) |
| 193 | return(DestroyImageList(image)); |
cristy | 2000687 | 2012-09-12 11:39:22 +0000 | [diff] [blame] | 194 | SetImageColorspace(image,GRAYColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 195 | if (scene == 0) |
| 196 | { |
| 197 | length=GetQuantumExtent(canvas_image,quantum_info,quantum_type); |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 198 | pixels=(const unsigned char *) ReadBlobStream(image,length, |
| 199 | GetQuantumPixels(quantum_info),&count); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 200 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 201 | for (y=0; y < (ssize_t) image->extract_info.height; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 202 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 203 | register const Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 204 | *magick_restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 205 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 206 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | x; |
| 208 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 209 | register Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 210 | *magick_restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 211 | |
cristy | 93b13ba | 2009-09-12 04:01:09 +0000 | [diff] [blame] | 212 | if (count != (ssize_t) length) |
| 213 | { |
| 214 | ThrowFileException(exception,CorruptImageError, |
| 215 | "UnexpectedEndOfFile",image->filename); |
| 216 | break; |
| 217 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 218 | q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 219 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 220 | break; |
| 221 | length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info, |
| 222 | quantum_type,pixels,exception); |
| 223 | if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse) |
| 224 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 225 | if (((y-image->extract_info.y) >= 0) && |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 226 | ((y-image->extract_info.y) < (ssize_t) image->rows)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 227 | { |
| 228 | p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0, |
| 229 | image->columns,1,exception); |
| 230 | q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns, |
| 231 | 1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 232 | if ((p == (const Quantum *) NULL) || |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 233 | (q == (Quantum *) NULL)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 234 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 235 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 236 | { |
cristy | 0852850 | 2012-01-01 00:52:17 +0000 | [diff] [blame] | 237 | SetPixelGray(image,GetPixelGray(canvas_image,p),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 238 | p+=GetPixelChannels(canvas_image); |
| 239 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 240 | } |
| 241 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 242 | break; |
| 243 | } |
| 244 | if (image->previous == (Image *) NULL) |
| 245 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 246 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
cristy | bee0093 | 2011-01-15 20:28:27 +0000 | [diff] [blame] | 247 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 248 | if (status == MagickFalse) |
| 249 | break; |
| 250 | } |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 251 | pixels=(const unsigned char *) ReadBlobStream(image,length, |
| 252 | GetQuantumPixels(quantum_info),&count); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 253 | } |
| 254 | SetQuantumImageType(image,quantum_type); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 255 | /* |
| 256 | Proceed to next image. |
| 257 | */ |
| 258 | if (image_info->number_scenes != 0) |
| 259 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 260 | break; |
| 261 | if (count == (ssize_t) length) |
| 262 | { |
| 263 | /* |
| 264 | Allocate next image structure. |
| 265 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 266 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 267 | if (GetNextImageInList(image) == (Image *) NULL) |
| 268 | { |
| 269 | image=DestroyImageList(image); |
| 270 | return((Image *) NULL); |
| 271 | } |
| 272 | image=SyncNextImageInList(image); |
| 273 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 274 | GetBlobSize(image)); |
| 275 | if (status == MagickFalse) |
| 276 | break; |
| 277 | } |
| 278 | scene++; |
| 279 | } while (count == (ssize_t) length); |
| 280 | quantum_info=DestroyQuantumInfo(quantum_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 281 | canvas_image=DestroyImage(canvas_image); |
| 282 | (void) CloseBlob(image); |
| 283 | return(GetFirstImageInList(image)); |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 288 | % % |
| 289 | % % |
| 290 | % % |
| 291 | % R e g i s t e r G R A Y I m a g e % |
| 292 | % % |
| 293 | % % |
| 294 | % % |
| 295 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 296 | % |
| 297 | % RegisterGRAYImage() adds attributes for the GRAY image format to |
| 298 | % the list of supported formats. The attributes include the image format |
| 299 | % tag, a method to read and/or write the format, whether the format |
| 300 | % supports the saving of more than one frame to the same file or blob, |
| 301 | % whether the format supports native in-memory I/O, and a brief |
| 302 | % description of the format. |
| 303 | % |
| 304 | % The format of the RegisterGRAYImage method is: |
| 305 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 306 | % size_t RegisterGRAYImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 307 | % |
| 308 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 309 | ModuleExport size_t RegisterGRAYImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 310 | { |
| 311 | MagickInfo |
| 312 | *entry; |
| 313 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 314 | entry=AcquireMagickInfo("GRAY","GRAY","Raw gray samples"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 315 | entry->decoder=(DecodeImageHandler *) ReadGRAYImage; |
| 316 | entry->encoder=(EncodeImageHandler *) WriteGRAYImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 317 | entry->flags|=CoderRawSupportFlag; |
| 318 | entry->flags|=CoderEndianSupportFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 319 | (void) RegisterMagickInfo(entry); |
| 320 | return(MagickImageCoderSignature); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 325 | % % |
| 326 | % % |
| 327 | % % |
| 328 | % U n r e g i s t e r G R A Y I m a g e % |
| 329 | % % |
| 330 | % % |
| 331 | % % |
| 332 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 333 | % |
| 334 | % UnregisterGRAYImage() removes format registrations made by the |
| 335 | % GRAY module from the list of supported formats. |
| 336 | % |
| 337 | % The format of the UnregisterGRAYImage method is: |
| 338 | % |
| 339 | % UnregisterGRAYImage(void) |
| 340 | % |
| 341 | */ |
| 342 | ModuleExport void UnregisterGRAYImage(void) |
| 343 | { |
| 344 | (void) UnregisterMagickInfo("GRAY"); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 349 | % % |
| 350 | % % |
| 351 | % % |
| 352 | % W r i t e G R A Y I m a g e % |
| 353 | % % |
| 354 | % % |
| 355 | % % |
| 356 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 357 | % |
| 358 | % WriteGRAYImage() writes an image to a file as gray scale intensity |
| 359 | % values. |
| 360 | % |
| 361 | % The format of the WriteGRAYImage method is: |
| 362 | % |
| 363 | % MagickBooleanType WriteGRAYImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 364 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 365 | % |
| 366 | % A description of each parameter follows. |
| 367 | % |
| 368 | % o image_info: the image info. |
| 369 | % |
| 370 | % o image: The image. |
| 371 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 372 | % o exception: return any errors or warnings in this structure. |
| 373 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 374 | */ |
| 375 | static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 376 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 377 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 378 | MagickBooleanType |
| 379 | status; |
| 380 | |
| 381 | MagickOffsetType |
| 382 | scene; |
| 383 | |
| 384 | QuantumInfo |
| 385 | *quantum_info; |
| 386 | |
| 387 | QuantumType |
| 388 | quantum_type; |
| 389 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 390 | size_t |
| 391 | length; |
| 392 | |
cristy | ebc891a | 2011-04-24 23:04:16 +0000 | [diff] [blame] | 393 | ssize_t |
| 394 | count, |
| 395 | y; |
| 396 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 397 | unsigned char |
| 398 | *pixels; |
| 399 | |
| 400 | /* |
| 401 | Open output image file. |
| 402 | */ |
| 403 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 404 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 405 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 406 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 407 | if (image->debug != MagickFalse) |
| 408 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 409 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 410 | assert(exception->signature == MagickCoreSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 411 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 412 | if (status == MagickFalse) |
| 413 | return(status); |
| 414 | scene=0; |
| 415 | do |
| 416 | { |
| 417 | /* |
| 418 | Write grayscale pixels. |
| 419 | */ |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 420 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 421 | quantum_type=GrayQuantum; |
cristy | 5f766ef | 2014-12-14 21:12:47 +0000 | [diff] [blame] | 422 | quantum_info=AcquireQuantumInfo(image_info,image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 423 | if (quantum_info == (QuantumInfo *) NULL) |
| 424 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | b3f97ae | 2015-05-18 12:29:32 +0000 | [diff] [blame] | 425 | pixels=(unsigned char *) GetQuantumPixels(quantum_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 426 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 427 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 428 | register const Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 429 | *magick_restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 430 | |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 431 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 432 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 433 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 434 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 435 | quantum_type,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 436 | count=WriteBlob(image,length,pixels); |
| 437 | if (count != (ssize_t) length) |
| 438 | break; |
| 439 | if (image->previous == (Image *) NULL) |
| 440 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 441 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | 2b1f0a3 | 2010-08-21 18:45:41 +0000 | [diff] [blame] | 442 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 443 | if (status == MagickFalse) |
| 444 | break; |
| 445 | } |
| 446 | } |
| 447 | quantum_info=DestroyQuantumInfo(quantum_info); |
| 448 | if (GetNextImageInList(image) == (Image *) NULL) |
| 449 | break; |
| 450 | image=SyncNextImageInList(image); |
| 451 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 452 | GetImageListLength(image)); |
| 453 | if (status == MagickFalse) |
| 454 | break; |
| 455 | } while (image_info->adjoin != MagickFalse); |
| 456 | (void) CloseBlob(image); |
| 457 | return(MagickTrue); |
| 458 | } |