2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write RAW Gray Image Format %
20 % Copyright 1999-2010 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colorspace.h"
47 #include "magick/constitute.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/image.h"
51 #include "magick/image-private.h"
52 #include "magick/list.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/pixel.h"
58 #include "magick/pixel-private.h"
59 #include "magick/quantum-private.h"
60 #include "magick/static.h"
61 #include "magick/statistic.h"
62 #include "magick/string_.h"
63 #include "magick/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)
135 assert(image_info != (const ImageInfo *) NULL);
136 assert(image_info->signature == MagickSignature);
137 if (image_info->debug != MagickFalse)
138 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
139 image_info->filename);
140 assert(exception != (ExceptionInfo *) NULL);
141 assert(exception->signature == MagickSignature);
142 image=AcquireImage(image_info);
143 if ((image->columns == 0) || (image->rows == 0))
144 ThrowReaderException(OptionError,"MustSpecifyImageSize");
145 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
146 if (status == MagickFalse)
148 image=DestroyImageList(image);
149 return((Image *) NULL);
151 for (i=0; i < image->offset; i++)
152 if (ReadBlobByte(image) == EOF)
154 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
159 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
161 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
163 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod);
164 quantum_type=GrayQuantum;
165 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
166 if (quantum_info == (QuantumInfo *) NULL)
167 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
168 pixels=GetQuantumPixels(quantum_info);
169 if (image_info->number_scenes != 0)
170 while (image->scene < image_info->scene)
176 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
177 for (y=0; y < (long) image->rows; y++)
179 count=ReadBlob(image,length,pixels);
180 if (count != (ssize_t) length)
190 Read pixels to virtual canvas image then push to image.
192 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
193 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
197 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
198 count=ReadBlob(image,length,pixels);
200 for (y=0; y < (long) image->extract_info.height; y++)
202 register const PixelPacket
211 if (count != (ssize_t) length)
213 ThrowFileException(exception,CorruptImageError,
214 "UnexpectedEndOfFile",image->filename);
217 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
218 if (q == (PixelPacket *) NULL)
220 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
221 quantum_type,pixels,exception);
222 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
224 if (((y-image->extract_info.y) >= 0) &&
225 ((y-image->extract_info.y) < (long) image->rows))
227 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
228 image->columns,1,exception);
229 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
231 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
233 for (x=0; x < (long) image->columns; x++)
235 SetRedPixelComponent(q,GetRedPixelComponent(p));
236 SetGreenPixelComponent(q,GetGreenPixelComponent(p));
237 SetBluePixelComponent(q,GetBluePixelComponent(p));
241 if (SyncAuthenticPixels(image,exception) == MagickFalse)
244 if (image->previous == (Image *) NULL)
246 status=SetImageProgress(image,LoadImageTag,y,image->rows);
247 if (status == MagickFalse)
250 count=ReadBlob(image,length,pixels);
252 SetQuantumImageType(image,quantum_type);
254 Proceed to next image.
256 if (image_info->number_scenes != 0)
257 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
259 if (count == (ssize_t) length)
262 Allocate next image structure.
264 AcquireNextImage(image_info,image);
265 if (GetNextImageInList(image) == (Image *) NULL)
267 image=DestroyImageList(image);
268 return((Image *) NULL);
270 image=SyncNextImageInList(image);
271 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
273 if (status == MagickFalse)
277 } while (count == (ssize_t) length);
278 quantum_info=DestroyQuantumInfo(quantum_info);
279 InheritException(&image->exception,&canvas_image->exception);
280 canvas_image=DestroyImage(canvas_image);
281 (void) CloseBlob(image);
282 return(GetFirstImageInList(image));
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % R e g i s t e r G R A Y I m a g e %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % RegisterGRAYImage() adds attributes for the GRAY image format to
297 % the list of supported formats. The attributes include the image format
298 % tag, a method to read and/or write the format, whether the format
299 % supports the saving of more than one frame to the same file or blob,
300 % whether the format supports native in-memory I/O, and a brief
301 % description of the format.
303 % The format of the RegisterGRAYImage method is:
305 % unsigned long RegisterGRAYImage(void)
308 ModuleExport unsigned long RegisterGRAYImage(void)
313 entry=SetMagickInfo("GRAY");
314 entry->decoder=(DecodeImageHandler *) ReadGRAYImage;
315 entry->encoder=(EncodeImageHandler *) WriteGRAYImage;
316 entry->raw=MagickTrue;
317 entry->endian_support=MagickTrue;
318 entry->format_type=ExplicitFormatType;
319 entry->description=ConstantString("Raw gray samples");
320 entry->module=ConstantString("GRAY");
321 (void) RegisterMagickInfo(entry);
322 return(MagickImageCoderSignature);
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 % U n r e g i s t e r G R A Y I m a g e %
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 % UnregisterGRAYImage() removes format registrations made by the
337 % GRAY module from the list of supported formats.
339 % The format of the UnregisterGRAYImage method is:
341 % UnregisterGRAYImage(void)
344 ModuleExport void UnregisterGRAYImage(void)
346 (void) UnregisterMagickInfo("GRAY");
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 % W r i t e G R A Y I m a g e %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 % WriteGRAYImage() writes an image to a file as gray scale intensity
363 % The format of the WriteGRAYImage method is:
365 % MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
368 % A description of each parameter follows.
370 % o image_info: the image info.
372 % o image: The image.
375 static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
403 Open output image file.
405 assert(image_info != (const ImageInfo *) NULL);
406 assert(image_info->signature == MagickSignature);
407 assert(image != (Image *) NULL);
408 assert(image->signature == MagickSignature);
409 if (image->debug != MagickFalse)
410 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
411 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
412 if (status == MagickFalse)
418 Write grayscale pixels.
420 if (image->colorspace != RGBColorspace)
421 (void) TransformImageColorspace(image,RGBColorspace);
422 quantum_type=GrayQuantum;
423 quantum_info=AcquireQuantumInfo(image_info,image);
424 if (quantum_info == (QuantumInfo *) NULL)
425 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
426 pixels=GetQuantumPixels(quantum_info);
427 for (y=0; y < (long) image->rows; y++)
429 register const PixelPacket
432 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
433 if (p == (const PixelPacket *) NULL)
435 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
436 quantum_type,pixels,&image->exception);
437 count=WriteBlob(image,length,pixels);
438 if (count != (ssize_t) length)
440 if (image->previous == (Image *) NULL)
442 status=SetImageProgress(image,SaveImageTag,y,image->rows);
443 if (status == MagickFalse)
447 quantum_info=DestroyQuantumInfo(quantum_info);
448 if (GetNextImageInList(image) == (Image *) NULL)
450 image=SyncNextImageInList(image);
451 status=SetImageProgress(image,SaveImagesTag,scene++,
452 GetImageListLength(image));
453 if (status == MagickFalse)
455 } while (image_info->adjoin != MagickFalse);
456 (void) CloseBlob(image);