blob: 37d6104fce9fb312e55a44e65b661c7b8fa4d7e3 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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 %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
cristyd9ecd042012-06-17 18:26:12 +000043#include "MagickCore/attribute.h"
cristy4c08aed2011-07-01 19:47:50 +000044#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000048#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000049#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"
cristy3ed852e2009-09-05 21:47:34 +000066
67/*
68 Forward declarations.
69*/
70static MagickBooleanType
cristy1e178e72011-08-28 19:44:34 +000071 WriteGRAYImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000072
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
cristybee00932011-01-15 20:28:27 +000086% returns a pointer to the new image.
cristy3ed852e2009-09-05 21:47:34 +000087%
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*/
100static Image *ReadGRAYImage(const ImageInfo *image_info,
101 ExceptionInfo *exception)
102{
103 Image
104 *canvas_image,
105 *image;
106
cristy3ed852e2009-09-05 21:47:34 +0000107 MagickBooleanType
108 status;
109
110 MagickOffsetType
111 scene;
112
113 QuantumInfo
114 *quantum_info;
115
116 QuantumType
117 quantum_type;
118
cristy3ed852e2009-09-05 21:47:34 +0000119 size_t
120 length;
121
cristybee00932011-01-15 20:28:27 +0000122 ssize_t
cristyebc891a2011-04-24 23:04:16 +0000123 count,
124 y;
cristybee00932011-01-15 20:28:27 +0000125
cristy3ed852e2009-09-05 21:47:34 +0000126 unsigned char
127 *pixels;
128
129 /*
130 Open image file.
131 */
132 assert(image_info != (const ImageInfo *) NULL);
133 assert(image_info->signature == MagickSignature);
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 == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000139 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000140 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 }
cristy55a91cd2010-12-01 00:57:40 +0000148 if (DiscardBlobBytes(image,(size_t) image->offset) == MagickFalse)
cristyd4297022010-09-16 22:59:09 +0000149 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
150 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000151 /*
152 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
153 */
cristye2c4f182012-05-12 14:11:53 +0000154 SetImageColorspace(image,GRAYColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000155 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
156 exception);
cristy387430f2012-02-07 13:09:46 +0000157 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod,
158 exception);
cristy3ed852e2009-09-05 21:47:34 +0000159 quantum_type=GrayQuantum;
160 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
161 if (quantum_info == (QuantumInfo *) NULL)
162 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
163 pixels=GetQuantumPixels(quantum_info);
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);
cristybb503372010-05-27 20:51:26 +0000172 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000173 {
174 count=ReadBlob(image,length,pixels);
175 if (count != (ssize_t) length)
176 break;
177 }
178 }
179 scene=0;
180 count=0;
181 length=0;
182 do
183 {
184 /*
185 Read pixels to virtual canvas image then push to image.
186 */
187 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
188 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
189 break;
cristy20006872012-09-12 11:39:22 +0000190 SetImageColorspace(image,GRAYColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000191 if (scene == 0)
192 {
193 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
194 count=ReadBlob(image,length,pixels);
cristy3ed852e2009-09-05 21:47:34 +0000195 }
cristybb503372010-05-27 20:51:26 +0000196 for (y=0; y < (ssize_t) image->extract_info.height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000197 {
cristy4c08aed2011-07-01 19:47:50 +0000198 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000199 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000200
cristybb503372010-05-27 20:51:26 +0000201 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000202 x;
203
cristy4c08aed2011-07-01 19:47:50 +0000204 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000205 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000206
cristy93b13ba2009-09-12 04:01:09 +0000207 if (count != (ssize_t) length)
208 {
209 ThrowFileException(exception,CorruptImageError,
210 "UnexpectedEndOfFile",image->filename);
211 break;
212 }
cristy3ed852e2009-09-05 21:47:34 +0000213 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000214 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000215 break;
216 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
217 quantum_type,pixels,exception);
218 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
219 break;
cristy3ed852e2009-09-05 21:47:34 +0000220 if (((y-image->extract_info.y) >= 0) &&
cristybb503372010-05-27 20:51:26 +0000221 ((y-image->extract_info.y) < (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000222 {
223 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
224 image->columns,1,exception);
225 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
226 1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000227 if ((p == (const Quantum *) NULL) ||
cristyacd2ed22011-08-30 01:44:23 +0000228 (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000229 break;
cristybb503372010-05-27 20:51:26 +0000230 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000231 {
cristy08528502012-01-01 00:52:17 +0000232 SetPixelGray(image,GetPixelGray(canvas_image,p),q);
cristyed231572011-07-14 02:18:59 +0000233 p+=GetPixelChannels(canvas_image);
234 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000235 }
236 if (SyncAuthenticPixels(image,exception) == MagickFalse)
237 break;
238 }
239 if (image->previous == (Image *) NULL)
240 {
cristycee97112010-05-28 00:44:52 +0000241 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristybee00932011-01-15 20:28:27 +0000242 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000243 if (status == MagickFalse)
244 break;
245 }
cristy93b13ba2009-09-12 04:01:09 +0000246 count=ReadBlob(image,length,pixels);
cristy3ed852e2009-09-05 21:47:34 +0000247 }
248 SetQuantumImageType(image,quantum_type);
cristy3ed852e2009-09-05 21:47:34 +0000249 /*
250 Proceed to next image.
251 */
252 if (image_info->number_scenes != 0)
253 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
254 break;
255 if (count == (ssize_t) length)
256 {
257 /*
258 Allocate next image structure.
259 */
cristy9950d572011-10-01 18:22:35 +0000260 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000261 if (GetNextImageInList(image) == (Image *) NULL)
262 {
263 image=DestroyImageList(image);
264 return((Image *) NULL);
265 }
266 image=SyncNextImageInList(image);
267 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
268 GetBlobSize(image));
269 if (status == MagickFalse)
270 break;
271 }
272 scene++;
273 } while (count == (ssize_t) length);
274 quantum_info=DestroyQuantumInfo(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +0000275 canvas_image=DestroyImage(canvas_image);
276 (void) CloseBlob(image);
277 return(GetFirstImageInList(image));
278}
279
280/*
281%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282% %
283% %
284% %
285% R e g i s t e r G R A Y I m a g e %
286% %
287% %
288% %
289%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290%
291% RegisterGRAYImage() adds attributes for the GRAY image format to
292% the list of supported formats. The attributes include the image format
293% tag, a method to read and/or write the format, whether the format
294% supports the saving of more than one frame to the same file or blob,
295% whether the format supports native in-memory I/O, and a brief
296% description of the format.
297%
298% The format of the RegisterGRAYImage method is:
299%
cristybb503372010-05-27 20:51:26 +0000300% size_t RegisterGRAYImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000301%
302*/
cristybb503372010-05-27 20:51:26 +0000303ModuleExport size_t RegisterGRAYImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000304{
305 MagickInfo
306 *entry;
307
308 entry=SetMagickInfo("GRAY");
309 entry->decoder=(DecodeImageHandler *) ReadGRAYImage;
310 entry->encoder=(EncodeImageHandler *) WriteGRAYImage;
311 entry->raw=MagickTrue;
312 entry->endian_support=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000313 entry->description=ConstantString("Raw gray samples");
314 entry->module=ConstantString("GRAY");
315 (void) RegisterMagickInfo(entry);
316 return(MagickImageCoderSignature);
317}
318
319/*
320%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
321% %
322% %
323% %
324% U n r e g i s t e r G R A Y I m a g e %
325% %
326% %
327% %
328%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
329%
330% UnregisterGRAYImage() removes format registrations made by the
331% GRAY module from the list of supported formats.
332%
333% The format of the UnregisterGRAYImage method is:
334%
335% UnregisterGRAYImage(void)
336%
337*/
338ModuleExport void UnregisterGRAYImage(void)
339{
340 (void) UnregisterMagickInfo("GRAY");
341}
342
343/*
344%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
345% %
346% %
347% %
348% W r i t e G R A Y I m a g e %
349% %
350% %
351% %
352%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353%
354% WriteGRAYImage() writes an image to a file as gray scale intensity
355% values.
356%
357% The format of the WriteGRAYImage method is:
358%
359% MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000360% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000361%
362% A description of each parameter follows.
363%
364% o image_info: the image info.
365%
366% o image: The image.
367%
cristy1e178e72011-08-28 19:44:34 +0000368% o exception: return any errors or warnings in this structure.
369%
cristy3ed852e2009-09-05 21:47:34 +0000370*/
371static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
cristy1e178e72011-08-28 19:44:34 +0000372 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000373{
cristy3ed852e2009-09-05 21:47:34 +0000374 MagickBooleanType
375 status;
376
377 MagickOffsetType
378 scene;
379
380 QuantumInfo
381 *quantum_info;
382
383 QuantumType
384 quantum_type;
385
cristy3ed852e2009-09-05 21:47:34 +0000386 size_t
387 length;
388
cristyebc891a2011-04-24 23:04:16 +0000389 ssize_t
390 count,
391 y;
392
cristy3ed852e2009-09-05 21:47:34 +0000393 unsigned char
394 *pixels;
395
396 /*
397 Open output image file.
398 */
399 assert(image_info != (const ImageInfo *) NULL);
400 assert(image_info->signature == MagickSignature);
401 assert(image != (Image *) NULL);
402 assert(image->signature == MagickSignature);
403 if (image->debug != MagickFalse)
404 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000405 assert(exception != (ExceptionInfo *) NULL);
406 assert(exception->signature == MagickSignature);
cristy1e178e72011-08-28 19:44:34 +0000407 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000408 if (status == MagickFalse)
409 return(status);
410 scene=0;
411 do
412 {
413 /*
414 Write grayscale pixels.
415 */
cristy3d9f5ba2012-06-26 13:37:31 +0000416 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +0000417 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000418 quantum_type=GrayQuantum;
419 quantum_info=AcquireQuantumInfo(image_info,image);
420 if (quantum_info == (QuantumInfo *) NULL)
421 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
422 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +0000423 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000424 {
cristy4c08aed2011-07-01 19:47:50 +0000425 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000426 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000427
cristy1e178e72011-08-28 19:44:34 +0000428 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000429 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000430 break;
cristy4c08aed2011-07-01 19:47:50 +0000431 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy1e178e72011-08-28 19:44:34 +0000432 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000433 count=WriteBlob(image,length,pixels);
434 if (count != (ssize_t) length)
435 break;
436 if (image->previous == (Image *) NULL)
437 {
cristycee97112010-05-28 00:44:52 +0000438 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy2b1f0a32010-08-21 18:45:41 +0000439 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000440 if (status == MagickFalse)
441 break;
442 }
443 }
444 quantum_info=DestroyQuantumInfo(quantum_info);
445 if (GetNextImageInList(image) == (Image *) NULL)
446 break;
447 image=SyncNextImageInList(image);
448 status=SetImageProgress(image,SaveImagesTag,scene++,
449 GetImageListLength(image));
450 if (status == MagickFalse)
451 break;
452 } while (image_info->adjoin != MagickFalse);
453 (void) CloseBlob(image);
454 return(MagickTrue);
455}