2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write RAW Image Format %
20 % Copyright 1999-2011 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)
130 assert(image_info != (const ImageInfo *) NULL);
131 assert(image_info->signature == MagickSignature);
132 if (image_info->debug != MagickFalse)
133 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
134 image_info->filename);
135 assert(exception != (ExceptionInfo *) NULL);
136 assert(exception->signature == MagickSignature);
137 image=AcquireImage(image_info);
138 if ((image->columns == 0) || (image->rows == 0))
139 ThrowReaderException(OptionError,"MustSpecifyImageSize");
140 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
141 if (status == MagickFalse)
143 image=DestroyImageList(image);
144 return((Image *) NULL);
146 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
147 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
150 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
152 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
154 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod);
155 quantum_type=GrayQuantum;
156 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
157 if (quantum_info == (QuantumInfo *) NULL)
158 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
159 pixels=GetQuantumPixels(quantum_info);
160 if (image_info->number_scenes != 0)
161 while (image->scene < image_info->scene)
167 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
168 for (y=0; y < (ssize_t) image->rows; y++)
170 count=ReadBlob(image,length,pixels);
171 if (count != (ssize_t) length)
181 Read pixels to virtual canvas image then push to image.
183 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
184 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
188 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
189 count=ReadBlob(image,length,pixels);
191 for (y=0; y < (ssize_t) image->extract_info.height; y++)
193 register const PixelPacket
202 if (count != (ssize_t) length)
204 ThrowFileException(exception,CorruptImageError,
205 "UnexpectedEndOfFile",image->filename);
208 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
209 if (q == (PixelPacket *) NULL)
211 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
212 quantum_type,pixels,exception);
213 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
215 if (((y-image->extract_info.y) >= 0) &&
216 ((y-image->extract_info.y) < (ssize_t) image->rows))
218 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
219 image->columns,1,exception);
220 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
222 if ((p == (const PixelPacket *) NULL) || (q == (PixelPacket *) NULL))
224 for (x=0; x < (ssize_t) image->columns; x++)
226 SetRedPixelComponent(q,GetRedPixelComponent(p));
227 SetGreenPixelComponent(q,GetGreenPixelComponent(p));
228 SetBluePixelComponent(q,GetBluePixelComponent(p));
232 if (SyncAuthenticPixels(image,exception) == MagickFalse)
235 if (image->previous == (Image *) NULL)
237 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
239 if (status == MagickFalse)
242 count=ReadBlob(image,length,pixels);
244 SetQuantumImageType(image,quantum_type);
246 Proceed to next image.
248 if (image_info->number_scenes != 0)
249 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
251 if (count == (ssize_t) length)
254 Allocate next image structure.
256 AcquireNextImage(image_info,image);
257 if (GetNextImageInList(image) == (Image *) NULL)
259 image=DestroyImageList(image);
260 return((Image *) NULL);
262 image=SyncNextImageInList(image);
263 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
265 if (status == MagickFalse)
269 } while (count == (ssize_t) length);
270 quantum_info=DestroyQuantumInfo(quantum_info);
271 InheritException(&image->exception,&canvas_image->exception);
272 canvas_image=DestroyImage(canvas_image);
273 (void) CloseBlob(image);
274 return(GetFirstImageInList(image));
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % R e g i s t e r R A W I m a g e %
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288 % RegisterRAWImage() adds attributes for the RAW image format to the list of
289 % supported formats. The attributes include the image format tag, a method to
290 % read and/or write the format, whether the format supports the saving of
291 % more than one frame to the same file or blob, whether the format supports
292 % native in-memory I/O, and a brief description of the format.
294 % The format of the RegisterRAWImage method is:
296 % size_t RegisterRAWImage(void)
299 ModuleExport size_t RegisterRAWImage(void)
304 entry=SetMagickInfo("R");
305 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
306 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
307 entry->raw=MagickTrue;
308 entry->endian_support=MagickTrue;
309 entry->description=ConstantString("Raw red samples");
310 entry->module=ConstantString("RAW");
311 (void) RegisterMagickInfo(entry);
312 entry=SetMagickInfo("C");
313 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
314 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
315 entry->raw=MagickTrue;
316 entry->endian_support=MagickTrue;
317 entry->description=ConstantString("Raw cyan samples");
318 entry->module=ConstantString("RAW");
319 (void) RegisterMagickInfo(entry);
320 entry=SetMagickInfo("G");
321 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
322 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
323 entry->raw=MagickTrue;
324 entry->endian_support=MagickTrue;
325 entry->description=ConstantString("Raw green samples");
326 entry->module=ConstantString("RAW");
327 (void) RegisterMagickInfo(entry);
328 entry=SetMagickInfo("M");
329 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
330 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
331 entry->raw=MagickTrue;
332 entry->endian_support=MagickTrue;
333 entry->description=ConstantString("Raw magenta samples");
334 entry->module=ConstantString("RAW");
335 (void) RegisterMagickInfo(entry);
336 entry=SetMagickInfo("B");
337 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
338 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
339 entry->raw=MagickTrue;
340 entry->endian_support=MagickTrue;
341 entry->description=ConstantString("Raw blue samples");
342 entry->module=ConstantString("RAW");
343 (void) RegisterMagickInfo(entry);
344 entry=SetMagickInfo("Y");
345 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
346 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
347 entry->raw=MagickTrue;
348 entry->endian_support=MagickTrue;
349 entry->description=ConstantString("Raw yellow samples");
350 entry->module=ConstantString("RAW");
351 (void) RegisterMagickInfo(entry);
352 entry=SetMagickInfo("A");
353 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
354 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
355 entry->raw=MagickTrue;
356 entry->endian_support=MagickTrue;
357 entry->description=ConstantString("Raw alpha samples");
358 entry->module=ConstantString("RAW");
359 (void) RegisterMagickInfo(entry);
360 entry=SetMagickInfo("O");
361 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
362 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
363 entry->raw=MagickTrue;
364 entry->endian_support=MagickTrue;
365 entry->description=ConstantString("Raw opacity samples");
366 entry->module=ConstantString("RAW");
367 (void) RegisterMagickInfo(entry);
368 entry=SetMagickInfo("K");
369 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
370 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
371 entry->raw=MagickTrue;
372 entry->endian_support=MagickTrue;
373 entry->description=ConstantString("Raw black samples");
374 entry->module=ConstantString("RAW");
375 (void) RegisterMagickInfo(entry);
376 return(MagickImageCoderSignature);
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384 % U n r e g i s t e r R A W I m a g e %
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 % UnregisterRAWImage() removes format registrations made by the RAW module
391 % from the list of supported formats.
393 % The format of the UnregisterRAWImage method is:
395 % UnregisterRAWImage(void)
398 ModuleExport void UnregisterRAWImage(void)
400 (void) UnregisterMagickInfo("R");
401 (void) UnregisterMagickInfo("C");
402 (void) UnregisterMagickInfo("G");
403 (void) UnregisterMagickInfo("M");
404 (void) UnregisterMagickInfo("B");
405 (void) UnregisterMagickInfo("Y");
406 (void) UnregisterMagickInfo("A");
407 (void) UnregisterMagickInfo("O");
408 (void) UnregisterMagickInfo("K");
412 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
416 % W r i t e R A W I m a g e %
420 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 % WriteRAWImage() writes an image to a file as raw intensity values.
424 % The format of the WriteRAWImage method is:
426 % MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image)
428 % A description of each parameter follows.
430 % o image_info: the image info.
432 % o image: The image.
435 static MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image)
452 register const PixelPacket
465 Open output image file.
467 assert(image_info != (const ImageInfo *) NULL);
468 assert(image_info->signature == MagickSignature);
469 assert(image != (Image *) NULL);
470 assert(image->signature == MagickSignature);
471 if (image->debug != MagickFalse)
472 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
473 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
474 if (status == MagickFalse)
476 switch (*image->magick)
481 quantum_type=AlphaQuantum;
487 quantum_type=BlueQuantum;
493 quantum_type=CyanQuantum;
494 if (image->colorspace == CMYKColorspace)
496 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
501 quantum_type=GreenQuantum;
507 quantum_type=IndexQuantum;
513 quantum_type=BlackQuantum;
514 if (image->colorspace == CMYKColorspace)
516 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
521 quantum_type=MagentaQuantum;
522 if (image->colorspace == CMYKColorspace)
524 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
529 quantum_type=OpacityQuantum;
535 quantum_type=RedQuantum;
541 quantum_type=YellowQuantum;
542 if (image->colorspace == CMYKColorspace)
544 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
548 quantum_type=GrayQuantum;
556 Convert image to RAW raster pixels.
558 quantum_info=AcquireQuantumInfo(image_info,image);
559 if (quantum_info == (QuantumInfo *) NULL)
560 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
561 pixels=GetQuantumPixels(quantum_info);
562 for (y=0; y < (ssize_t) image->rows; y++)
564 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
565 if (p == (const PixelPacket *) NULL)
567 length=ExportQuantumPixels(image,(const CacheView *) NULL,quantum_info,
568 quantum_type,pixels,&image->exception);
569 count=WriteBlob(image,length,pixels);
570 if (count != (ssize_t) length)
572 if (image->previous == (Image *) NULL)
574 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
576 if (status == MagickFalse)
580 quantum_info=DestroyQuantumInfo(quantum_info);
581 if (GetNextImageInList(image) == (Image *) NULL)
583 image=SyncNextImageInList(image);
584 status=SetImageProgress(image,SaveImagesTag,scene++,
585 GetImageListLength(image));
586 if (status == MagickFalse)
588 } while (image_info->adjoin != MagickFalse);
589 (void) CloseBlob(image);