blob: 43cafa88d9749b4e5ee4109a5ee6dbe8e0524703 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% RRRR AAA W W %
7% R R A A W W %
8% RRRR AAAAA W W W %
9% R R A A WW WW %
10% R R A A W W %
11% %
12% %
13% Read/Write RAW Image Format %
14% %
15% Software Design %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristyf6ff9ea2016-12-05 09:53:35 -050020% Copyright 1999-2017 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% %
Cristyf19d4142017-04-24 11:34:30 -040026% https://www.imagemagick.org/script/license.php %
cristy3ed852e2009-09-05 21:47:34 +000027% %
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"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/colorspace.h"
47#include "MagickCore/constitute.h"
48#include "MagickCore/exception.h"
49#include "MagickCore/exception-private.h"
50#include "MagickCore/image.h"
51#include "MagickCore/image-private.h"
52#include "MagickCore/list.h"
53#include "MagickCore/magick.h"
54#include "MagickCore/memory_.h"
55#include "MagickCore/monitor.h"
56#include "MagickCore/monitor-private.h"
57#include "MagickCore/pixel-accessor.h"
58#include "MagickCore/quantum-private.h"
59#include "MagickCore/quantum-private.h"
60#include "MagickCore/static.h"
61#include "MagickCore/statistic.h"
62#include "MagickCore/string_.h"
63#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000064
65/*
66 Forward declarations.
67*/
68static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000069 WriteRAWImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000070
71/*
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73% %
74% %
75% %
76% R e a d R A W I m a g e %
77% %
78% %
79% %
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81%
82% ReadRAWImage() reads an image of raw samples and returns it. It allocates
83% the memory necessary for the new Image structure and returns a pointer to
84% the new image.
85%
86% The format of the ReadRAWImage method is:
87%
88% Image *ReadRAWImage(const ImageInfo *image_info,ExceptionInfo *exception)
89%
90% A description of each parameter follows:
91%
92% o image_info: the image info.
93%
94% o exception: return any errors or warnings in this structure.
95%
96*/
cristy0f8bcf32012-01-11 01:27:20 +000097static Image *ReadRAWImage(const ImageInfo *image_info,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +000098{
cristyb3f97ae2015-05-18 12:29:32 +000099 const unsigned char
cristybd797f12015-01-24 20:42:32 +0000100 *pixels;
101
cristy3ed852e2009-09-05 21:47:34 +0000102 Image
103 *canvas_image,
104 *image;
105
cristy3ed852e2009-09-05 21:47:34 +0000106 MagickBooleanType
107 status;
108
cristy9af8a2a2009-10-02 13:29:27 +0000109 MagickOffsetType
110 scene;
111
cristy3ed852e2009-09-05 21:47:34 +0000112 QuantumInfo
113 *quantum_info;
114
115 QuantumType
116 quantum_type;
117
cristyb20901d2010-09-16 23:13:55 +0000118 size_t
119 length;
cristy3ed852e2009-09-05 21:47:34 +0000120
121 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000122 count,
123 y;
cristy3ed852e2009-09-05 21:47:34 +0000124
cristy3ed852e2009-09-05 21:47:34 +0000125 /*
126 Open image file.
127 */
128 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000129 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000130 if (image_info->debug != MagickFalse)
131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
132 image_info->filename);
133 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000134 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000135 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000136 if ((image->columns == 0) || (image->rows == 0))
137 ThrowReaderException(OptionError,"MustSpecifyImageSize");
Cristyf420dab2017-09-13 10:49:56 -0400138 status=SetImageExtent(image,image->columns,image->rows,exception);
139 if (status == MagickFalse)
140 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000141 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
142 if (status == MagickFalse)
143 {
144 image=DestroyImageList(image);
145 return((Image *) NULL);
146 }
Cristyf420dab2017-09-13 10:49:56 -0400147 if (DiscardBlobBytes(image,(MagickSizeType) image->offset) == MagickFalse)
cristyd4297022010-09-16 22:59:09 +0000148 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
149 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000150 /*
151 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
152 */
153 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
154 exception);
cristy387430f2012-02-07 13:09:46 +0000155 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod,
156 exception);
cristy3ed852e2009-09-05 21:47:34 +0000157 quantum_type=GrayQuantum;
cristy5f766ef2014-12-14 21:12:47 +0000158 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
cristy3ed852e2009-09-05 21:47:34 +0000159 if (quantum_info == (QuantumInfo *) NULL)
160 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristyb3f97ae2015-05-18 12:29:32 +0000161 pixels=(const unsigned char *) NULL;
cristy3ed852e2009-09-05 21:47:34 +0000162 if (image_info->number_scenes != 0)
163 while (image->scene < image_info->scene)
164 {
165 /*
166 Skip to next image.
167 */
168 image->scene++;
169 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
cristybb503372010-05-27 20:51:26 +0000170 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000171 {
cristyb3f97ae2015-05-18 12:29:32 +0000172 pixels=(const unsigned char *) ReadBlobStream(image,length,
173 GetQuantumPixels(quantum_info),&count);
cristy3ed852e2009-09-05 21:47:34 +0000174 if (count != (ssize_t) length)
175 break;
176 }
177 }
cristy9af8a2a2009-10-02 13:29:27 +0000178 scene=0;
179 count=0;
180 length=0;
Cristy7a610d42017-09-14 07:18:36 -0400181 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000182 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;
cristyacabb842014-12-14 23:36:33 +0000190 status=SetImageExtent(image,image->columns,image->rows,exception);
191 if (status == MagickFalse)
Cristyf420dab2017-09-13 10:49:56 -0400192 break;
cristy9af8a2a2009-10-02 13:29:27 +0000193 if (scene == 0)
194 {
195 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
cristyb3f97ae2015-05-18 12:29:32 +0000196 pixels=(const unsigned char *) ReadBlobStream(image,length,
197 GetQuantumPixels(quantum_info),&count);
cristy9af8a2a2009-10-02 13:29:27 +0000198 }
cristybb503372010-05-27 20:51:26 +0000199 for (y=0; y < (ssize_t) image->extract_info.height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000200 {
cristy4c08aed2011-07-01 19:47:50 +0000201 register const Quantum
dirk05d2ff72015-11-18 23:13:43 +0100202 *magick_restrict p;
cristy9af8a2a2009-10-02 13:29:27 +0000203
cristy4c08aed2011-07-01 19:47:50 +0000204 register Quantum
dirk05d2ff72015-11-18 23:13:43 +0100205 *magick_restrict q;
cristy9af8a2a2009-10-02 13:29:27 +0000206
cristyc6da28e2011-04-28 01:41:35 +0000207 register ssize_t
208 x;
209
cristy9af8a2a2009-10-02 13:29:27 +0000210 if (count != (ssize_t) length)
211 {
Cristy7a610d42017-09-14 07:18:36 -0400212 status=MagickFalse;
cristy128e20d2012-01-11 01:24:58 +0000213 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
214 image->filename);
cristy9af8a2a2009-10-02 13:29:27 +0000215 break;
216 }
cristy3ed852e2009-09-05 21:47:34 +0000217 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000218 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000219 break;
220 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
221 quantum_type,pixels,exception);
222 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
223 break;
cristy3ed852e2009-09-05 21:47:34 +0000224 if (((y-image->extract_info.y) >= 0) &&
cristybb503372010-05-27 20:51:26 +0000225 ((y-image->extract_info.y) < (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000226 {
227 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
cristy9af8a2a2009-10-02 13:29:27 +0000228 image->columns,1,exception);
229 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
230 1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000231 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
cristy3ed852e2009-09-05 21:47:34 +0000232 break;
cristybb503372010-05-27 20:51:26 +0000233 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000234 {
cristy4c08aed2011-07-01 19:47:50 +0000235 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
236 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
237 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
cristyed231572011-07-14 02:18:59 +0000238 p+=GetPixelChannels(canvas_image);
239 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000240 }
241 if (SyncAuthenticPixels(image,exception) == MagickFalse)
242 break;
243 }
244 if (image->previous == (Image *) NULL)
245 {
cristycee97112010-05-28 00:44:52 +0000246 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000247 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000248 if (status == MagickFalse)
249 break;
250 }
cristyb3f97ae2015-05-18 12:29:32 +0000251 pixels=(const unsigned char *) ReadBlobStream(image,length,
252 GetQuantumPixels(quantum_info),&count);
cristy3ed852e2009-09-05 21:47:34 +0000253 }
254 SetQuantumImageType(image,quantum_type);
255 /*
256 Proceed to next image.
257 */
258 if (image_info->number_scenes != 0)
259 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
260 break;
261 if (count == (ssize_t) length)
262 {
263 /*
264 Allocate next image structure.
265 */
cristy9950d572011-10-01 18:22:35 +0000266 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000267 if (GetNextImageInList(image) == (Image *) NULL)
268 {
269 image=DestroyImageList(image);
270 return((Image *) NULL);
271 }
272 image=SyncNextImageInList(image);
273 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
274 GetBlobSize(image));
275 if (status == MagickFalse)
276 break;
277 }
cristy9af8a2a2009-10-02 13:29:27 +0000278 scene++;
cristy3ed852e2009-09-05 21:47:34 +0000279 } while (count == (ssize_t) length);
280 quantum_info=DestroyQuantumInfo(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +0000281 canvas_image=DestroyImage(canvas_image);
282 (void) CloseBlob(image);
Cristy7a610d42017-09-14 07:18:36 -0400283 if (status == MagickFalse)
284 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000285 return(GetFirstImageInList(image));
286}
287
288/*
289%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290% %
291% %
292% %
293% R e g i s t e r R A W I m a g e %
294% %
295% %
296% %
297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298%
299% RegisterRAWImage() adds attributes for the RAW image format to the list of
300% supported formats. The attributes include the image format tag, a method to
301% read and/or write the format, whether the format supports the saving of
302% more than one frame to the same file or blob, whether the format supports
303% native in-memory I/O, and a brief description of the format.
304%
305% The format of the RegisterRAWImage method is:
306%
cristybb503372010-05-27 20:51:26 +0000307% size_t RegisterRAWImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000308%
309*/
cristybb503372010-05-27 20:51:26 +0000310ModuleExport size_t RegisterRAWImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000311{
312 MagickInfo
313 *entry;
314
dirk06b627a2015-04-06 18:59:17 +0000315 entry=AcquireMagickInfo("RAW","R","Raw red samples");
cristy3ed852e2009-09-05 21:47:34 +0000316 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
317 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000318 entry->flags|=CoderRawSupportFlag;
319 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000320 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000321 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000322 entry=AcquireMagickInfo("RAW","C","Raw cyan samples");
cristy3ed852e2009-09-05 21:47:34 +0000323 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
324 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000325 entry->flags|=CoderRawSupportFlag;
326 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000327 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000328 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000329 entry=AcquireMagickInfo("RAW","G","Raw green samples");
cristy3ed852e2009-09-05 21:47:34 +0000330 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
331 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000332 entry->flags|=CoderRawSupportFlag;
333 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000334 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000335 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000336 entry=AcquireMagickInfo("RAW","M","Raw magenta samples");
cristy3ed852e2009-09-05 21:47:34 +0000337 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
338 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000339 entry->flags|=CoderRawSupportFlag;
340 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000341 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000342 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000343 entry=AcquireMagickInfo("RAW","B","Raw blue samples");
cristy3ed852e2009-09-05 21:47:34 +0000344 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
345 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000346 entry->flags|=CoderRawSupportFlag;
347 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000348 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000349 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000350 entry=AcquireMagickInfo("RAW","Y","Raw yellow samples");
cristy3ed852e2009-09-05 21:47:34 +0000351 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
352 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000353 entry->flags|=CoderRawSupportFlag;
354 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000355 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000356 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000357 entry=AcquireMagickInfo("RAW","A","Raw alpha samples");
cristy3ed852e2009-09-05 21:47:34 +0000358 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
359 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000360 entry->flags|=CoderRawSupportFlag;
361 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000362 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000363 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000364 entry=AcquireMagickInfo("RAW","O","Raw opacity samples");
cristy3ed852e2009-09-05 21:47:34 +0000365 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
366 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000367 entry->flags|=CoderRawSupportFlag;
368 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000369 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000370 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000371 entry=AcquireMagickInfo("RAW","K","Raw black samples");
cristy3ed852e2009-09-05 21:47:34 +0000372 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
373 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
dirk08e9a112015-02-22 01:51:41 +0000374 entry->flags|=CoderRawSupportFlag;
375 entry->flags|=CoderEndianSupportFlag;
cristy32ccaaa2015-06-14 19:13:46 +0000376 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000377 (void) RegisterMagickInfo(entry);
378 return(MagickImageCoderSignature);
379}
380
381/*
382%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383% %
384% %
385% %
386% U n r e g i s t e r R A W I m a g e %
387% %
388% %
389% %
390%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391%
392% UnregisterRAWImage() removes format registrations made by the RAW module
393% from the list of supported formats.
394%
395% The format of the UnregisterRAWImage method is:
396%
397% UnregisterRAWImage(void)
398%
399*/
400ModuleExport void UnregisterRAWImage(void)
401{
402 (void) UnregisterMagickInfo("R");
403 (void) UnregisterMagickInfo("C");
404 (void) UnregisterMagickInfo("G");
405 (void) UnregisterMagickInfo("M");
406 (void) UnregisterMagickInfo("B");
407 (void) UnregisterMagickInfo("Y");
408 (void) UnregisterMagickInfo("A");
409 (void) UnregisterMagickInfo("O");
410 (void) UnregisterMagickInfo("K");
411}
412
413/*
414%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415% %
416% %
417% %
418% W r i t e R A W I m a g e %
419% %
420% %
421% %
422%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423%
424% WriteRAWImage() writes an image to a file as raw intensity values.
425%
426% The format of the WriteRAWImage method is:
427%
cristy3a37efd2011-08-28 20:31:03 +0000428% MagickBooleanType WriteRAWImage(const ImageInfo *image_info,
429% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000430%
431% A description of each parameter follows.
432%
433% o image_info: the image info.
434%
435% o image: The image.
436%
cristy3a37efd2011-08-28 20:31:03 +0000437% o exception: return any errors or warnings in this structure.
438%
cristy3ed852e2009-09-05 21:47:34 +0000439*/
cristy3a37efd2011-08-28 20:31:03 +0000440static MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image,
441 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000442{
cristy3ed852e2009-09-05 21:47:34 +0000443 MagickOffsetType
444 scene;
445
446 QuantumInfo
447 *quantum_info;
448
449 QuantumType
450 quantum_type;
451
452 MagickBooleanType
453 status;
454
cristy4c08aed2011-07-01 19:47:50 +0000455 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000456 *p;
457
cristy3ed852e2009-09-05 21:47:34 +0000458 size_t
459 length;
460
cristyc6da28e2011-04-28 01:41:35 +0000461 ssize_t
462 count,
463 y;
464
cristy3ed852e2009-09-05 21:47:34 +0000465 unsigned char
466 *pixels;
467
468 /*
469 Open output image file.
470 */
471 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000472 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000473 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000474 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000475 if (image->debug != MagickFalse)
476 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000477 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000478 assert(exception->signature == MagickCoreSignature);
cristy3a37efd2011-08-28 20:31:03 +0000479 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000480 if (status == MagickFalse)
481 return(status);
482 switch (*image->magick)
483 {
484 case 'A':
485 case 'a':
486 {
487 quantum_type=AlphaQuantum;
488 break;
489 }
490 case 'B':
491 case 'b':
492 {
493 quantum_type=BlueQuantum;
494 break;
495 }
496 case 'C':
497 case 'c':
498 {
499 quantum_type=CyanQuantum;
500 if (image->colorspace == CMYKColorspace)
501 break;
502 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
503 }
504 case 'g':
505 case 'G':
506 {
507 quantum_type=GreenQuantum;
508 break;
509 }
510 case 'I':
511 case 'i':
512 {
513 quantum_type=IndexQuantum;
514 break;
515 }
516 case 'K':
517 case 'k':
518 {
519 quantum_type=BlackQuantum;
520 if (image->colorspace == CMYKColorspace)
521 break;
522 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
523 }
524 case 'M':
525 case 'm':
526 {
527 quantum_type=MagentaQuantum;
528 if (image->colorspace == CMYKColorspace)
529 break;
530 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
531 }
532 case 'o':
533 case 'O':
534 {
535 quantum_type=OpacityQuantum;
536 break;
537 }
538 case 'R':
539 case 'r':
540 {
541 quantum_type=RedQuantum;
542 break;
543 }
544 case 'Y':
545 case 'y':
546 {
547 quantum_type=YellowQuantum;
548 if (image->colorspace == CMYKColorspace)
549 break;
550 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
551 }
552 default:
553 {
554 quantum_type=GrayQuantum;
555 break;
556 }
557 }
558 scene=0;
559 do
560 {
561 /*
562 Convert image to RAW raster pixels.
563 */
cristy5f766ef2014-12-14 21:12:47 +0000564 quantum_info=AcquireQuantumInfo(image_info,image);
cristy3ed852e2009-09-05 21:47:34 +0000565 if (quantum_info == (QuantumInfo *) NULL)
566 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristyb3f97ae2015-05-18 12:29:32 +0000567 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +0000568 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000569 {
cristy3a37efd2011-08-28 20:31:03 +0000570 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000571 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000572 break;
cristy4c08aed2011-07-01 19:47:50 +0000573 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +0000574 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +0000575 count=WriteBlob(image,length,pixels);
576 if (count != (ssize_t) length)
577 break;
578 if (image->previous == (Image *) NULL)
579 {
cristycee97112010-05-28 00:44:52 +0000580 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy566c5a82010-06-06 15:37:51 +0000581 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000582 if (status == MagickFalse)
583 break;
584 }
585 }
586 quantum_info=DestroyQuantumInfo(quantum_info);
587 if (GetNextImageInList(image) == (Image *) NULL)
588 break;
589 image=SyncNextImageInList(image);
590 status=SetImageProgress(image,SaveImagesTag,scene++,
591 GetImageListLength(image));
592 if (status == MagickFalse)
593 break;
594 } while (image_info->adjoin != MagickFalse);
595 (void) CloseBlob(image);
596 return(MagickTrue);
597}