2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Raw BGR Image Format %
20 % Copyright 1999-2008 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/colorspace-private.h"
48 #include "MagickCore/constitute.h"
49 #include "MagickCore/exception.h"
50 #include "MagickCore/exception-private.h"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/pixel-accessor.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"
64 #include "MagickCore/utility.h"
69 static MagickBooleanType
70 WriteBGRImage(const ImageInfo *,Image *,ExceptionInfo *);
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % R e a d B G R I m a g e %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % ReadBGRImage() reads an image of raw BGR, or BGRA samples and returns
84 % it. It allocates the memory necessary for the new Image structure and
85 % returns a pointer to the new image.
87 % The format of the ReadBGRImage method is:
89 % Image *ReadBGRImage(const ImageInfo *image_info,
90 % ExceptionInfo *exception)
92 % A description of each parameter follows:
94 % o image_info: the image info.
96 % o exception: return any errors or warnings in this structure.
99 static Image *ReadBGRImage(const ImageInfo *image_info,
100 ExceptionInfo *exception)
134 assert(image_info != (const ImageInfo *) NULL);
135 assert(image_info->signature == MagickSignature);
136 if (image_info->debug != MagickFalse)
137 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
138 image_info->filename);
139 assert(exception != (ExceptionInfo *) NULL);
140 assert(exception->signature == MagickSignature);
141 image=AcquireImage(image_info,exception);
142 if ((image->columns == 0) || (image->rows == 0))
143 ThrowReaderException(OptionError,"MustSpecifyImageSize");
144 image->colorspace=RGBColorspace;
145 if (image_info->interlace != PartitionInterlace)
147 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
148 if (status == MagickFalse)
150 image=DestroyImageList(image);
151 return((Image *) NULL);
153 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
154 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
158 Create virtual canvas to support cropping (i.e. image.rgb[100x100+10+20]).
160 canvas_image=CloneImage(image,image->extract_info.width,1,MagickFalse,
162 (void) SetImageVirtualPixelMethod(canvas_image,BlackVirtualPixelMethod);
163 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
164 if (quantum_info == (QuantumInfo *) NULL)
165 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
166 pixels=GetQuantumPixels(quantum_info);
167 quantum_type=BGRQuantum;
168 if (LocaleCompare(image_info->magick,"BGRA") == 0)
170 quantum_type=BGRAQuantum;
171 image->matte=MagickTrue;
173 if (image_info->number_scenes != 0)
174 while (image->scene < image_info->scene)
180 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
181 for (y=0; y < (ssize_t) image->rows; y++)
183 count=ReadBlob(image,length,pixels);
184 if (count != (ssize_t) length)
194 Read pixels to virtual canvas image then push to image.
196 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
197 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
199 image->colorspace=RGBColorspace;
200 switch (image_info->interlace)
206 No interlacing: BGRBGRBGRBGRBGRBGR...
210 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
211 count=ReadBlob(image,length,pixels);
213 for (y=0; y < (ssize_t) image->extract_info.height; y++)
215 register const Quantum
224 if (count != (ssize_t) length)
226 ThrowFileException(exception,CorruptImageError,
227 "UnexpectedEndOfFile",image->filename);
230 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
232 if (q == (Quantum *) NULL)
234 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
235 quantum_info,quantum_type,pixels,exception);
236 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
238 if (((y-image->extract_info.y) >= 0) &&
239 ((y-image->extract_info.y) < (ssize_t) image->rows))
241 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
242 canvas_image->columns,1,exception);
243 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,
244 image->columns,1,exception);
245 if ((p == (const Quantum *) NULL) ||
246 (q == (Quantum *) NULL))
248 for (x=0; x < (ssize_t) image->columns; x++)
250 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
251 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
252 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
253 SetPixelAlpha(image,OpaqueAlpha,q);
254 if (image->matte != MagickFalse)
255 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
256 p+=GetPixelChannels(canvas_image);
257 q+=GetPixelChannels(image);
259 if (SyncAuthenticPixels(image,exception) == MagickFalse)
262 if (image->previous == (Image *) NULL)
264 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
266 if (status == MagickFalse)
269 count=ReadBlob(image,length,pixels);
285 Line interlacing: BBB...GGG...RRR...RRR...GGG...BBB...
289 length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
290 count=ReadBlob(image,length,pixels);
292 for (y=0; y < (ssize_t) image->extract_info.height; y++)
294 register const Quantum
303 if (count != (ssize_t) length)
305 ThrowFileException(exception,CorruptImageError,
306 "UnexpectedEndOfFile",image->filename);
309 for (i=0; i < (ssize_t) (image->matte != MagickFalse ? 4 : 3); i++)
311 quantum_type=quantum_types[i];
312 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
314 if (q == (Quantum *) NULL)
316 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
317 quantum_info,quantum_type,pixels,exception);
318 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
320 if (((y-image->extract_info.y) >= 0) &&
321 ((y-image->extract_info.y) < (ssize_t) image->rows))
323 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
324 canvas_image->columns,1,exception);
325 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
326 image->columns,1,exception);
327 if ((p == (const Quantum *) NULL) ||
328 (q == (Quantum *) NULL))
330 for (x=0; x < (ssize_t) image->columns; x++)
332 switch (quantum_type)
336 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
341 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
346 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
351 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
356 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
362 p+=GetPixelChannels(canvas_image);
363 q+=GetPixelChannels(image);
365 if (SyncAuthenticPixels(image,exception) == MagickFalse)
368 count=ReadBlob(image,length,pixels);
370 if (image->previous == (Image *) NULL)
372 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
374 if (status == MagickFalse)
383 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
387 length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
388 count=ReadBlob(image,length,pixels);
390 for (y=0; y < (ssize_t) image->extract_info.height; y++)
392 register const Quantum
401 if (count != (ssize_t) length)
403 ThrowFileException(exception,CorruptImageError,
404 "UnexpectedEndOfFile",image->filename);
407 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
409 if (q == (Quantum *) NULL)
411 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
412 quantum_info,RedQuantum,pixels,exception);
413 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
415 if (((y-image->extract_info.y) >= 0) &&
416 ((y-image->extract_info.y) < (ssize_t) image->rows))
418 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
419 canvas_image->columns,1,exception);
420 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
421 image->columns,1,exception);
422 if ((p == (const Quantum *) NULL) ||
423 (q == (Quantum *) NULL))
425 for (x=0; x < (ssize_t) image->columns; x++)
427 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
428 p+=GetPixelChannels(canvas_image);
429 q+=GetPixelChannels(image);
431 if (SyncAuthenticPixels(image,exception) == MagickFalse)
434 count=ReadBlob(image,length,pixels);
436 if (image->previous == (Image *) NULL)
438 status=SetImageProgress(image,LoadImageTag,1,6);
439 if (status == MagickFalse)
442 for (y=0; y < (ssize_t) image->extract_info.height; y++)
444 register const Quantum
453 if (count != (ssize_t) length)
455 ThrowFileException(exception,CorruptImageError,
456 "UnexpectedEndOfFile",image->filename);
459 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
461 if (q == (Quantum *) NULL)
463 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
464 quantum_info,GreenQuantum,pixels,exception);
465 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
467 if (((y-image->extract_info.y) >= 0) &&
468 ((y-image->extract_info.y) < (ssize_t) image->rows))
470 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
471 canvas_image->columns,1,exception);
472 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
473 image->columns,1,exception);
474 if ((p == (const Quantum *) NULL) ||
475 (q == (Quantum *) NULL))
477 for (x=0; x < (ssize_t) image->columns; x++)
479 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
480 p+=GetPixelChannels(canvas_image);
481 q+=GetPixelChannels(image);
483 if (SyncAuthenticPixels(image,exception) == MagickFalse)
486 count=ReadBlob(image,length,pixels);
488 if (image->previous == (Image *) NULL)
490 status=SetImageProgress(image,LoadImageTag,2,6);
491 if (status == MagickFalse)
494 for (y=0; y < (ssize_t) image->extract_info.height; y++)
496 register const Quantum
505 if (count != (ssize_t) length)
507 ThrowFileException(exception,CorruptImageError,
508 "UnexpectedEndOfFile",image->filename);
511 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
513 if (q == (Quantum *) NULL)
515 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
516 quantum_info,BlueQuantum,pixels,exception);
517 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
519 if (((y-image->extract_info.y) >= 0) &&
520 ((y-image->extract_info.y) < (ssize_t) image->rows))
522 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
523 canvas_image->columns,1,exception);
524 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
525 image->columns,1,exception);
526 if ((p == (const Quantum *) NULL) ||
527 (q == (Quantum *) NULL))
529 for (x=0; x < (ssize_t) image->columns; x++)
531 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
532 p+=GetPixelChannels(canvas_image);
533 q+=GetPixelChannels(image);
535 if (SyncAuthenticPixels(image,exception) == MagickFalse)
538 count=ReadBlob(image,length,pixels);
540 if (image->previous == (Image *) NULL)
542 status=SetImageProgress(image,LoadImageTag,3,6);
543 if (status == MagickFalse)
546 if (image->previous == (Image *) NULL)
548 status=SetImageProgress(image,LoadImageTag,4,6);
549 if (status == MagickFalse)
552 if (image->matte != MagickFalse)
554 for (y=0; y < (ssize_t) image->extract_info.height; y++)
556 register const Quantum
565 if (count != (ssize_t) length)
567 ThrowFileException(exception,CorruptImageError,
568 "UnexpectedEndOfFile",image->filename);
571 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
573 if (q == (Quantum *) NULL)
575 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
576 quantum_info,AlphaQuantum,pixels,exception);
577 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
579 if (((y-image->extract_info.y) >= 0) &&
580 ((y-image->extract_info.y) < (ssize_t) image->rows))
582 p=GetVirtualPixels(canvas_image,
583 canvas_image->extract_info.x,0,canvas_image->columns,1,
585 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
586 image->columns,1,exception);
587 if ((p == (const Quantum *) NULL) ||
588 (q == (Quantum *) NULL))
590 for (x=0; x < (ssize_t) image->columns; x++)
592 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
593 p+=GetPixelChannels(canvas_image);
594 q+=GetPixelChannels(image);
596 if (SyncAuthenticPixels(image,exception) == MagickFalse)
599 count=ReadBlob(image,length,pixels);
601 if (image->previous == (Image *) NULL)
603 status=SetImageProgress(image,LoadImageTag,5,6);
604 if (status == MagickFalse)
608 if (image->previous == (Image *) NULL)
610 status=SetImageProgress(image,LoadImageTag,6,6);
611 if (status == MagickFalse)
616 case PartitionInterlace:
619 Partition interlacing: BBBBBB..., GGGGGG..., RRRRRR...
621 AppendImageFormat("B",image->filename);
622 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
623 if (status == MagickFalse)
625 canvas_image=DestroyImageList(canvas_image);
626 image=DestroyImageList(image);
627 return((Image *) NULL);
629 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
630 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
632 length=GetQuantumExtent(canvas_image,quantum_info,BlueQuantum);
633 for (i=0; i < (ssize_t) scene; i++)
634 for (y=0; y < (ssize_t) image->extract_info.height; y++)
635 if (ReadBlob(image,length,pixels) != (ssize_t) length)
637 ThrowFileException(exception,CorruptImageError,
638 "UnexpectedEndOfFile",image->filename);
641 count=ReadBlob(image,length,pixels);
642 for (y=0; y < (ssize_t) image->extract_info.height; y++)
644 register const Quantum
653 if (count != (ssize_t) length)
655 ThrowFileException(exception,CorruptImageError,
656 "UnexpectedEndOfFile",image->filename);
659 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
661 if (q == (Quantum *) NULL)
663 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
664 quantum_info,BlueQuantum,pixels,exception);
665 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
667 if (((y-image->extract_info.y) >= 0) &&
668 ((y-image->extract_info.y) < (ssize_t) image->rows))
670 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
671 canvas_image->columns,1,exception);
672 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
673 image->columns,1,exception);
674 if ((p == (const Quantum *) NULL) ||
675 (q == (Quantum *) NULL))
677 for (x=0; x < (ssize_t) image->columns; x++)
679 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
680 p+=GetPixelChannels(canvas_image);
681 q+=GetPixelChannels(image);
683 if (SyncAuthenticPixels(image,exception) == MagickFalse)
686 count=ReadBlob(image,length,pixels);
688 if (image->previous == (Image *) NULL)
690 status=SetImageProgress(image,LoadImageTag,1,5);
691 if (status == MagickFalse)
694 (void) CloseBlob(image);
695 AppendImageFormat("G",image->filename);
696 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
697 if (status == MagickFalse)
699 canvas_image=DestroyImageList(canvas_image);
700 image=DestroyImageList(image);
701 return((Image *) NULL);
703 length=GetQuantumExtent(canvas_image,quantum_info,GreenQuantum);
704 for (i=0; i < (ssize_t) scene; i++)
705 for (y=0; y < (ssize_t) image->extract_info.height; y++)
706 if (ReadBlob(image,length,pixels) != (ssize_t) length)
708 ThrowFileException(exception,CorruptImageError,
709 "UnexpectedEndOfFile",image->filename);
712 count=ReadBlob(image,length,pixels);
713 for (y=0; y < (ssize_t) image->extract_info.height; y++)
715 register const Quantum
724 if (count != (ssize_t) length)
726 ThrowFileException(exception,CorruptImageError,
727 "UnexpectedEndOfFile",image->filename);
730 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
732 if (q == (Quantum *) NULL)
734 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
735 quantum_info,GreenQuantum,pixels,exception);
736 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
738 if (((y-image->extract_info.y) >= 0) &&
739 ((y-image->extract_info.y) < (ssize_t) image->rows))
741 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
742 canvas_image->columns,1,exception);
743 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
744 image->columns,1,exception);
745 if ((p == (const Quantum *) NULL) ||
746 (q == (Quantum *) NULL))
748 for (x=0; x < (ssize_t) image->columns; x++)
750 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
751 p+=GetPixelChannels(canvas_image);
752 q+=GetPixelChannels(image);
754 if (SyncAuthenticPixels(image,exception) == MagickFalse)
757 count=ReadBlob(image,length,pixels);
759 if (image->previous == (Image *) NULL)
761 status=SetImageProgress(image,LoadImageTag,2,5);
762 if (status == MagickFalse)
765 (void) CloseBlob(image);
766 AppendImageFormat("R",image->filename);
767 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
768 if (status == MagickFalse)
770 canvas_image=DestroyImageList(canvas_image);
771 image=DestroyImageList(image);
772 return((Image *) NULL);
774 length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
775 for (i=0; i < (ssize_t) scene; i++)
776 for (y=0; y < (ssize_t) image->extract_info.height; y++)
777 if (ReadBlob(image,length,pixels) != (ssize_t) length)
779 ThrowFileException(exception,CorruptImageError,
780 "UnexpectedEndOfFile",image->filename);
783 count=ReadBlob(image,length,pixels);
784 for (y=0; y < (ssize_t) image->extract_info.height; y++)
786 register const Quantum
795 if (count != (ssize_t) length)
797 ThrowFileException(exception,CorruptImageError,
798 "UnexpectedEndOfFile",image->filename);
801 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
803 if (q == (Quantum *) NULL)
805 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
806 quantum_info,RedQuantum,pixels,exception);
807 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
809 if (((y-image->extract_info.y) >= 0) &&
810 ((y-image->extract_info.y) < (ssize_t) image->rows))
812 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
813 canvas_image->columns,1,exception);
814 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
815 image->columns,1,exception);
816 if ((p == (const Quantum *) NULL) ||
817 (q == (Quantum *) NULL))
819 for (x=0; x < (ssize_t) image->columns; x++)
821 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
822 p+=GetPixelChannels(canvas_image);
823 q+=GetPixelChannels(image);
825 if (SyncAuthenticPixels(image,exception) == MagickFalse)
828 count=ReadBlob(image,length,pixels);
830 if (image->previous == (Image *) NULL)
832 status=SetImageProgress(image,LoadImageTag,3,5);
833 if (status == MagickFalse)
836 if (image->matte != MagickFalse)
838 (void) CloseBlob(image);
839 AppendImageFormat("A",image->filename);
840 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
841 if (status == MagickFalse)
843 canvas_image=DestroyImageList(canvas_image);
844 image=DestroyImageList(image);
845 return((Image *) NULL);
847 length=GetQuantumExtent(canvas_image,quantum_info,AlphaQuantum);
848 for (i=0; i < (ssize_t) scene; i++)
849 for (y=0; y < (ssize_t) image->extract_info.height; y++)
850 if (ReadBlob(image,length,pixels) != (ssize_t) length)
852 ThrowFileException(exception,CorruptImageError,
853 "UnexpectedEndOfFile",image->filename);
856 count=ReadBlob(image,length,pixels);
857 for (y=0; y < (ssize_t) image->extract_info.height; y++)
859 register const Quantum
868 if (count != (ssize_t) length)
870 ThrowFileException(exception,CorruptImageError,
871 "UnexpectedEndOfFile",image->filename);
874 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
876 if (q == (Quantum *) NULL)
878 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
879 quantum_info,BlueQuantum,pixels,exception);
880 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
882 if (((y-image->extract_info.y) >= 0) &&
883 ((y-image->extract_info.y) < (ssize_t) image->rows))
885 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,
886 0,canvas_image->columns,1,exception);
887 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
888 image->columns,1,exception);
889 if ((p == (const Quantum *) NULL) ||
890 (q == (Quantum *) NULL))
892 for (x=0; x < (ssize_t) image->columns; x++)
894 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
895 p+=GetPixelChannels(canvas_image);
896 q+=GetPixelChannels(image);
898 if (SyncAuthenticPixels(image,exception) == MagickFalse)
901 count=ReadBlob(image,length,pixels);
903 if (image->previous == (Image *) NULL)
905 status=SetImageProgress(image,LoadImageTag,4,5);
906 if (status == MagickFalse)
910 (void) CloseBlob(image);
911 if (image->previous == (Image *) NULL)
913 status=SetImageProgress(image,LoadImageTag,5,5);
914 if (status == MagickFalse)
920 SetQuantumImageType(image,quantum_type);
922 Proceed to next image.
924 if (image_info->number_scenes != 0)
925 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
927 if (count == (ssize_t) length)
930 Allocate next image structure.
932 AcquireNextImage(image_info,image,exception);
933 if (GetNextImageInList(image) == (Image *) NULL)
935 image=DestroyImageList(image);
936 return((Image *) NULL);
938 image=SyncNextImageInList(image);
939 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
941 if (status == MagickFalse)
945 } while (count == (ssize_t) length);
946 quantum_info=DestroyQuantumInfo(quantum_info);
947 canvas_image=DestroyImage(canvas_image);
948 (void) CloseBlob(image);
949 return(GetFirstImageInList(image));
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957 % R e g i s t e r B G R I m a g e %
961 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963 % RegisterBGRImage() adds attributes for the BGR image format to
964 % the list of supported formats. The attributes include the image format
965 % tag, a method to read and/or write the format, whether the format
966 % supports the saving of more than one frame to the same file or blob,
967 % whether the format supports native in-memory I/O, and a brief
968 % description of the format.
970 % The format of the RegisterBGRImage method is:
972 % size_t RegisterBGRImage(void)
975 ModuleExport size_t RegisterBGRImage(void)
980 entry=SetMagickInfo("BGR");
981 entry->decoder=(DecodeImageHandler *) ReadBGRImage;
982 entry->encoder=(EncodeImageHandler *) WriteBGRImage;
983 entry->raw=MagickTrue;
984 entry->endian_support=MagickTrue;
985 entry->description=ConstantString("Raw blue, green, and red samples");
986 entry->module=ConstantString("BGR");
987 (void) RegisterMagickInfo(entry);
988 entry=SetMagickInfo("BGRA");
989 entry->decoder=(DecodeImageHandler *) ReadBGRImage;
990 entry->encoder=(EncodeImageHandler *) WriteBGRImage;
991 entry->raw=MagickTrue;
992 entry->endian_support=MagickTrue;
993 entry->description=ConstantString("Raw blue, green, red, and alpha samples");
994 entry->module=ConstantString("BGR");
995 (void) RegisterMagickInfo(entry);
996 return(MagickImageCoderSignature);
1000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1004 % U n r e g i s t e r B G R I m a g e %
1008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010 % UnregisterBGRImage() removes format registrations made by the BGR module
1011 % from the list of supported formats.
1013 % The format of the UnregisterBGRImage method is:
1015 % UnregisterBGRImage(void)
1018 ModuleExport void UnregisterBGRImage(void)
1020 (void) UnregisterMagickInfo("BGRA");
1021 (void) UnregisterMagickInfo("BGR");
1025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1029 % W r i t e B G R I m a g e %
1033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1035 % WriteBGRImage() writes an image to a file in the BGR or BGRA
1036 % rasterfile format.
1038 % The format of the WriteBGRImage method is:
1040 % MagickBooleanType WriteBGRImage(const ImageInfo *image_info,
1041 % Image *image,ExceptionInfo *exception)
1043 % A description of each parameter follows.
1045 % o image_info: the image info.
1047 % o image: The image.
1049 % o exception: return any errors or warnings in this structure.
1052 static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
1053 ExceptionInfo *exception)
1078 Allocate memory for pixels.
1080 assert(image_info != (const ImageInfo *) NULL);
1081 assert(image_info->signature == MagickSignature);
1082 assert(image != (Image *) NULL);
1083 assert(image->signature == MagickSignature);
1084 if (image->debug != MagickFalse)
1085 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1086 if (image_info->interlace != PartitionInterlace)
1089 Open output image file.
1091 assert(exception != (ExceptionInfo *) NULL);
1092 assert(exception->signature == MagickSignature);
1093 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1094 if (status == MagickFalse)
1097 quantum_type=BGRQuantum;
1098 if (LocaleCompare(image_info->magick,"BGRA") == 0)
1100 quantum_type=BGRAQuantum;
1101 image->matte=MagickTrue;
1107 Convert MIFF to BGR raster pixels.
1109 if (IsRGBColorspace(image->colorspace) == MagickFalse)
1110 (void) TransformImageColorspace(image,RGBColorspace,exception);
1111 if ((LocaleCompare(image_info->magick,"BGRA") == 0) &&
1112 (image->matte == MagickFalse))
1113 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1114 quantum_info=AcquireQuantumInfo(image_info,image);
1115 if (quantum_info == (QuantumInfo *) NULL)
1116 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1117 pixels=GetQuantumPixels(quantum_info);
1118 switch (image_info->interlace)
1124 No interlacing: BGRBGRBGRBGRBGRBGR...
1126 for (y=0; y < (ssize_t) image->rows; y++)
1128 register const Quantum
1131 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1132 if (p == (const Quantum *) NULL)
1134 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1135 quantum_type,pixels,exception);
1136 count=WriteBlob(image,length,pixels);
1137 if (count != (ssize_t) length)
1139 if (image->previous == (Image *) NULL)
1141 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1143 if (status == MagickFalse)
1152 Line interlacing: BBB...GGG...RRR...RRR...GGG...BBB...
1154 for (y=0; y < (ssize_t) image->rows; y++)
1156 register const Quantum
1159 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1160 if (p == (const Quantum *) NULL)
1162 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1163 BlueQuantum,pixels,exception);
1164 count=WriteBlob(image,length,pixels);
1165 if (count != (ssize_t) length)
1167 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1168 GreenQuantum,pixels,exception);
1169 count=WriteBlob(image,length,pixels);
1170 if (count != (ssize_t) length)
1172 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1173 RedQuantum,pixels,exception);
1174 count=WriteBlob(image,length,pixels);
1175 if (count != (ssize_t) length)
1177 if (quantum_type == BGRAQuantum)
1179 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1180 AlphaQuantum,pixels,exception);
1181 count=WriteBlob(image,length,pixels);
1182 if (count != (ssize_t) length)
1185 if (image->previous == (Image *) NULL)
1187 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1189 if (status == MagickFalse)
1195 case PlaneInterlace:
1198 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
1200 for (y=0; y < (ssize_t) image->rows; y++)
1202 register const Quantum
1205 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1206 if (p == (const Quantum *) NULL)
1208 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1209 RedQuantum,pixels,exception);
1210 count=WriteBlob(image,length,pixels);
1211 if (count != (ssize_t) length)
1214 if (image->previous == (Image *) NULL)
1216 status=SetImageProgress(image,SaveImageTag,1,6);
1217 if (status == MagickFalse)
1220 for (y=0; y < (ssize_t) image->rows; y++)
1222 register const Quantum
1225 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1226 if (p == (const Quantum *) NULL)
1228 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1229 GreenQuantum,pixels,exception);
1230 count=WriteBlob(image,length,pixels);
1231 if (count != (ssize_t) length)
1234 if (image->previous == (Image *) NULL)
1236 status=SetImageProgress(image,SaveImageTag,2,6);
1237 if (status == MagickFalse)
1240 for (y=0; y < (ssize_t) image->rows; y++)
1242 register const Quantum
1245 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1246 if (p == (const Quantum *) NULL)
1248 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1249 BlueQuantum,pixels,exception);
1250 count=WriteBlob(image,length,pixels);
1251 if (count != (ssize_t) length)
1254 if (image->previous == (Image *) NULL)
1256 status=SetImageProgress(image,SaveImageTag,3,6);
1257 if (status == MagickFalse)
1260 if (quantum_type == BGRAQuantum)
1262 for (y=0; y < (ssize_t) image->rows; y++)
1264 register const Quantum
1267 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1268 if (p == (const Quantum *) NULL)
1270 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1271 AlphaQuantum,pixels,exception);
1272 count=WriteBlob(image,length,pixels);
1273 if (count != (ssize_t) length)
1276 if (image->previous == (Image *) NULL)
1278 status=SetImageProgress(image,SaveImageTag,5,6);
1279 if (status == MagickFalse)
1283 if (image_info->interlace == PartitionInterlace)
1284 (void) CopyMagickString(image->filename,image_info->filename,
1286 if (image->previous == (Image *) NULL)
1288 status=SetImageProgress(image,SaveImageTag,6,6);
1289 if (status == MagickFalse)
1294 case PartitionInterlace:
1297 Partition interlacing: BBBBBB..., GGGGGG..., RRRRRR...
1299 AppendImageFormat("B",image->filename);
1300 status=OpenBlob(image_info,image,scene == 0 ? WriteBinaryBlobMode :
1301 AppendBinaryBlobMode,exception);
1302 if (status == MagickFalse)
1304 for (y=0; y < (ssize_t) image->rows; y++)
1306 register const Quantum
1309 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1310 if (p == (const Quantum *) NULL)
1312 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1313 BlueQuantum,pixels,exception);
1314 count=WriteBlob(image,length,pixels);
1315 if (count != (ssize_t) length)
1318 if (image->previous == (Image *) NULL)
1320 status=SetImageProgress(image,SaveImageTag,1,6);
1321 if (status == MagickFalse)
1324 (void) CloseBlob(image);
1325 AppendImageFormat("G",image->filename);
1326 status=OpenBlob(image_info,image,scene == 0 ? WriteBinaryBlobMode :
1327 AppendBinaryBlobMode,exception);
1328 if (status == MagickFalse)
1330 for (y=0; y < (ssize_t) image->rows; y++)
1332 register const Quantum
1335 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1336 if (p == (const Quantum *) NULL)
1338 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1339 GreenQuantum,pixels,exception);
1340 count=WriteBlob(image,length,pixels);
1341 if (count != (ssize_t) length)
1344 if (image->previous == (Image *) NULL)
1346 status=SetImageProgress(image,SaveImageTag,2,6);
1347 if (status == MagickFalse)
1350 (void) CloseBlob(image);
1351 AppendImageFormat("R",image->filename);
1352 status=OpenBlob(image_info,image,scene == 0 ? WriteBinaryBlobMode :
1353 AppendBinaryBlobMode,exception);
1354 if (status == MagickFalse)
1356 for (y=0; y < (ssize_t) image->rows; y++)
1358 register const Quantum
1361 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1362 if (p == (const Quantum *) NULL)
1364 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1365 RedQuantum,pixels,exception);
1366 count=WriteBlob(image,length,pixels);
1367 if (count != (ssize_t) length)
1370 if (image->previous == (Image *) NULL)
1372 status=SetImageProgress(image,SaveImageTag,3,6);
1373 if (status == MagickFalse)
1376 (void) CloseBlob(image);
1377 if (quantum_type == BGRAQuantum)
1379 (void) CloseBlob(image);
1380 AppendImageFormat("A",image->filename);
1381 status=OpenBlob(image_info,image,scene == 0 ? WriteBinaryBlobMode :
1382 AppendBinaryBlobMode,exception);
1383 if (status == MagickFalse)
1385 for (y=0; y < (ssize_t) image->rows; y++)
1387 register const Quantum
1390 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1391 if (p == (const Quantum *) NULL)
1393 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1394 AlphaQuantum,pixels,exception);
1395 count=WriteBlob(image,length,pixels);
1396 if (count != (ssize_t) length)
1399 if (image->previous == (Image *) NULL)
1401 status=SetImageProgress(image,SaveImageTag,5,6);
1402 if (status == MagickFalse)
1406 (void) CloseBlob(image);
1407 (void) CopyMagickString(image->filename,image_info->filename,
1409 if (image->previous == (Image *) NULL)
1411 status=SetImageProgress(image,SaveImageTag,6,6);
1412 if (status == MagickFalse)
1418 quantum_info=DestroyQuantumInfo(quantum_info);
1419 if (GetNextImageInList(image) == (Image *) NULL)
1421 image=SyncNextImageInList(image);
1422 status=SetImageProgress(image,SaveImagesTag,scene++,
1423 GetImageListLength(image));
1424 if (status == MagickFalse)
1426 } while (image_info->adjoin != MagickFalse);
1427 (void) CloseBlob(image);