2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write RAW Image Format %
20 % Copyright 1999-2009 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 "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"
67 static MagickBooleanType
68 WriteRAWImage(const ImageInfo *,Image *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % R e a d R A W I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
85 % The format of the ReadRAWImage method is:
87 % Image *ReadRAWImage(const ImageInfo *image_info,ExceptionInfo *exception)
89 % A description of each parameter follows:
91 % o image_info: the image info.
93 % o exception: return any errors or warnings in this structure.
96 static Image *ReadRAWImage(const ImageInfo *image_info,
97 ExceptionInfo *exception)
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)
146 image=DestroyImageList(image);
147 return((Image *) NULL);
149 for (i=0; i < image->offset; i++)
150 if (ReadBlobByte(image) == EOF)
152 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
157 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
159 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
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)
174 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
175 for (y=0; y < (long) image->rows; y++)
177 count=ReadBlob(image,length,pixels);
178 if (count != (ssize_t) length)
188 Read pixels to virtual canvas image then push to image.
190 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
191 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
195 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
196 count=ReadBlob(image,length,pixels);
198 for (y=0; y < (long) image->extract_info.height; y++)
200 register const PixelPacket
209 if (count != (ssize_t) length)
211 ThrowFileException(exception,CorruptImageError,
212 "UnexpectedEndOfFile",image->filename);
215 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
216 if (q == (PixelPacket *) NULL)
218 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
219 quantum_type,pixels,exception);
220 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
222 if (((y-image->extract_info.y) >= 0) &&
223 ((y-image->extract_info.y) < (long) image->rows))
225 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
226 image->columns,1,exception);
227 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
229 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
231 for (x=0; x < (long) image->columns; x++)
239 if (SyncAuthenticPixels(image,exception) == MagickFalse)
242 if (image->previous == (Image *) NULL)
244 status=SetImageProgress(image,LoadImageTag,y,image->rows);
245 if (status == MagickFalse)
248 count=ReadBlob(image,length,pixels);
250 SetQuantumImageType(image,quantum_type);
252 Proceed to next image.
254 if (image_info->number_scenes != 0)
255 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
257 if (count == (ssize_t) length)
260 Allocate next image structure.
262 AcquireNextImage(image_info,image);
263 if (GetNextImageInList(image) == (Image *) NULL)
265 image=DestroyImageList(image);
266 return((Image *) NULL);
268 image=SyncNextImageInList(image);
269 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
271 if (status == MagickFalse)
275 } while (count == (ssize_t) length);
276 quantum_info=DestroyQuantumInfo(quantum_info);
277 InheritException(&image->exception,&canvas_image->exception);
278 canvas_image=DestroyImage(canvas_image);
279 (void) CloseBlob(image);
280 return(GetFirstImageInList(image));
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % R e g i s t e r R A W I m a g e %
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % RegisterRAWImage() adds attributes for the RAW image format to the list of
295 % supported formats. The attributes include the image format tag, a method to
296 % read and/or write the format, whether the format supports the saving of
297 % more than one frame to the same file or blob, whether the format supports
298 % native in-memory I/O, and a brief description of the format.
300 % The format of the RegisterRAWImage method is:
302 % unsigned long RegisterRAWImage(void)
305 ModuleExport unsigned long RegisterRAWImage(void)
310 entry=SetMagickInfo("R");
311 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
312 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
313 entry->raw=MagickTrue;
314 entry->endian_support=MagickTrue;
315 entry->format_type=ExplicitFormatType;
316 entry->description=ConstantString("Raw red samples");
317 entry->module=ConstantString("RAW");
318 (void) RegisterMagickInfo(entry);
319 entry=SetMagickInfo("C");
320 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
321 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
322 entry->raw=MagickTrue;
323 entry->endian_support=MagickTrue;
324 entry->format_type=ExplicitFormatType;
325 entry->description=ConstantString("Raw cyan samples");
326 entry->module=ConstantString("RAW");
327 (void) RegisterMagickInfo(entry);
328 entry=SetMagickInfo("G");
329 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
330 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
331 entry->raw=MagickTrue;
332 entry->endian_support=MagickTrue;
333 entry->format_type=ExplicitFormatType;
334 entry->description=ConstantString("Raw green samples");
335 entry->module=ConstantString("RAW");
336 (void) RegisterMagickInfo(entry);
337 entry=SetMagickInfo("M");
338 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
339 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
340 entry->raw=MagickTrue;
341 entry->endian_support=MagickTrue;
342 entry->format_type=ExplicitFormatType;
343 entry->description=ConstantString("Raw magenta samples");
344 entry->module=ConstantString("RAW");
345 (void) RegisterMagickInfo(entry);
346 entry=SetMagickInfo("B");
347 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
348 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
349 entry->raw=MagickTrue;
350 entry->endian_support=MagickTrue;
351 entry->description=ConstantString("Raw blue samples");
352 entry->format_type=ExplicitFormatType;
353 entry->module=ConstantString("RAW");
354 (void) RegisterMagickInfo(entry);
355 entry=SetMagickInfo("Y");
356 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
357 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
358 entry->raw=MagickTrue;
359 entry->endian_support=MagickTrue;
360 entry->format_type=ExplicitFormatType;
361 entry->description=ConstantString("Raw yellow samples");
362 entry->module=ConstantString("RAW");
363 (void) RegisterMagickInfo(entry);
364 entry=SetMagickInfo("A");
365 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
366 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
367 entry->raw=MagickTrue;
368 entry->endian_support=MagickTrue;
369 entry->description=ConstantString("Raw alpha samples");
370 entry->module=ConstantString("RAW");
371 (void) RegisterMagickInfo(entry);
372 entry=SetMagickInfo("O");
373 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
374 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
375 entry->raw=MagickTrue;
376 entry->endian_support=MagickTrue;
377 entry->format_type=ExplicitFormatType;
378 entry->description=ConstantString("Raw opacity samples");
379 entry->module=ConstantString("RAW");
380 (void) RegisterMagickInfo(entry);
381 entry=SetMagickInfo("K");
382 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
383 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
384 entry->raw=MagickTrue;
385 entry->endian_support=MagickTrue;
386 entry->format_type=ExplicitFormatType;
387 entry->description=ConstantString("Raw black samples");
388 entry->module=ConstantString("RAW");
389 (void) RegisterMagickInfo(entry);
390 return(MagickImageCoderSignature);
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 % U n r e g i s t e r R A W I m a g e %
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 % UnregisterRAWImage() removes format registrations made by the RAW module
405 % from the list of supported formats.
407 % The format of the UnregisterRAWImage method is:
409 % UnregisterRAWImage(void)
412 ModuleExport void UnregisterRAWImage(void)
414 (void) UnregisterMagickInfo("R");
415 (void) UnregisterMagickInfo("C");
416 (void) UnregisterMagickInfo("G");
417 (void) UnregisterMagickInfo("M");
418 (void) UnregisterMagickInfo("B");
419 (void) UnregisterMagickInfo("Y");
420 (void) UnregisterMagickInfo("A");
421 (void) UnregisterMagickInfo("O");
422 (void) UnregisterMagickInfo("K");
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430 % W r i t e R A W I m a g e %
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % WriteRAWImage() writes an image to a file as raw intensity values.
438 % The format of the WriteRAWImage method is:
440 % MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image)
442 % A description of each parameter follows.
444 % o image_info: the image info.
446 % o image: The image.
449 static MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image)
466 register const PixelPacket
479 Open output image file.
481 assert(image_info != (const ImageInfo *) NULL);
482 assert(image_info->signature == MagickSignature);
483 assert(image != (Image *) NULL);
484 assert(image->signature == MagickSignature);
485 if (image->debug != MagickFalse)
486 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
487 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
488 if (status == MagickFalse)
490 switch (*image->magick)
495 quantum_type=AlphaQuantum;
501 quantum_type=BlueQuantum;
507 quantum_type=CyanQuantum;
508 if (image->colorspace == CMYKColorspace)
510 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
515 quantum_type=GreenQuantum;
521 quantum_type=IndexQuantum;
527 quantum_type=BlackQuantum;
528 if (image->colorspace == CMYKColorspace)
530 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
535 quantum_type=MagentaQuantum;
536 if (image->colorspace == CMYKColorspace)
538 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
543 quantum_type=OpacityQuantum;
549 quantum_type=RedQuantum;
555 quantum_type=YellowQuantum;
556 if (image->colorspace == CMYKColorspace)
558 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
562 quantum_type=GrayQuantum;
570 Convert image to RAW raster pixels.
572 quantum_info=AcquireQuantumInfo(image_info,image);
573 if (quantum_info == (QuantumInfo *) NULL)
574 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
575 pixels=GetQuantumPixels(quantum_info);
576 for (y=0; y < (long) image->rows; y++)
578 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
579 if (p == (const PixelPacket *) NULL)
581 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
582 quantum_type,pixels,&image->exception);
583 count=WriteBlob(image,length,pixels);
584 if (count != (ssize_t) length)
586 if (image->previous == (Image *) NULL)
588 status=SetImageProgress(image,SaveImageTag,y,image->rows);
589 if (status == MagickFalse)
593 quantum_info=DestroyQuantumInfo(quantum_info);
594 if (GetNextImageInList(image) == (Image *) NULL)
596 image=SyncNextImageInList(image);
597 status=SetImageProgress(image,SaveImagesTag,scene++,
598 GetImageListLength(image));
599 if (status == MagickFalse)
601 } while (image_info->adjoin != MagickFalse);
602 (void) CloseBlob(image);