blob: 36c1359ab5f01884573eac12a446dd4c2dc9df3a [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 %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 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*/
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/quantum-private.h"
58#include "magick/quantum-private.h"
59#include "magick/static.h"
60#include "magick/statistic.h"
61#include "magick/string_.h"
62#include "magick/module.h"
63
64/*
65 Forward declarations.
66*/
67static MagickBooleanType
68 WriteRAWImage(const ImageInfo *,Image *);
69
70/*
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72% %
73% %
74% %
75% R e a d R A W I m a g e %
76% %
77% %
78% %
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80%
81% ReadRAWImage() reads an image of raw samples and returns it. It allocates
82% the memory necessary for the new Image structure and returns a pointer to
83% the new image.
84%
85% The format of the ReadRAWImage method is:
86%
87% Image *ReadRAWImage(const ImageInfo *image_info,ExceptionInfo *exception)
88%
89% A description of each parameter follows:
90%
91% o image_info: the image info.
92%
93% o exception: return any errors or warnings in this structure.
94%
95*/
96static Image *ReadRAWImage(const ImageInfo *image_info,
97 ExceptionInfo *exception)
98{
99 Image
100 *canvas_image,
101 *image;
102
cristybb503372010-05-27 20:51:26 +0000103 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000104 y;
105
106 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
cristybb503372010-05-27 20:51:26 +0000118 register ssize_t
cristy9af8a2a2009-10-02 13:29:27 +0000119 i;
cristy3ed852e2009-09-05 21:47:34 +0000120
121 ssize_t
122 count;
123
124 size_t
125 length;
126
127 unsigned char
128 *pixels;
129
130 /*
131 Open image file.
132 */
133 assert(image_info != (const ImageInfo *) NULL);
134 assert(image_info->signature == MagickSignature);
135 if (image_info->debug != MagickFalse)
136 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
137 image_info->filename);
138 assert(exception != (ExceptionInfo *) NULL);
139 assert(exception->signature == MagickSignature);
140 image=AcquireImage(image_info);
141 if ((image->columns == 0) || (image->rows == 0))
142 ThrowReaderException(OptionError,"MustSpecifyImageSize");
143 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
144 if (status == MagickFalse)
145 {
146 image=DestroyImageList(image);
147 return((Image *) NULL);
148 }
149 for (i=0; i < image->offset; i++)
150 if (ReadBlobByte(image) == EOF)
151 {
152 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
153 image->filename);
154 break;
155 }
156 /*
157 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
158 */
159 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
160 exception);
161 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod);
162 quantum_type=GrayQuantum;
163 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
164 if (quantum_info == (QuantumInfo *) NULL)
165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
166 pixels=GetQuantumPixels(quantum_info);
167 if (image_info->number_scenes != 0)
168 while (image->scene < image_info->scene)
169 {
170 /*
171 Skip to next image.
172 */
173 image->scene++;
174 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
cristybb503372010-05-27 20:51:26 +0000175 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000176 {
177 count=ReadBlob(image,length,pixels);
178 if (count != (ssize_t) length)
179 break;
180 }
181 }
cristy9af8a2a2009-10-02 13:29:27 +0000182 scene=0;
183 count=0;
184 length=0;
cristy3ed852e2009-09-05 21:47:34 +0000185 do
186 {
187 /*
188 Read pixels to virtual canvas image then push to image.
189 */
190 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
191 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
192 break;
cristy9af8a2a2009-10-02 13:29:27 +0000193 if (scene == 0)
194 {
195 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
196 count=ReadBlob(image,length,pixels);
197 }
cristybb503372010-05-27 20:51:26 +0000198 for (y=0; y < (ssize_t) image->extract_info.height; y++)
cristy3ed852e2009-09-05 21:47:34 +0000199 {
cristy9af8a2a2009-10-02 13:29:27 +0000200 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000201 *restrict p;
cristy9af8a2a2009-10-02 13:29:27 +0000202
cristybb503372010-05-27 20:51:26 +0000203 register ssize_t
cristy9af8a2a2009-10-02 13:29:27 +0000204 x;
205
206 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +0000207 *restrict q;
cristy9af8a2a2009-10-02 13:29:27 +0000208
209 if (count != (ssize_t) length)
210 {
211 ThrowFileException(exception,CorruptImageError,
212 "UnexpectedEndOfFile",image->filename);
213 break;
214 }
cristy3ed852e2009-09-05 21:47:34 +0000215 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
216 if (q == (PixelPacket *) NULL)
217 break;
218 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
219 quantum_type,pixels,exception);
220 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
221 break;
cristy3ed852e2009-09-05 21:47:34 +0000222 if (((y-image->extract_info.y) >= 0) &&
cristybb503372010-05-27 20:51:26 +0000223 ((y-image->extract_info.y) < (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +0000224 {
225 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
cristy9af8a2a2009-10-02 13:29:27 +0000226 image->columns,1,exception);
227 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
228 1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000229 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
230 break;
cristybb503372010-05-27 20:51:26 +0000231 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000232 {
cristyce70c172010-01-07 17:15:30 +0000233 SetRedPixelComponent(q,GetRedPixelComponent(p));
234 SetGreenPixelComponent(q,GetGreenPixelComponent(p));
235 SetBluePixelComponent(q,GetBluePixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000236 p++;
237 q++;
238 }
239 if (SyncAuthenticPixels(image,exception) == MagickFalse)
240 break;
241 }
242 if (image->previous == (Image *) NULL)
243 {
cristycee97112010-05-28 00:44:52 +0000244 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
245 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000246 if (status == MagickFalse)
247 break;
248 }
cristy9af8a2a2009-10-02 13:29:27 +0000249 count=ReadBlob(image,length,pixels);
cristy3ed852e2009-09-05 21:47:34 +0000250 }
251 SetQuantumImageType(image,quantum_type);
252 /*
253 Proceed to next image.
254 */
255 if (image_info->number_scenes != 0)
256 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
257 break;
258 if (count == (ssize_t) length)
259 {
260 /*
261 Allocate next image structure.
262 */
263 AcquireNextImage(image_info,image);
264 if (GetNextImageInList(image) == (Image *) NULL)
265 {
266 image=DestroyImageList(image);
267 return((Image *) NULL);
268 }
269 image=SyncNextImageInList(image);
270 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
271 GetBlobSize(image));
272 if (status == MagickFalse)
273 break;
274 }
cristy9af8a2a2009-10-02 13:29:27 +0000275 scene++;
cristy3ed852e2009-09-05 21:47:34 +0000276 } while (count == (ssize_t) length);
277 quantum_info=DestroyQuantumInfo(quantum_info);
cristy01a3f332009-10-27 14:17:37 +0000278 InheritException(&image->exception,&canvas_image->exception);
cristy3ed852e2009-09-05 21:47:34 +0000279 canvas_image=DestroyImage(canvas_image);
280 (void) CloseBlob(image);
281 return(GetFirstImageInList(image));
282}
283
284/*
285%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286% %
287% %
288% %
289% R e g i s t e r R A W I m a g e %
290% %
291% %
292% %
293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294%
295% RegisterRAWImage() adds attributes for the RAW image format to the list of
296% supported formats. The attributes include the image format tag, a method to
297% read and/or write the format, whether the format supports the saving of
298% more than one frame to the same file or blob, whether the format supports
299% native in-memory I/O, and a brief description of the format.
300%
301% The format of the RegisterRAWImage method is:
302%
cristybb503372010-05-27 20:51:26 +0000303% size_t RegisterRAWImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000304%
305*/
cristybb503372010-05-27 20:51:26 +0000306ModuleExport size_t RegisterRAWImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000307{
308 MagickInfo
309 *entry;
310
311 entry=SetMagickInfo("R");
312 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
313 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
314 entry->raw=MagickTrue;
315 entry->endian_support=MagickTrue;
316 entry->format_type=ExplicitFormatType;
317 entry->description=ConstantString("Raw red samples");
318 entry->module=ConstantString("RAW");
319 (void) RegisterMagickInfo(entry);
320 entry=SetMagickInfo("C");
321 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
322 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
323 entry->raw=MagickTrue;
324 entry->endian_support=MagickTrue;
325 entry->format_type=ExplicitFormatType;
326 entry->description=ConstantString("Raw cyan samples");
327 entry->module=ConstantString("RAW");
328 (void) RegisterMagickInfo(entry);
329 entry=SetMagickInfo("G");
330 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
331 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
332 entry->raw=MagickTrue;
333 entry->endian_support=MagickTrue;
334 entry->format_type=ExplicitFormatType;
335 entry->description=ConstantString("Raw green samples");
336 entry->module=ConstantString("RAW");
337 (void) RegisterMagickInfo(entry);
338 entry=SetMagickInfo("M");
339 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
340 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
341 entry->raw=MagickTrue;
342 entry->endian_support=MagickTrue;
343 entry->format_type=ExplicitFormatType;
344 entry->description=ConstantString("Raw magenta samples");
345 entry->module=ConstantString("RAW");
346 (void) RegisterMagickInfo(entry);
347 entry=SetMagickInfo("B");
348 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
349 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
350 entry->raw=MagickTrue;
351 entry->endian_support=MagickTrue;
352 entry->description=ConstantString("Raw blue samples");
353 entry->format_type=ExplicitFormatType;
354 entry->module=ConstantString("RAW");
355 (void) RegisterMagickInfo(entry);
356 entry=SetMagickInfo("Y");
357 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
358 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
359 entry->raw=MagickTrue;
360 entry->endian_support=MagickTrue;
361 entry->format_type=ExplicitFormatType;
362 entry->description=ConstantString("Raw yellow samples");
363 entry->module=ConstantString("RAW");
364 (void) RegisterMagickInfo(entry);
365 entry=SetMagickInfo("A");
366 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
367 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
368 entry->raw=MagickTrue;
369 entry->endian_support=MagickTrue;
370 entry->description=ConstantString("Raw alpha samples");
371 entry->module=ConstantString("RAW");
372 (void) RegisterMagickInfo(entry);
373 entry=SetMagickInfo("O");
374 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
375 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
376 entry->raw=MagickTrue;
377 entry->endian_support=MagickTrue;
378 entry->format_type=ExplicitFormatType;
379 entry->description=ConstantString("Raw opacity samples");
380 entry->module=ConstantString("RAW");
381 (void) RegisterMagickInfo(entry);
382 entry=SetMagickInfo("K");
383 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
384 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
385 entry->raw=MagickTrue;
386 entry->endian_support=MagickTrue;
387 entry->format_type=ExplicitFormatType;
388 entry->description=ConstantString("Raw black samples");
389 entry->module=ConstantString("RAW");
390 (void) RegisterMagickInfo(entry);
391 return(MagickImageCoderSignature);
392}
393
394/*
395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396% %
397% %
398% %
399% U n r e g i s t e r R A W I m a g e %
400% %
401% %
402% %
403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404%
405% UnregisterRAWImage() removes format registrations made by the RAW module
406% from the list of supported formats.
407%
408% The format of the UnregisterRAWImage method is:
409%
410% UnregisterRAWImage(void)
411%
412*/
413ModuleExport void UnregisterRAWImage(void)
414{
415 (void) UnregisterMagickInfo("R");
416 (void) UnregisterMagickInfo("C");
417 (void) UnregisterMagickInfo("G");
418 (void) UnregisterMagickInfo("M");
419 (void) UnregisterMagickInfo("B");
420 (void) UnregisterMagickInfo("Y");
421 (void) UnregisterMagickInfo("A");
422 (void) UnregisterMagickInfo("O");
423 (void) UnregisterMagickInfo("K");
424}
425
426/*
427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428% %
429% %
430% %
431% W r i t e R A W I m a g e %
432% %
433% %
434% %
435%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436%
437% WriteRAWImage() writes an image to a file as raw intensity values.
438%
439% The format of the WriteRAWImage method is:
440%
441% MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image)
442%
443% A description of each parameter follows.
444%
445% o image_info: the image info.
446%
447% o image: The image.
448%
449*/
450static MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image)
451{
cristybb503372010-05-27 20:51:26 +0000452 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000453 y;
454
455 MagickOffsetType
456 scene;
457
458 QuantumInfo
459 *quantum_info;
460
461 QuantumType
462 quantum_type;
463
464 MagickBooleanType
465 status;
466
467 register const PixelPacket
468 *p;
469
470 ssize_t
471 count;
472
473 size_t
474 length;
475
476 unsigned char
477 *pixels;
478
479 /*
480 Open output image file.
481 */
482 assert(image_info != (const ImageInfo *) NULL);
483 assert(image_info->signature == MagickSignature);
484 assert(image != (Image *) NULL);
485 assert(image->signature == MagickSignature);
486 if (image->debug != MagickFalse)
487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
488 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
489 if (status == MagickFalse)
490 return(status);
491 switch (*image->magick)
492 {
493 case 'A':
494 case 'a':
495 {
496 quantum_type=AlphaQuantum;
497 break;
498 }
499 case 'B':
500 case 'b':
501 {
502 quantum_type=BlueQuantum;
503 break;
504 }
505 case 'C':
506 case 'c':
507 {
508 quantum_type=CyanQuantum;
509 if (image->colorspace == CMYKColorspace)
510 break;
511 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
512 }
513 case 'g':
514 case 'G':
515 {
516 quantum_type=GreenQuantum;
517 break;
518 }
519 case 'I':
520 case 'i':
521 {
522 quantum_type=IndexQuantum;
523 break;
524 }
525 case 'K':
526 case 'k':
527 {
528 quantum_type=BlackQuantum;
529 if (image->colorspace == CMYKColorspace)
530 break;
531 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
532 }
533 case 'M':
534 case 'm':
535 {
536 quantum_type=MagentaQuantum;
537 if (image->colorspace == CMYKColorspace)
538 break;
539 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
540 }
541 case 'o':
542 case 'O':
543 {
544 quantum_type=OpacityQuantum;
545 break;
546 }
547 case 'R':
548 case 'r':
549 {
550 quantum_type=RedQuantum;
551 break;
552 }
553 case 'Y':
554 case 'y':
555 {
556 quantum_type=YellowQuantum;
557 if (image->colorspace == CMYKColorspace)
558 break;
559 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
560 }
561 default:
562 {
563 quantum_type=GrayQuantum;
564 break;
565 }
566 }
567 scene=0;
568 do
569 {
570 /*
571 Convert image to RAW raster pixels.
572 */
573 quantum_info=AcquireQuantumInfo(image_info,image);
574 if (quantum_info == (QuantumInfo *) NULL)
575 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
576 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +0000577 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000578 {
579 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
580 if (p == (const PixelPacket *) NULL)
581 break;
582 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
583 quantum_type,pixels,&image->exception);
584 count=WriteBlob(image,length,pixels);
585 if (count != (ssize_t) length)
586 break;
587 if (image->previous == (Image *) NULL)
588 {
cristycee97112010-05-28 00:44:52 +0000589 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristy566c5a82010-06-06 15:37:51 +0000590 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000591 if (status == MagickFalse)
592 break;
593 }
594 }
595 quantum_info=DestroyQuantumInfo(quantum_info);
596 if (GetNextImageInList(image) == (Image *) NULL)
597 break;
598 image=SyncNextImageInList(image);
599 status=SetImageProgress(image,SaveImagesTag,scene++,
600 GetImageListLength(image));
601 if (status == MagickFalse)
602 break;
603 } while (image_info->adjoin != MagickFalse);
604 (void) CloseBlob(image);
605 return(MagickTrue);
606}