2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write RAW Gray Image Format %
20 % Copyright 1999-2017 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/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/colorspace.h"
48 #include "MagickCore/colorspace-private.h"
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"
70 static MagickBooleanType
71 WriteGRAYImage(const ImageInfo *,Image *,ExceptionInfo *);
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % R e a d G R A Y I m a g e %
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % ReadGRAYImage() reads an image of raw grayscale samples and returns
85 % it. It allocates the memory necessary for the new Image structure and
86 % returns a pointer to the new image.
88 % The format of the ReadGRAYImage method is:
90 % Image *ReadGRAYImage(const ImageInfo *image_info,
91 % ExceptionInfo *exception)
93 % A description of each parameter follows:
95 % o image_info: the image info.
97 % o exception: return any errors or warnings in this structure.
100 static Image *ReadGRAYImage(const ImageInfo *image_info,
101 ExceptionInfo *exception)
132 assert(image_info != (const ImageInfo *) NULL);
133 assert(image_info->signature == MagickCoreSignature);
134 if (image_info->debug != MagickFalse)
135 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
136 image_info->filename);
137 assert(exception != (ExceptionInfo *) NULL);
138 assert(exception->signature == MagickCoreSignature);
139 image=AcquireImage(image_info,exception);
140 if ((image->columns == 0) || (image->rows == 0))
141 ThrowReaderException(OptionError,"MustSpecifyImageSize");
142 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
143 if (status == MagickFalse)
145 image=DestroyImageList(image);
146 return((Image *) NULL);
148 if (DiscardBlobBytes(image,(size_t) image->offset) == MagickFalse)
149 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
152 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
154 SetImageColorspace(image,GRAYColorspace,exception);
155 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
157 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod,
159 quantum_type=GrayQuantum;
160 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
161 if (quantum_info == (QuantumInfo *) NULL)
162 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
163 pixels=(const unsigned char *) NULL;
164 if (image_info->number_scenes != 0)
165 while (image->scene < image_info->scene)
171 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
172 for (y=0; y < (ssize_t) image->rows; y++)
174 pixels=(const unsigned char *) ReadBlobStream(image,length,
175 GetQuantumPixels(quantum_info),&count);
176 if (count != (ssize_t) length)
186 Read pixels to virtual canvas image then push to image.
188 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
189 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
191 status=SetImageExtent(image,image->columns,image->rows,exception);
192 if (status == MagickFalse)
193 return(DestroyImageList(image));
194 SetImageColorspace(image,GRAYColorspace,exception);
197 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
198 pixels=(const unsigned char *) ReadBlobStream(image,length,
199 GetQuantumPixels(quantum_info),&count);
201 for (y=0; y < (ssize_t) image->extract_info.height; y++)
203 register const Quantum
212 if (count != (ssize_t) length)
214 ThrowFileException(exception,CorruptImageError,
215 "UnexpectedEndOfFile",image->filename);
218 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
219 if (q == (Quantum *) NULL)
221 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
222 quantum_type,pixels,exception);
223 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
225 if (((y-image->extract_info.y) >= 0) &&
226 ((y-image->extract_info.y) < (ssize_t) image->rows))
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,
232 if ((p == (const Quantum *) NULL) ||
233 (q == (Quantum *) NULL))
235 for (x=0; x < (ssize_t) image->columns; x++)
237 SetPixelGray(image,GetPixelGray(canvas_image,p),q);
238 p+=GetPixelChannels(canvas_image);
239 q+=GetPixelChannels(image);
241 if (SyncAuthenticPixels(image,exception) == MagickFalse)
244 if (image->previous == (Image *) NULL)
246 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
248 if (status == MagickFalse)
251 pixels=(const unsigned char *) ReadBlobStream(image,length,
252 GetQuantumPixels(quantum_info),&count);
254 SetQuantumImageType(image,quantum_type);
256 Proceed to next image.
258 if (image_info->number_scenes != 0)
259 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
261 if (count == (ssize_t) length)
264 Allocate next image structure.
266 AcquireNextImage(image_info,image,exception);
267 if (GetNextImageInList(image) == (Image *) NULL)
269 image=DestroyImageList(image);
270 return((Image *) NULL);
272 image=SyncNextImageInList(image);
273 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
275 if (status == MagickFalse)
279 } while (count == (ssize_t) length);
280 quantum_info=DestroyQuantumInfo(quantum_info);
281 canvas_image=DestroyImage(canvas_image);
282 (void) CloseBlob(image);
283 return(GetFirstImageInList(image));
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % R e g i s t e r G R A Y I m a g e %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
304 % The format of the RegisterGRAYImage method is:
306 % size_t RegisterGRAYImage(void)
309 ModuleExport size_t RegisterGRAYImage(void)
314 entry=AcquireMagickInfo("GRAY","GRAY","Raw gray samples");
315 entry->decoder=(DecodeImageHandler *) ReadGRAYImage;
316 entry->encoder=(EncodeImageHandler *) WriteGRAYImage;
317 entry->flags|=CoderRawSupportFlag;
318 entry->flags|=CoderEndianSupportFlag;
319 (void) RegisterMagickInfo(entry);
320 return(MagickImageCoderSignature);
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 % U n r e g i s t e r G R A Y I m a g e %
332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 % UnregisterGRAYImage() removes format registrations made by the
335 % GRAY module from the list of supported formats.
337 % The format of the UnregisterGRAYImage method is:
339 % UnregisterGRAYImage(void)
342 ModuleExport void UnregisterGRAYImage(void)
344 (void) UnregisterMagickInfo("GRAY");
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 % W r i t e G R A Y I m a g e %
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 % WriteGRAYImage() writes an image to a file as gray scale intensity
361 % The format of the WriteGRAYImage method is:
363 % MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
364 % Image *image,ExceptionInfo *exception)
366 % A description of each parameter follows.
368 % o image_info: the image info.
370 % o image: The image.
372 % o exception: return any errors or warnings in this structure.
375 static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
376 Image *image,ExceptionInfo *exception)
401 Open output image file.
403 assert(image_info != (const ImageInfo *) NULL);
404 assert(image_info->signature == MagickCoreSignature);
405 assert(image != (Image *) NULL);
406 assert(image->signature == MagickCoreSignature);
407 if (image->debug != MagickFalse)
408 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
409 assert(exception != (ExceptionInfo *) NULL);
410 assert(exception->signature == MagickCoreSignature);
411 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
412 if (status == MagickFalse)
418 Write grayscale pixels.
420 (void) TransformImageColorspace(image,sRGBColorspace,exception);
421 quantum_type=GrayQuantum;
422 quantum_info=AcquireQuantumInfo(image_info,image);
423 if (quantum_info == (QuantumInfo *) NULL)
424 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
425 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
426 for (y=0; y < (ssize_t) image->rows; y++)
428 register const Quantum
431 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
432 if (p == (const Quantum *) NULL)
434 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
435 quantum_type,pixels,exception);
436 count=WriteBlob(image,length,pixels);
437 if (count != (ssize_t) length)
439 if (image->previous == (Image *) NULL)
441 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
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);