2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write RAW Gray Image Format %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colorspace.h"
47 #include "MagickCore/constitute.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/image.h"
51 #include "MagickCore/image-private.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/magick.h"
54 #include "MagickCore/memory_.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/pixel.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/statistic.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
68 static MagickBooleanType
69 WriteGRAYImage(const ImageInfo *,Image *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % R e a d G R A Y I m a g e %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % ReadGRAYImage() reads an image of raw grayscale samples and returns
83 % it. It allocates the memory necessary for the new Image structure and
84 % returns a pointer to the new image.
86 % The format of the ReadGRAYImage method is:
88 % Image *ReadGRAYImage(const ImageInfo *image_info,
89 % ExceptionInfo *exception)
91 % A description of each parameter follows:
93 % o image_info: the image info.
95 % o exception: return any errors or warnings in this structure.
98 static Image *ReadGRAYImage(const ImageInfo *image_info,
99 ExceptionInfo *exception)
130 assert(image_info != (const ImageInfo *) NULL);
131 assert(image_info->signature == MagickSignature);
132 if (image_info->debug != MagickFalse)
133 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
134 image_info->filename);
135 assert(exception != (ExceptionInfo *) NULL);
136 assert(exception->signature == MagickSignature);
137 image=AcquireImage(image_info);
138 if ((image->columns == 0) || (image->rows == 0))
139 ThrowReaderException(OptionError,"MustSpecifyImageSize");
140 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
141 if (status == MagickFalse)
143 image=DestroyImageList(image);
144 return((Image *) NULL);
146 if (DiscardBlobBytes(image,(size_t) image->offset) == MagickFalse)
147 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
150 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
152 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
154 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod);
155 quantum_type=GrayQuantum;
156 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
157 if (quantum_info == (QuantumInfo *) NULL)
158 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
159 pixels=GetQuantumPixels(quantum_info);
160 if (image_info->number_scenes != 0)
161 while (image->scene < image_info->scene)
167 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
168 for (y=0; y < (ssize_t) image->rows; y++)
170 count=ReadBlob(image,length,pixels);
171 if (count != (ssize_t) length)
181 Read pixels to virtual canvas image then push to image.
183 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
184 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
188 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
189 count=ReadBlob(image,length,pixels);
191 for (y=0; y < (ssize_t) image->extract_info.height; y++)
193 register const Quantum
202 if (count != (ssize_t) length)
204 ThrowFileException(exception,CorruptImageError,
205 "UnexpectedEndOfFile",image->filename);
208 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
209 if (q == (const Quantum *) NULL)
211 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
212 quantum_type,pixels,exception);
213 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
215 if (((y-image->extract_info.y) >= 0) &&
216 ((y-image->extract_info.y) < (ssize_t) image->rows))
218 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
219 image->columns,1,exception);
220 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
222 if ((p == (const Quantum *) NULL) ||
223 (q == (const Quantum *) NULL))
225 for (x=0; x < (ssize_t) image->columns; x++)
227 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
228 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
229 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
230 p+=GetPixelChannels(canvas_image);
231 q+=GetPixelChannels(image);
233 if (SyncAuthenticPixels(image,exception) == MagickFalse)
236 if (image->previous == (Image *) NULL)
238 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
240 if (status == MagickFalse)
243 count=ReadBlob(image,length,pixels);
245 SetQuantumImageType(image,quantum_type);
247 Proceed to next image.
249 if (image_info->number_scenes != 0)
250 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
252 if (count == (ssize_t) length)
255 Allocate next image structure.
257 AcquireNextImage(image_info,image);
258 if (GetNextImageInList(image) == (Image *) NULL)
260 image=DestroyImageList(image);
261 return((Image *) NULL);
263 image=SyncNextImageInList(image);
264 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
266 if (status == MagickFalse)
270 } while (count == (ssize_t) length);
271 quantum_info=DestroyQuantumInfo(quantum_info);
272 InheritException(&image->exception,&canvas_image->exception);
273 canvas_image=DestroyImage(canvas_image);
274 (void) CloseBlob(image);
275 return(GetFirstImageInList(image));
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % R e g i s t e r G R A Y I m a g e %
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 % RegisterGRAYImage() adds attributes for the GRAY image format to
290 % the list of supported formats. The attributes include the image format
291 % tag, a method to read and/or write the format, whether the format
292 % supports the saving of more than one frame to the same file or blob,
293 % whether the format supports native in-memory I/O, and a brief
294 % description of the format.
296 % The format of the RegisterGRAYImage method is:
298 % size_t RegisterGRAYImage(void)
301 ModuleExport size_t RegisterGRAYImage(void)
306 entry=SetMagickInfo("GRAY");
307 entry->decoder=(DecodeImageHandler *) ReadGRAYImage;
308 entry->encoder=(EncodeImageHandler *) WriteGRAYImage;
309 entry->raw=MagickTrue;
310 entry->endian_support=MagickTrue;
311 entry->description=ConstantString("Raw gray samples");
312 entry->module=ConstantString("GRAY");
313 (void) RegisterMagickInfo(entry);
314 return(MagickImageCoderSignature);
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % U n r e g i s t e r G R A Y I m a g e %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % UnregisterGRAYImage() removes format registrations made by the
329 % GRAY module from the list of supported formats.
331 % The format of the UnregisterGRAYImage method is:
333 % UnregisterGRAYImage(void)
336 ModuleExport void UnregisterGRAYImage(void)
338 (void) UnregisterMagickInfo("GRAY");
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % W r i t e G R A Y I m a g e %
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 % WriteGRAYImage() writes an image to a file as gray scale intensity
355 % The format of the WriteGRAYImage method is:
357 % MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
360 % A description of each parameter follows.
362 % o image_info: the image info.
364 % o image: The image.
367 static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
393 Open output image file.
395 assert(image_info != (const ImageInfo *) NULL);
396 assert(image_info->signature == MagickSignature);
397 assert(image != (Image *) NULL);
398 assert(image->signature == MagickSignature);
399 if (image->debug != MagickFalse)
400 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
401 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
402 if (status == MagickFalse)
408 Write grayscale pixels.
410 if (image->colorspace != RGBColorspace)
411 (void) TransformImageColorspace(image,RGBColorspace);
412 quantum_type=GrayQuantum;
413 quantum_info=AcquireQuantumInfo(image_info,image);
414 if (quantum_info == (QuantumInfo *) NULL)
415 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
416 pixels=GetQuantumPixels(quantum_info);
417 for (y=0; y < (ssize_t) image->rows; y++)
419 register const Quantum
422 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
423 if (p == (const Quantum *) NULL)
425 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
426 quantum_type,pixels,&image->exception);
427 count=WriteBlob(image,length,pixels);
428 if (count != (ssize_t) length)
430 if (image->previous == (Image *) NULL)
432 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
434 if (status == MagickFalse)
438 quantum_info=DestroyQuantumInfo(quantum_info);
439 if (GetNextImageInList(image) == (Image *) NULL)
441 image=SyncNextImageInList(image);
442 status=SetImageProgress(image,SaveImagesTag,scene++,
443 GetImageListLength(image));
444 if (status == MagickFalse)
446 } while (image_info->adjoin != MagickFalse);
447 (void) CloseBlob(image);