blob: 68e00712815a6f1c82e79c256103a05dbcbc018b [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% %
20% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21% 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*/
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"
64
65/*
66 Forward declarations.
67*/
68static MagickBooleanType
69 WriteGRAYImage(const ImageInfo *,Image *);
70
71/*
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
76% R e a d G R A Y I m a g e %
77% %
78% %
79% %
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%
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.
85%
86% The format of the ReadGRAYImage method is:
87%
88% Image *ReadGRAYImage(const ImageInfo *image_info,
89% ExceptionInfo *exception)
90%
91% A description of each parameter follows:
92%
93% o image_info: the image info.
94%
95% o exception: return any errors or warnings in this structure.
96%
97*/
98static Image *ReadGRAYImage(const ImageInfo *image_info,
99 ExceptionInfo *exception)
100{
101 Image
102 *canvas_image,
103 *image;
104
105 long
106 y;
107
108 MagickBooleanType
109 status;
110
111 MagickOffsetType
112 scene;
113
114 QuantumInfo
115 *quantum_info;
116
117 QuantumType
118 quantum_type;
119
120 register long
121 i;
122
123 ssize_t
124 count;
125
126 size_t
127 length;
128
129 unsigned char
130 *pixels;
131
132 /*
133 Open image file.
134 */
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)
147 {
148 image=DestroyImageList(image);
149 return((Image *) NULL);
150 }
151 for (i=0; i < image->offset; i++)
152 if (ReadBlobByte(image) == EOF)
153 {
154 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
155 image->filename);
156 break;
157 }
158 /*
159 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
160 */
161 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
162 exception);
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)
171 {
172 /*
173 Skip to next image.
174 */
175 image->scene++;
176 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
177 for (y=0; y < (long) image->rows; y++)
178 {
179 count=ReadBlob(image,length,pixels);
180 if (count != (ssize_t) length)
181 break;
182 }
183 }
184 scene=0;
185 count=0;
186 length=0;
187 do
188 {
189 /*
190 Read pixels to virtual canvas image then push to image.
191 */
192 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
193 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
194 break;
195 if (scene == 0)
196 {
197 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
198 count=ReadBlob(image,length,pixels);
199 if (count != (ssize_t) length)
200 break;
201 }
202 for (y=0; y < (long) image->extract_info.height; y++)
203 {
204 register const PixelPacket
205 *__restrict p;
206
207 register long
208 x;
209
210 register PixelPacket
211 *__restrict q;
212
213 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
214 if (q == (PixelPacket *) NULL)
215 break;
216 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
217 quantum_type,pixels,exception);
218 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
219 break;
220 count=ReadBlob(image,length,pixels);
221 if (((y-image->extract_info.y) >= 0) &&
222 ((y-image->extract_info.y) < (long) image->rows))
223 {
224 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
225 image->columns,1,exception);
226 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
227 1,exception);
228 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
229 break;
230 for (x=0; x < (long) image->columns; x++)
231 {
232 q->red=p->red;
233 q->green=p->green;
234 q->blue=p->blue;
235 p++;
236 q++;
237 }
238 if (SyncAuthenticPixels(image,exception) == MagickFalse)
239 break;
240 }
241 if (image->previous == (Image *) NULL)
242 {
243 status=SetImageProgress(image,LoadImageTag,y,image->rows);
244 if (status == MagickFalse)
245 break;
246 }
247 }
248 SetQuantumImageType(image,quantum_type);
249 /*
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 */
260 AcquireNextImage(image_info,image);
261 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);
275 InheritException(exception,&canvas_image->exception);
276 canvas_image=DestroyImage(canvas_image);
277 (void) CloseBlob(image);
278 return(GetFirstImageInList(image));
279}
280
281/*
282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283% %
284% %
285% %
286% R e g i s t e r G R A Y I m a g e %
287% %
288% %
289% %
290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291%
292% RegisterGRAYImage() adds attributes for the GRAY image format to
293% the list of supported formats. The attributes include the image format
294% tag, a method to read and/or write the format, whether the format
295% supports the saving of more than one frame to the same file or blob,
296% whether the format supports native in-memory I/O, and a brief
297% description of the format.
298%
299% The format of the RegisterGRAYImage method is:
300%
301% unsigned long RegisterGRAYImage(void)
302%
303*/
304ModuleExport unsigned long RegisterGRAYImage(void)
305{
306 MagickInfo
307 *entry;
308
309 entry=SetMagickInfo("GRAY");
310 entry->decoder=(DecodeImageHandler *) ReadGRAYImage;
311 entry->encoder=(EncodeImageHandler *) WriteGRAYImage;
312 entry->raw=MagickTrue;
313 entry->endian_support=MagickTrue;
314 entry->format_type=ExplicitFormatType;
315 entry->description=ConstantString("Raw gray samples");
316 entry->module=ConstantString("GRAY");
317 (void) RegisterMagickInfo(entry);
318 return(MagickImageCoderSignature);
319}
320
321/*
322%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323% %
324% %
325% %
326% U n r e g i s t e r G R A Y I m a g e %
327% %
328% %
329% %
330%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331%
332% UnregisterGRAYImage() removes format registrations made by the
333% GRAY module from the list of supported formats.
334%
335% The format of the UnregisterGRAYImage method is:
336%
337% UnregisterGRAYImage(void)
338%
339*/
340ModuleExport void UnregisterGRAYImage(void)
341{
342 (void) UnregisterMagickInfo("GRAY");
343}
344
345/*
346%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347% %
348% %
349% %
350% W r i t e G R A Y I m a g e %
351% %
352% %
353% %
354%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
355%
356% WriteGRAYImage() writes an image to a file as gray scale intensity
357% values.
358%
359% The format of the WriteGRAYImage method is:
360%
361% MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
362% Image *image)
363%
364% A description of each parameter follows.
365%
366% o image_info: the image info.
367%
368% o image: The image.
369%
370*/
371static MagickBooleanType WriteGRAYImage(const ImageInfo *image_info,
372 Image *image)
373{
374 long
375 y;
376
377 MagickBooleanType
378 status;
379
380 MagickOffsetType
381 scene;
382
383 QuantumInfo
384 *quantum_info;
385
386 QuantumType
387 quantum_type;
388
389 ssize_t
390 count;
391
392 size_t
393 length;
394
395 unsigned char
396 *pixels;
397
398 /*
399 Open output image file.
400 */
401 assert(image_info != (const ImageInfo *) NULL);
402 assert(image_info->signature == MagickSignature);
403 assert(image != (Image *) NULL);
404 assert(image->signature == MagickSignature);
405 if (image->debug != MagickFalse)
406 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
407 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
408 if (status == MagickFalse)
409 return(status);
410 scene=0;
411 do
412 {
413 /*
414 Write grayscale pixels.
415 */
416 if (image->colorspace != RGBColorspace)
417 (void) TransformImageColorspace(image,RGBColorspace);
418 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);
423 for (y=0; y < (long) image->rows; y++)
424 {
425 register const PixelPacket
426 *__restrict p;
427
428 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
429 if (p == (const PixelPacket *) NULL)
430 break;
431 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
432 quantum_type,pixels,&image->exception);
433 count=WriteBlob(image,length,pixels);
434 if (count != (ssize_t) length)
435 break;
436 if (image->previous == (Image *) NULL)
437 {
438 status=SetImageProgress(image,SaveImageTag,y,image->rows);
439 if (status == MagickFalse)
440 break;
441 }
442 }
443 quantum_info=DestroyQuantumInfo(quantum_info);
444 if (GetNextImageInList(image) == (Image *) NULL)
445 break;
446 image=SyncNextImageInList(image);
447 status=SetImageProgress(image,SaveImagesTag,scene++,
448 GetImageListLength(image));
449 if (status == MagickFalse)
450 break;
451 } while (image_info->adjoin != MagickFalse);
452 (void) CloseBlob(image);
453 return(MagickTrue);
454}