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 "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"
68 static MagickBooleanType
69 WriteRAWImage(const ImageInfo *,Image *,ExceptionInfo *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % R e a d R A W I m a g e %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
86 % The format of the ReadRAWImage method is:
88 % Image *ReadRAWImage(const ImageInfo *image_info,ExceptionInfo *exception)
90 % A description of each parameter follows:
92 % o image_info: the image info.
94 % o exception: return any errors or warnings in this structure.
97 static Image *ReadRAWImage(const ImageInfo *image_info,
98 ExceptionInfo *exception)
129 assert(image_info != (const ImageInfo *) NULL);
130 assert(image_info->signature == MagickSignature);
131 if (image_info->debug != MagickFalse)
132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
133 image_info->filename);
134 assert(exception != (ExceptionInfo *) NULL);
135 assert(exception->signature == MagickSignature);
136 image=AcquireImage(image_info,exception);
137 if ((image->columns == 0) || (image->rows == 0))
138 ThrowReaderException(OptionError,"MustSpecifyImageSize");
139 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
140 if (status == MagickFalse)
142 image=DestroyImageList(image);
143 return((Image *) NULL);
145 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
146 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
149 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
151 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
153 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod);
154 quantum_type=GrayQuantum;
155 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
156 if (quantum_info == (QuantumInfo *) NULL)
157 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
158 pixels=GetQuantumPixels(quantum_info);
159 if (image_info->number_scenes != 0)
160 while (image->scene < image_info->scene)
166 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
167 for (y=0; y < (ssize_t) image->rows; y++)
169 count=ReadBlob(image,length,pixels);
170 if (count != (ssize_t) length)
180 Read pixels to virtual canvas image then push to image.
182 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
183 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
187 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
188 count=ReadBlob(image,length,pixels);
190 for (y=0; y < (ssize_t) image->extract_info.height; y++)
192 register const Quantum
201 if (count != (ssize_t) length)
203 ThrowFileException(exception,CorruptImageError,
204 "UnexpectedEndOfFile",image->filename);
207 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
208 if (q == (Quantum *) NULL)
210 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
211 quantum_type,pixels,exception);
212 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
214 if (((y-image->extract_info.y) >= 0) &&
215 ((y-image->extract_info.y) < (ssize_t) image->rows))
217 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
218 image->columns,1,exception);
219 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
221 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
223 for (x=0; x < (ssize_t) image->columns; x++)
225 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
226 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
227 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
228 p+=GetPixelChannels(canvas_image);
229 q+=GetPixelChannels(image);
231 if (SyncAuthenticPixels(image,exception) == MagickFalse)
234 if (image->previous == (Image *) NULL)
236 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
238 if (status == MagickFalse)
241 count=ReadBlob(image,length,pixels);
243 SetQuantumImageType(image,quantum_type);
245 Proceed to next image.
247 if (image_info->number_scenes != 0)
248 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
250 if (count == (ssize_t) length)
253 Allocate next image structure.
255 AcquireNextImage(image_info,image,exception);
256 if (GetNextImageInList(image) == (Image *) NULL)
258 image=DestroyImageList(image);
259 return((Image *) NULL);
261 image=SyncNextImageInList(image);
262 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
264 if (status == MagickFalse)
268 } while (count == (ssize_t) length);
269 quantum_info=DestroyQuantumInfo(quantum_info);
270 InheritException(&image->exception,&canvas_image->exception);
271 canvas_image=DestroyImage(canvas_image);
272 (void) CloseBlob(image);
273 return(GetFirstImageInList(image));
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % R e g i s t e r R A W I m a g e %
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 % RegisterRAWImage() adds attributes for the RAW image format to the list of
288 % supported formats. The attributes include the image format tag, a method to
289 % read and/or write the format, whether the format supports the saving of
290 % more than one frame to the same file or blob, whether the format supports
291 % native in-memory I/O, and a brief description of the format.
293 % The format of the RegisterRAWImage method is:
295 % size_t RegisterRAWImage(void)
298 ModuleExport size_t RegisterRAWImage(void)
303 entry=SetMagickInfo("R");
304 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
305 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
306 entry->raw=MagickTrue;
307 entry->endian_support=MagickTrue;
308 entry->description=ConstantString("Raw red samples");
309 entry->module=ConstantString("RAW");
310 (void) RegisterMagickInfo(entry);
311 entry=SetMagickInfo("C");
312 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
313 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
314 entry->raw=MagickTrue;
315 entry->endian_support=MagickTrue;
316 entry->description=ConstantString("Raw cyan samples");
317 entry->module=ConstantString("RAW");
318 (void) RegisterMagickInfo(entry);
319 entry=SetMagickInfo("G");
320 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
321 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
322 entry->raw=MagickTrue;
323 entry->endian_support=MagickTrue;
324 entry->description=ConstantString("Raw green samples");
325 entry->module=ConstantString("RAW");
326 (void) RegisterMagickInfo(entry);
327 entry=SetMagickInfo("M");
328 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
329 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
330 entry->raw=MagickTrue;
331 entry->endian_support=MagickTrue;
332 entry->description=ConstantString("Raw magenta samples");
333 entry->module=ConstantString("RAW");
334 (void) RegisterMagickInfo(entry);
335 entry=SetMagickInfo("B");
336 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
337 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
338 entry->raw=MagickTrue;
339 entry->endian_support=MagickTrue;
340 entry->description=ConstantString("Raw blue samples");
341 entry->module=ConstantString("RAW");
342 (void) RegisterMagickInfo(entry);
343 entry=SetMagickInfo("Y");
344 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
345 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
346 entry->raw=MagickTrue;
347 entry->endian_support=MagickTrue;
348 entry->description=ConstantString("Raw yellow samples");
349 entry->module=ConstantString("RAW");
350 (void) RegisterMagickInfo(entry);
351 entry=SetMagickInfo("A");
352 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
353 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
354 entry->raw=MagickTrue;
355 entry->endian_support=MagickTrue;
356 entry->description=ConstantString("Raw alpha samples");
357 entry->module=ConstantString("RAW");
358 (void) RegisterMagickInfo(entry);
359 entry=SetMagickInfo("O");
360 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
361 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
362 entry->raw=MagickTrue;
363 entry->endian_support=MagickTrue;
364 entry->description=ConstantString("Raw opacity samples");
365 entry->module=ConstantString("RAW");
366 (void) RegisterMagickInfo(entry);
367 entry=SetMagickInfo("K");
368 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
369 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
370 entry->raw=MagickTrue;
371 entry->endian_support=MagickTrue;
372 entry->description=ConstantString("Raw black samples");
373 entry->module=ConstantString("RAW");
374 (void) RegisterMagickInfo(entry);
375 return(MagickImageCoderSignature);
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 % U n r e g i s t e r R A W I m a g e %
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 % UnregisterRAWImage() removes format registrations made by the RAW module
390 % from the list of supported formats.
392 % The format of the UnregisterRAWImage method is:
394 % UnregisterRAWImage(void)
397 ModuleExport void UnregisterRAWImage(void)
399 (void) UnregisterMagickInfo("R");
400 (void) UnregisterMagickInfo("C");
401 (void) UnregisterMagickInfo("G");
402 (void) UnregisterMagickInfo("M");
403 (void) UnregisterMagickInfo("B");
404 (void) UnregisterMagickInfo("Y");
405 (void) UnregisterMagickInfo("A");
406 (void) UnregisterMagickInfo("O");
407 (void) UnregisterMagickInfo("K");
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
415 % W r i t e R A W I m a g e %
419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421 % WriteRAWImage() writes an image to a file as raw intensity values.
423 % The format of the WriteRAWImage method is:
425 % MagickBooleanType WriteRAWImage(const ImageInfo *image_info,
426 % Image *image,ExceptionInfo *exception)
428 % A description of each parameter follows.
430 % o image_info: the image info.
432 % o image: The image.
434 % o exception: return any errors or warnings in this structure.
437 static MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image,
438 ExceptionInfo *exception)
452 register const Quantum
466 Open output image file.
468 assert(image_info != (const ImageInfo *) NULL);
469 assert(image_info->signature == MagickSignature);
470 assert(image != (Image *) NULL);
471 assert(image->signature == MagickSignature);
472 if (image->debug != MagickFalse)
473 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
474 assert(exception != (ExceptionInfo *) NULL);
475 assert(exception->signature == MagickSignature);
476 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
477 if (status == MagickFalse)
479 switch (*image->magick)
484 quantum_type=AlphaQuantum;
490 quantum_type=BlueQuantum;
496 quantum_type=CyanQuantum;
497 if (image->colorspace == CMYKColorspace)
499 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
504 quantum_type=GreenQuantum;
510 quantum_type=IndexQuantum;
516 quantum_type=BlackQuantum;
517 if (image->colorspace == CMYKColorspace)
519 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
524 quantum_type=MagentaQuantum;
525 if (image->colorspace == CMYKColorspace)
527 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
532 quantum_type=OpacityQuantum;
538 quantum_type=RedQuantum;
544 quantum_type=YellowQuantum;
545 if (image->colorspace == CMYKColorspace)
547 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
551 quantum_type=GrayQuantum;
559 Convert image to RAW raster pixels.
561 quantum_info=AcquireQuantumInfo(image_info,image);
562 if (quantum_info == (QuantumInfo *) NULL)
563 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
564 pixels=GetQuantumPixels(quantum_info);
565 for (y=0; y < (ssize_t) image->rows; y++)
567 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
568 if (p == (const Quantum *) NULL)
570 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
571 quantum_type,pixels,exception);
572 count=WriteBlob(image,length,pixels);
573 if (count != (ssize_t) length)
575 if (image->previous == (Image *) NULL)
577 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
579 if (status == MagickFalse)
583 quantum_info=DestroyQuantumInfo(quantum_info);
584 if (GetNextImageInList(image) == (Image *) NULL)
586 image=SyncNextImageInList(image);
587 status=SetImageProgress(image,SaveImagesTag,scene++,
588 GetImageListLength(image));
589 if (status == MagickFalse)
591 } while (image_info->adjoin != MagickFalse);
592 (void) CloseBlob(image);