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/colorspace-private.h"
48 #include "MagickCore/constitute.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/pixel.h"
59 #include "MagickCore/pixel-accessor.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/statistic.h"
63 #include "MagickCore/string_.h"
64 #include "MagickCore/module.h"
69 static MagickBooleanType
70 WriteGRAYImage(const ImageInfo *,Image *,ExceptionInfo *);
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % R e a d G R A Y I m a g e %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % ReadGRAYImage() reads an image of raw grayscale samples and returns
84 % it. It allocates the memory necessary for the new Image structure and
85 % returns a pointer to the new image.
87 % The format of the ReadGRAYImage method is:
89 % Image *ReadGRAYImage(const ImageInfo *image_info,
90 % ExceptionInfo *exception)
92 % A description of each parameter follows:
94 % o image_info: the image info.
96 % o exception: return any errors or warnings in this structure.
99 static Image *ReadGRAYImage(const ImageInfo *image_info,
100 ExceptionInfo *exception)
131 assert(image_info != (const ImageInfo *) NULL);
132 assert(image_info->signature == MagickSignature);
133 if (image_info->debug != MagickFalse)
134 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
135 image_info->filename);
136 assert(exception != (ExceptionInfo *) NULL);
137 assert(exception->signature == MagickSignature);
138 image=AcquireImage(image_info,exception);
139 if ((image->columns == 0) || (image->rows == 0))
140 ThrowReaderException(OptionError,"MustSpecifyImageSize");
141 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
142 if (status == MagickFalse)
144 image=DestroyImageList(image);
145 return((Image *) NULL);
147 if (DiscardBlobBytes(image,(size_t) image->offset) == MagickFalse)
148 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
151 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
153 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
155 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod);
156 quantum_type=GrayQuantum;
157 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
158 if (quantum_info == (QuantumInfo *) NULL)
159 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
160 pixels=GetQuantumPixels(quantum_info);
161 if (image_info->number_scenes != 0)
162 while (image->scene < image_info->scene)
168 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
169 for (y=0; y < (ssize_t) image->rows; y++)
171 count=ReadBlob(image,length,pixels);
172 if (count != (ssize_t) length)
182 Read pixels to virtual canvas image then push to image.
184 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
185 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
189 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
190 count=ReadBlob(image,length,pixels);
192 for (y=0; y < (ssize_t) image->extract_info.height; y++)
194 register const Quantum
203 if (count != (ssize_t) length)
205 ThrowFileException(exception,CorruptImageError,
206 "UnexpectedEndOfFile",image->filename);
209 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
210 if (q == (Quantum *) NULL)
212 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
213 quantum_type,pixels,exception);
214 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
216 if (((y-image->extract_info.y) >= 0) &&
217 ((y-image->extract_info.y) < (ssize_t) image->rows))
219 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
220 image->columns,1,exception);
221 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
223 if ((p == (const Quantum *) NULL) ||
224 (q == (Quantum *) NULL))
226 for (x=0; x < (ssize_t) image->columns; x++)
228 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
229 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
230 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
231 p+=GetPixelChannels(canvas_image);
232 q+=GetPixelChannels(image);
234 if (SyncAuthenticPixels(image,exception) == MagickFalse)
237 if (image->previous == (Image *) NULL)
239 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
241 if (status == MagickFalse)
244 count=ReadBlob(image,length,pixels);
246 SetQuantumImageType(image,quantum_type);
248 Proceed to next image.
250 if (image_info->number_scenes != 0)
251 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
253 if (count == (ssize_t) length)
256 Allocate next image structure.
258 AcquireNextImage(image_info,image,exception);
259 if (GetNextImageInList(image) == (Image *) NULL)
261 image=DestroyImageList(image);
262 return((Image *) NULL);
264 image=SyncNextImageInList(image);
265 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
267 if (status == MagickFalse)
271 } while (count == (ssize_t) length);
272 quantum_info=DestroyQuantumInfo(quantum_info);
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,
358 % Image *image,ExceptionInfo *exception)
360 % A description of each parameter follows.
362 % o image_info: the image info.
364 % o image: The image.
366 % o exception: return any errors or warnings in this structure.
369 static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
370 Image *image,ExceptionInfo *exception)
395 Open output image file.
397 assert(image_info != (const ImageInfo *) NULL);
398 assert(image_info->signature == MagickSignature);
399 assert(image != (Image *) NULL);
400 assert(image->signature == MagickSignature);
401 if (image->debug != MagickFalse)
402 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
403 assert(exception != (ExceptionInfo *) NULL);
404 assert(exception->signature == MagickSignature);
405 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
406 if (status == MagickFalse)
412 Write grayscale pixels.
414 if (IsRGBColorspace(image->colorspace) == MagickFalse)
415 (void) TransformImageColorspace(image,RGBColorspace,exception);
416 quantum_type=GrayQuantum;
417 quantum_info=AcquireQuantumInfo(image_info,image);
418 if (quantum_info == (QuantumInfo *) NULL)
419 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
420 pixels=GetQuantumPixels(quantum_info);
421 for (y=0; y < (ssize_t) image->rows; y++)
423 register const Quantum
426 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
427 if (p == (const Quantum *) NULL)
429 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
430 quantum_type,pixels,exception);
431 count=WriteBlob(image,length,pixels);
432 if (count != (ssize_t) length)
434 if (image->previous == (Image *) NULL)
436 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
438 if (status == MagickFalse)
442 quantum_info=DestroyQuantumInfo(quantum_info);
443 if (GetNextImageInList(image) == (Image *) NULL)
445 image=SyncNextImageInList(image);
446 status=SetImageProgress(image,SaveImagesTag,scene++,
447 GetImageListLength(image));
448 if (status == MagickFalse)
450 } while (image_info->adjoin != MagickFalse);
451 (void) CloseBlob(image);