2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write RAW Image Format %
20 % Copyright 1999-2017 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,ExceptionInfo *exception)
128 assert(image_info != (const ImageInfo *) NULL);
129 assert(image_info->signature == MagickCoreSignature);
130 if (image_info->debug != MagickFalse)
131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
132 image_info->filename);
133 assert(exception != (ExceptionInfo *) NULL);
134 assert(exception->signature == MagickCoreSignature);
135 image=AcquireImage(image_info,exception);
136 if ((image->columns == 0) || (image->rows == 0))
137 ThrowReaderException(OptionError,"MustSpecifyImageSize");
138 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
139 if (status == MagickFalse)
141 image=DestroyImageList(image);
142 return((Image *) NULL);
144 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
145 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
148 Create virtual canvas to support cropping (i.e. image.gray[100x100+10+20]).
150 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
152 (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=(const unsigned char *) NULL;
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 pixels=(const unsigned char *) ReadBlobStream(image,length,
170 GetQuantumPixels(quantum_info),&count);
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))
186 status=SetImageExtent(image,image->columns,image->rows,exception);
187 if (status == MagickFalse)
188 return(DestroyImageList(image));
191 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
192 pixels=(const unsigned char *) ReadBlobStream(image,length,
193 GetQuantumPixels(quantum_info),&count);
195 for (y=0; y < (ssize_t) image->extract_info.height; y++)
197 register const Quantum
206 if (count != (ssize_t) length)
208 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
212 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,exception);
213 if (q == (Quantum *) NULL)
215 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,quantum_info,
216 quantum_type,pixels,exception);
217 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
219 if (((y-image->extract_info.y) >= 0) &&
220 ((y-image->extract_info.y) < (ssize_t) image->rows))
222 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
223 image->columns,1,exception);
224 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,image->columns,
226 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
228 for (x=0; x < (ssize_t) image->columns; x++)
230 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
231 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
232 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
233 p+=GetPixelChannels(canvas_image);
234 q+=GetPixelChannels(image);
236 if (SyncAuthenticPixels(image,exception) == MagickFalse)
239 if (image->previous == (Image *) NULL)
241 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
243 if (status == MagickFalse)
246 pixels=(const unsigned char *) ReadBlobStream(image,length,
247 GetQuantumPixels(quantum_info),&count);
249 SetQuantumImageType(image,quantum_type);
251 Proceed to next image.
253 if (image_info->number_scenes != 0)
254 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
256 if (count == (ssize_t) length)
259 Allocate next image structure.
261 AcquireNextImage(image_info,image,exception);
262 if (GetNextImageInList(image) == (Image *) NULL)
264 image=DestroyImageList(image);
265 return((Image *) NULL);
267 image=SyncNextImageInList(image);
268 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
270 if (status == MagickFalse)
274 } while (count == (ssize_t) length);
275 quantum_info=DestroyQuantumInfo(quantum_info);
276 canvas_image=DestroyImage(canvas_image);
277 (void) CloseBlob(image);
278 return(GetFirstImageInList(image));
282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 % R e g i s t e r R A W I m a g e %
290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 % RegisterRAWImage() adds attributes for the RAW image format to the list of
293 % supported formats. The attributes include the image format tag, a method to
294 % read and/or write the format, whether the format supports the saving of
295 % more than one frame to the same file or blob, whether the format supports
296 % native in-memory I/O, and a brief description of the format.
298 % The format of the RegisterRAWImage method is:
300 % size_t RegisterRAWImage(void)
303 ModuleExport size_t RegisterRAWImage(void)
308 entry=AcquireMagickInfo("RAW","R","Raw red samples");
309 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
310 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
311 entry->flags|=CoderRawSupportFlag;
312 entry->flags|=CoderEndianSupportFlag;
313 entry->format_type=ImplicitFormatType;
314 (void) RegisterMagickInfo(entry);
315 entry=AcquireMagickInfo("RAW","C","Raw cyan samples");
316 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
317 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
318 entry->flags|=CoderRawSupportFlag;
319 entry->flags|=CoderEndianSupportFlag;
320 entry->format_type=ImplicitFormatType;
321 (void) RegisterMagickInfo(entry);
322 entry=AcquireMagickInfo("RAW","G","Raw green samples");
323 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
324 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
325 entry->flags|=CoderRawSupportFlag;
326 entry->flags|=CoderEndianSupportFlag;
327 entry->format_type=ImplicitFormatType;
328 (void) RegisterMagickInfo(entry);
329 entry=AcquireMagickInfo("RAW","M","Raw magenta samples");
330 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
331 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
332 entry->flags|=CoderRawSupportFlag;
333 entry->flags|=CoderEndianSupportFlag;
334 entry->format_type=ImplicitFormatType;
335 (void) RegisterMagickInfo(entry);
336 entry=AcquireMagickInfo("RAW","B","Raw blue samples");
337 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
338 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
339 entry->flags|=CoderRawSupportFlag;
340 entry->flags|=CoderEndianSupportFlag;
341 entry->format_type=ImplicitFormatType;
342 (void) RegisterMagickInfo(entry);
343 entry=AcquireMagickInfo("RAW","Y","Raw yellow samples");
344 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
345 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
346 entry->flags|=CoderRawSupportFlag;
347 entry->flags|=CoderEndianSupportFlag;
348 entry->format_type=ImplicitFormatType;
349 (void) RegisterMagickInfo(entry);
350 entry=AcquireMagickInfo("RAW","A","Raw alpha samples");
351 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
352 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
353 entry->flags|=CoderRawSupportFlag;
354 entry->flags|=CoderEndianSupportFlag;
355 entry->format_type=ImplicitFormatType;
356 (void) RegisterMagickInfo(entry);
357 entry=AcquireMagickInfo("RAW","O","Raw opacity samples");
358 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
359 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
360 entry->flags|=CoderRawSupportFlag;
361 entry->flags|=CoderEndianSupportFlag;
362 entry->format_type=ImplicitFormatType;
363 (void) RegisterMagickInfo(entry);
364 entry=AcquireMagickInfo("RAW","K","Raw black samples");
365 entry->decoder=(DecodeImageHandler *) ReadRAWImage;
366 entry->encoder=(EncodeImageHandler *) WriteRAWImage;
367 entry->flags|=CoderRawSupportFlag;
368 entry->flags|=CoderEndianSupportFlag;
369 entry->format_type=ImplicitFormatType;
370 (void) RegisterMagickInfo(entry);
371 return(MagickImageCoderSignature);
375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 % U n r e g i s t e r R A W I m a g e %
383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385 % UnregisterRAWImage() removes format registrations made by the RAW module
386 % from the list of supported formats.
388 % The format of the UnregisterRAWImage method is:
390 % UnregisterRAWImage(void)
393 ModuleExport void UnregisterRAWImage(void)
395 (void) UnregisterMagickInfo("R");
396 (void) UnregisterMagickInfo("C");
397 (void) UnregisterMagickInfo("G");
398 (void) UnregisterMagickInfo("M");
399 (void) UnregisterMagickInfo("B");
400 (void) UnregisterMagickInfo("Y");
401 (void) UnregisterMagickInfo("A");
402 (void) UnregisterMagickInfo("O");
403 (void) UnregisterMagickInfo("K");
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411 % W r i t e R A W I m a g e %
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 % WriteRAWImage() writes an image to a file as raw intensity values.
419 % The format of the WriteRAWImage method is:
421 % MagickBooleanType WriteRAWImage(const ImageInfo *image_info,
422 % Image *image,ExceptionInfo *exception)
424 % A description of each parameter follows.
426 % o image_info: the image info.
428 % o image: The image.
430 % o exception: return any errors or warnings in this structure.
433 static MagickBooleanType WriteRAWImage(const ImageInfo *image_info,Image *image,
434 ExceptionInfo *exception)
448 register const Quantum
462 Open output image file.
464 assert(image_info != (const ImageInfo *) NULL);
465 assert(image_info->signature == MagickCoreSignature);
466 assert(image != (Image *) NULL);
467 assert(image->signature == MagickCoreSignature);
468 if (image->debug != MagickFalse)
469 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
470 assert(exception != (ExceptionInfo *) NULL);
471 assert(exception->signature == MagickCoreSignature);
472 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
473 if (status == MagickFalse)
475 switch (*image->magick)
480 quantum_type=AlphaQuantum;
486 quantum_type=BlueQuantum;
492 quantum_type=CyanQuantum;
493 if (image->colorspace == CMYKColorspace)
495 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
500 quantum_type=GreenQuantum;
506 quantum_type=IndexQuantum;
512 quantum_type=BlackQuantum;
513 if (image->colorspace == CMYKColorspace)
515 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
520 quantum_type=MagentaQuantum;
521 if (image->colorspace == CMYKColorspace)
523 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
528 quantum_type=OpacityQuantum;
534 quantum_type=RedQuantum;
540 quantum_type=YellowQuantum;
541 if (image->colorspace == CMYKColorspace)
543 ThrowWriterException(ImageError,"ColorSeparatedImageRequired");
547 quantum_type=GrayQuantum;
555 Convert image to RAW raster pixels.
557 quantum_info=AcquireQuantumInfo(image_info,image);
558 if (quantum_info == (QuantumInfo *) NULL)
559 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
560 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
561 for (y=0; y < (ssize_t) image->rows; y++)
563 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
564 if (p == (const Quantum *) NULL)
566 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
567 quantum_type,pixels,exception);
568 count=WriteBlob(image,length,pixels);
569 if (count != (ssize_t) length)
571 if (image->previous == (Image *) NULL)
573 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
575 if (status == MagickFalse)
579 quantum_info=DestroyQuantumInfo(quantum_info);
580 if (GetNextImageInList(image) == (Image *) NULL)
582 image=SyncNextImageInList(image);
583 status=SetImageProgress(image,SaveImagesTag,scene++,
584 GetImageListLength(image));
585 if (status == MagickFalse)
587 } while (image_info->adjoin != MagickFalse);
588 (void) CloseBlob(image);