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,
164 quantum_info=AcquireQuantumInfo(image_info,canvas_image);
165 if (quantum_info == (QuantumInfo *) NULL)
166 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
167 pixels=GetQuantumPixels(quantum_info);
168 quantum_type=BGRQuantum;
169 if (LocaleCompare(image_info->magick,"BGRA") == 0)
171 quantum_type=BGRAQuantum;
172 image->matte=MagickTrue;
174 if (image_info->number_scenes != 0)
175 while (image->scene < image_info->scene)
181 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
182 for (y=0; y < (ssize_t) image->rows; y++)
184 count=ReadBlob(image,length,pixels);
185 if (count != (ssize_t) length)
195 Read pixels to virtual canvas image then push to image.
197 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
198 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
200 image->colorspace=RGBColorspace;
201 switch (image_info->interlace)
207 No interlacing: BGRBGRBGRBGRBGRBGR...
211 length=GetQuantumExtent(canvas_image,quantum_info,quantum_type);
212 count=ReadBlob(image,length,pixels);
214 for (y=0; y < (ssize_t) image->extract_info.height; y++)
216 register const Quantum
225 if (count != (ssize_t) length)
227 ThrowFileException(exception,CorruptImageError,
228 "UnexpectedEndOfFile",image->filename);
231 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
233 if (q == (Quantum *) NULL)
235 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
236 quantum_info,quantum_type,pixels,exception);
237 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
239 if (((y-image->extract_info.y) >= 0) &&
240 ((y-image->extract_info.y) < (ssize_t) image->rows))
242 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
243 canvas_image->columns,1,exception);
244 q=QueueAuthenticPixels(image,0,y-image->extract_info.y,
245 image->columns,1,exception);
246 if ((p == (const Quantum *) NULL) ||
247 (q == (Quantum *) NULL))
249 for (x=0; x < (ssize_t) image->columns; x++)
251 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
252 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
253 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
254 SetPixelAlpha(image,OpaqueAlpha,q);
255 if (image->matte != MagickFalse)
256 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
257 p+=GetPixelChannels(canvas_image);
258 q+=GetPixelChannels(image);
260 if (SyncAuthenticPixels(image,exception) == MagickFalse)
263 if (image->previous == (Image *) NULL)
265 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
267 if (status == MagickFalse)
270 count=ReadBlob(image,length,pixels);
286 Line interlacing: BBB...GGG...RRR...RRR...GGG...BBB...
290 length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
291 count=ReadBlob(image,length,pixels);
293 for (y=0; y < (ssize_t) image->extract_info.height; y++)
295 register const Quantum
304 if (count != (ssize_t) length)
306 ThrowFileException(exception,CorruptImageError,
307 "UnexpectedEndOfFile",image->filename);
310 for (i=0; i < (ssize_t) (image->matte != MagickFalse ? 4 : 3); i++)
312 quantum_type=quantum_types[i];
313 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
315 if (q == (Quantum *) NULL)
317 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
318 quantum_info,quantum_type,pixels,exception);
319 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
321 if (((y-image->extract_info.y) >= 0) &&
322 ((y-image->extract_info.y) < (ssize_t) image->rows))
324 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
325 canvas_image->columns,1,exception);
326 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
327 image->columns,1,exception);
328 if ((p == (const Quantum *) NULL) ||
329 (q == (Quantum *) NULL))
331 for (x=0; x < (ssize_t) image->columns; x++)
333 switch (quantum_type)
337 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
342 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
347 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
352 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
357 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
363 p+=GetPixelChannels(canvas_image);
364 q+=GetPixelChannels(image);
366 if (SyncAuthenticPixels(image,exception) == MagickFalse)
369 count=ReadBlob(image,length,pixels);
371 if (image->previous == (Image *) NULL)
373 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
375 if (status == MagickFalse)
384 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
388 length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
389 count=ReadBlob(image,length,pixels);
391 for (y=0; y < (ssize_t) image->extract_info.height; y++)
393 register const Quantum
402 if (count != (ssize_t) length)
404 ThrowFileException(exception,CorruptImageError,
405 "UnexpectedEndOfFile",image->filename);
408 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
410 if (q == (Quantum *) NULL)
412 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
413 quantum_info,RedQuantum,pixels,exception);
414 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
416 if (((y-image->extract_info.y) >= 0) &&
417 ((y-image->extract_info.y) < (ssize_t) image->rows))
419 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
420 canvas_image->columns,1,exception);
421 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
422 image->columns,1,exception);
423 if ((p == (const Quantum *) NULL) ||
424 (q == (Quantum *) NULL))
426 for (x=0; x < (ssize_t) image->columns; x++)
428 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
429 p+=GetPixelChannels(canvas_image);
430 q+=GetPixelChannels(image);
432 if (SyncAuthenticPixels(image,exception) == MagickFalse)
435 count=ReadBlob(image,length,pixels);
437 if (image->previous == (Image *) NULL)
439 status=SetImageProgress(image,LoadImageTag,1,6);
440 if (status == MagickFalse)
443 for (y=0; y < (ssize_t) image->extract_info.height; y++)
445 register const Quantum
454 if (count != (ssize_t) length)
456 ThrowFileException(exception,CorruptImageError,
457 "UnexpectedEndOfFile",image->filename);
460 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
462 if (q == (Quantum *) NULL)
464 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
465 quantum_info,GreenQuantum,pixels,exception);
466 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
468 if (((y-image->extract_info.y) >= 0) &&
469 ((y-image->extract_info.y) < (ssize_t) image->rows))
471 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
472 canvas_image->columns,1,exception);
473 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
474 image->columns,1,exception);
475 if ((p == (const Quantum *) NULL) ||
476 (q == (Quantum *) NULL))
478 for (x=0; x < (ssize_t) image->columns; x++)
480 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
481 p+=GetPixelChannels(canvas_image);
482 q+=GetPixelChannels(image);
484 if (SyncAuthenticPixels(image,exception) == MagickFalse)
487 count=ReadBlob(image,length,pixels);
489 if (image->previous == (Image *) NULL)
491 status=SetImageProgress(image,LoadImageTag,2,6);
492 if (status == MagickFalse)
495 for (y=0; y < (ssize_t) image->extract_info.height; y++)
497 register const Quantum
506 if (count != (ssize_t) length)
508 ThrowFileException(exception,CorruptImageError,
509 "UnexpectedEndOfFile",image->filename);
512 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
514 if (q == (Quantum *) NULL)
516 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
517 quantum_info,BlueQuantum,pixels,exception);
518 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
520 if (((y-image->extract_info.y) >= 0) &&
521 ((y-image->extract_info.y) < (ssize_t) image->rows))
523 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
524 canvas_image->columns,1,exception);
525 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
526 image->columns,1,exception);
527 if ((p == (const Quantum *) NULL) ||
528 (q == (Quantum *) NULL))
530 for (x=0; x < (ssize_t) image->columns; x++)
532 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
533 p+=GetPixelChannels(canvas_image);
534 q+=GetPixelChannels(image);
536 if (SyncAuthenticPixels(image,exception) == MagickFalse)
539 count=ReadBlob(image,length,pixels);
541 if (image->previous == (Image *) NULL)
543 status=SetImageProgress(image,LoadImageTag,3,6);
544 if (status == MagickFalse)
547 if (image->previous == (Image *) NULL)
549 status=SetImageProgress(image,LoadImageTag,4,6);
550 if (status == MagickFalse)
553 if (image->matte != MagickFalse)
555 for (y=0; y < (ssize_t) image->extract_info.height; y++)
557 register const Quantum
566 if (count != (ssize_t) length)
568 ThrowFileException(exception,CorruptImageError,
569 "UnexpectedEndOfFile",image->filename);
572 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
574 if (q == (Quantum *) NULL)
576 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
577 quantum_info,AlphaQuantum,pixels,exception);
578 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
580 if (((y-image->extract_info.y) >= 0) &&
581 ((y-image->extract_info.y) < (ssize_t) image->rows))
583 p=GetVirtualPixels(canvas_image,
584 canvas_image->extract_info.x,0,canvas_image->columns,1,
586 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
587 image->columns,1,exception);
588 if ((p == (const Quantum *) NULL) ||
589 (q == (Quantum *) NULL))
591 for (x=0; x < (ssize_t) image->columns; x++)
593 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
594 p+=GetPixelChannels(canvas_image);
595 q+=GetPixelChannels(image);
597 if (SyncAuthenticPixels(image,exception) == MagickFalse)
600 count=ReadBlob(image,length,pixels);
602 if (image->previous == (Image *) NULL)
604 status=SetImageProgress(image,LoadImageTag,5,6);
605 if (status == MagickFalse)
609 if (image->previous == (Image *) NULL)
611 status=SetImageProgress(image,LoadImageTag,6,6);
612 if (status == MagickFalse)
617 case PartitionInterlace:
620 Partition interlacing: BBBBBB..., GGGGGG..., RRRRRR...
622 AppendImageFormat("B",image->filename);
623 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
624 if (status == MagickFalse)
626 canvas_image=DestroyImageList(canvas_image);
627 image=DestroyImageList(image);
628 return((Image *) NULL);
630 if (DiscardBlobBytes(image,image->offset) == MagickFalse)
631 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
633 length=GetQuantumExtent(canvas_image,quantum_info,BlueQuantum);
634 for (i=0; i < (ssize_t) scene; i++)
635 for (y=0; y < (ssize_t) image->extract_info.height; y++)
636 if (ReadBlob(image,length,pixels) != (ssize_t) length)
638 ThrowFileException(exception,CorruptImageError,
639 "UnexpectedEndOfFile",image->filename);
642 count=ReadBlob(image,length,pixels);
643 for (y=0; y < (ssize_t) image->extract_info.height; y++)
645 register const Quantum
654 if (count != (ssize_t) length)
656 ThrowFileException(exception,CorruptImageError,
657 "UnexpectedEndOfFile",image->filename);
660 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
662 if (q == (Quantum *) NULL)
664 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
665 quantum_info,BlueQuantum,pixels,exception);
666 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
668 if (((y-image->extract_info.y) >= 0) &&
669 ((y-image->extract_info.y) < (ssize_t) image->rows))
671 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
672 canvas_image->columns,1,exception);
673 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
674 image->columns,1,exception);
675 if ((p == (const Quantum *) NULL) ||
676 (q == (Quantum *) NULL))
678 for (x=0; x < (ssize_t) image->columns; x++)
680 SetPixelRed(image,GetPixelRed(canvas_image,p),q);
681 p+=GetPixelChannels(canvas_image);
682 q+=GetPixelChannels(image);
684 if (SyncAuthenticPixels(image,exception) == MagickFalse)
687 count=ReadBlob(image,length,pixels);
689 if (image->previous == (Image *) NULL)
691 status=SetImageProgress(image,LoadImageTag,1,5);
692 if (status == MagickFalse)
695 (void) CloseBlob(image);
696 AppendImageFormat("G",image->filename);
697 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
698 if (status == MagickFalse)
700 canvas_image=DestroyImageList(canvas_image);
701 image=DestroyImageList(image);
702 return((Image *) NULL);
704 length=GetQuantumExtent(canvas_image,quantum_info,GreenQuantum);
705 for (i=0; i < (ssize_t) scene; i++)
706 for (y=0; y < (ssize_t) image->extract_info.height; y++)
707 if (ReadBlob(image,length,pixels) != (ssize_t) length)
709 ThrowFileException(exception,CorruptImageError,
710 "UnexpectedEndOfFile",image->filename);
713 count=ReadBlob(image,length,pixels);
714 for (y=0; y < (ssize_t) image->extract_info.height; y++)
716 register const Quantum
725 if (count != (ssize_t) length)
727 ThrowFileException(exception,CorruptImageError,
728 "UnexpectedEndOfFile",image->filename);
731 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
733 if (q == (Quantum *) NULL)
735 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
736 quantum_info,GreenQuantum,pixels,exception);
737 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
739 if (((y-image->extract_info.y) >= 0) &&
740 ((y-image->extract_info.y) < (ssize_t) image->rows))
742 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
743 canvas_image->columns,1,exception);
744 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
745 image->columns,1,exception);
746 if ((p == (const Quantum *) NULL) ||
747 (q == (Quantum *) NULL))
749 for (x=0; x < (ssize_t) image->columns; x++)
751 SetPixelGreen(image,GetPixelGreen(canvas_image,p),q);
752 p+=GetPixelChannels(canvas_image);
753 q+=GetPixelChannels(image);
755 if (SyncAuthenticPixels(image,exception) == MagickFalse)
758 count=ReadBlob(image,length,pixels);
760 if (image->previous == (Image *) NULL)
762 status=SetImageProgress(image,LoadImageTag,2,5);
763 if (status == MagickFalse)
766 (void) CloseBlob(image);
767 AppendImageFormat("R",image->filename);
768 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
769 if (status == MagickFalse)
771 canvas_image=DestroyImageList(canvas_image);
772 image=DestroyImageList(image);
773 return((Image *) NULL);
775 length=GetQuantumExtent(canvas_image,quantum_info,RedQuantum);
776 for (i=0; i < (ssize_t) scene; i++)
777 for (y=0; y < (ssize_t) image->extract_info.height; y++)
778 if (ReadBlob(image,length,pixels) != (ssize_t) length)
780 ThrowFileException(exception,CorruptImageError,
781 "UnexpectedEndOfFile",image->filename);
784 count=ReadBlob(image,length,pixels);
785 for (y=0; y < (ssize_t) image->extract_info.height; y++)
787 register const Quantum
796 if (count != (ssize_t) length)
798 ThrowFileException(exception,CorruptImageError,
799 "UnexpectedEndOfFile",image->filename);
802 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
804 if (q == (Quantum *) NULL)
806 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
807 quantum_info,RedQuantum,pixels,exception);
808 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
810 if (((y-image->extract_info.y) >= 0) &&
811 ((y-image->extract_info.y) < (ssize_t) image->rows))
813 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,0,
814 canvas_image->columns,1,exception);
815 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
816 image->columns,1,exception);
817 if ((p == (const Quantum *) NULL) ||
818 (q == (Quantum *) NULL))
820 for (x=0; x < (ssize_t) image->columns; x++)
822 SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
823 p+=GetPixelChannels(canvas_image);
824 q+=GetPixelChannels(image);
826 if (SyncAuthenticPixels(image,exception) == MagickFalse)
829 count=ReadBlob(image,length,pixels);
831 if (image->previous == (Image *) NULL)
833 status=SetImageProgress(image,LoadImageTag,3,5);
834 if (status == MagickFalse)
837 if (image->matte != MagickFalse)
839 (void) CloseBlob(image);
840 AppendImageFormat("A",image->filename);
841 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
842 if (status == MagickFalse)
844 canvas_image=DestroyImageList(canvas_image);
845 image=DestroyImageList(image);
846 return((Image *) NULL);
848 length=GetQuantumExtent(canvas_image,quantum_info,AlphaQuantum);
849 for (i=0; i < (ssize_t) scene; i++)
850 for (y=0; y < (ssize_t) image->extract_info.height; y++)
851 if (ReadBlob(image,length,pixels) != (ssize_t) length)
853 ThrowFileException(exception,CorruptImageError,
854 "UnexpectedEndOfFile",image->filename);
857 count=ReadBlob(image,length,pixels);
858 for (y=0; y < (ssize_t) image->extract_info.height; y++)
860 register const Quantum
869 if (count != (ssize_t) length)
871 ThrowFileException(exception,CorruptImageError,
872 "UnexpectedEndOfFile",image->filename);
875 q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
877 if (q == (Quantum *) NULL)
879 length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
880 quantum_info,BlueQuantum,pixels,exception);
881 if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
883 if (((y-image->extract_info.y) >= 0) &&
884 ((y-image->extract_info.y) < (ssize_t) image->rows))
886 p=GetVirtualPixels(canvas_image,canvas_image->extract_info.x,
887 0,canvas_image->columns,1,exception);
888 q=GetAuthenticPixels(image,0,y-image->extract_info.y,
889 image->columns,1,exception);
890 if ((p == (const Quantum *) NULL) ||
891 (q == (Quantum *) NULL))
893 for (x=0; x < (ssize_t) image->columns; x++)
895 SetPixelAlpha(image,GetPixelAlpha(canvas_image,p),q);
896 p+=GetPixelChannels(canvas_image);
897 q+=GetPixelChannels(image);
899 if (SyncAuthenticPixels(image,exception) == MagickFalse)
902 count=ReadBlob(image,length,pixels);
904 if (image->previous == (Image *) NULL)
906 status=SetImageProgress(image,LoadImageTag,4,5);
907 if (status == MagickFalse)
911 (void) CloseBlob(image);
912 if (image->previous == (Image *) NULL)
914 status=SetImageProgress(image,LoadImageTag,5,5);
915 if (status == MagickFalse)
921 SetQuantumImageType(image,quantum_type);
923 Proceed to next image.
925 if (image_info->number_scenes != 0)
926 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
928 if (count == (ssize_t) length)
931 Allocate next image structure.
933 AcquireNextImage(image_info,image,exception);
934 if (GetNextImageInList(image) == (Image *) NULL)
936 image=DestroyImageList(image);
937 return((Image *) NULL);
939 image=SyncNextImageInList(image);
940 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
942 if (status == MagickFalse)
946 } while (count == (ssize_t) length);
947 quantum_info=DestroyQuantumInfo(quantum_info);
948 canvas_image=DestroyImage(canvas_image);
949 (void) CloseBlob(image);
950 return(GetFirstImageInList(image));
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
958 % R e g i s t e r B G R I m a g e %
962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964 % RegisterBGRImage() adds attributes for the BGR image format to
965 % the list of supported formats. The attributes include the image format
966 % tag, a method to read and/or write the format, whether the format
967 % supports the saving of more than one frame to the same file or blob,
968 % whether the format supports native in-memory I/O, and a brief
969 % description of the format.
971 % The format of the RegisterBGRImage method is:
973 % size_t RegisterBGRImage(void)
976 ModuleExport size_t RegisterBGRImage(void)
981 entry=SetMagickInfo("BGR");
982 entry->decoder=(DecodeImageHandler *) ReadBGRImage;
983 entry->encoder=(EncodeImageHandler *) WriteBGRImage;
984 entry->raw=MagickTrue;
985 entry->endian_support=MagickTrue;
986 entry->description=ConstantString("Raw blue, green, and red samples");
987 entry->module=ConstantString("BGR");
988 (void) RegisterMagickInfo(entry);
989 entry=SetMagickInfo("BGRA");
990 entry->decoder=(DecodeImageHandler *) ReadBGRImage;
991 entry->encoder=(EncodeImageHandler *) WriteBGRImage;
992 entry->raw=MagickTrue;
993 entry->endian_support=MagickTrue;
994 entry->description=ConstantString("Raw blue, green, red, and alpha samples");
995 entry->module=ConstantString("BGR");
996 (void) RegisterMagickInfo(entry);
997 return(MagickImageCoderSignature);
1001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005 % U n r e g i s t e r B G R I m a g e %
1009 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1011 % UnregisterBGRImage() removes format registrations made by the BGR module
1012 % from the list of supported formats.
1014 % The format of the UnregisterBGRImage method is:
1016 % UnregisterBGRImage(void)
1019 ModuleExport void UnregisterBGRImage(void)
1021 (void) UnregisterMagickInfo("BGRA");
1022 (void) UnregisterMagickInfo("BGR");
1026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1030 % W r i t e B G R I m a g e %
1034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1036 % WriteBGRImage() writes an image to a file in the BGR or BGRA
1037 % rasterfile format.
1039 % The format of the WriteBGRImage method is:
1041 % MagickBooleanType WriteBGRImage(const ImageInfo *image_info,
1042 % Image *image,ExceptionInfo *exception)
1044 % A description of each parameter follows.
1046 % o image_info: the image info.
1048 % o image: The image.
1050 % o exception: return any errors or warnings in this structure.
1053 static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
1054 ExceptionInfo *exception)
1079 Allocate memory for pixels.
1081 assert(image_info != (const ImageInfo *) NULL);
1082 assert(image_info->signature == MagickSignature);
1083 assert(image != (Image *) NULL);
1084 assert(image->signature == MagickSignature);
1085 if (image->debug != MagickFalse)
1086 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1087 if (image_info->interlace != PartitionInterlace)
1090 Open output image file.
1092 assert(exception != (ExceptionInfo *) NULL);
1093 assert(exception->signature == MagickSignature);
1094 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1095 if (status == MagickFalse)
1098 quantum_type=BGRQuantum;
1099 if (LocaleCompare(image_info->magick,"BGRA") == 0)
1101 quantum_type=BGRAQuantum;
1102 image->matte=MagickTrue;
1108 Convert MIFF to BGR raster pixels.
1110 if (IsRGBColorspace(image->colorspace) == MagickFalse)
1111 (void) TransformImageColorspace(image,sRGBColorspace,exception);
1112 if ((LocaleCompare(image_info->magick,"BGRA") == 0) &&
1113 (image->matte == MagickFalse))
1114 (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
1115 quantum_info=AcquireQuantumInfo(image_info,image);
1116 if (quantum_info == (QuantumInfo *) NULL)
1117 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
1118 pixels=GetQuantumPixels(quantum_info);
1119 switch (image_info->interlace)
1125 No interlacing: BGRBGRBGRBGRBGRBGR...
1127 for (y=0; y < (ssize_t) image->rows; y++)
1129 register const Quantum
1132 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1133 if (p == (const Quantum *) NULL)
1135 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1136 quantum_type,pixels,exception);
1137 count=WriteBlob(image,length,pixels);
1138 if (count != (ssize_t) length)
1140 if (image->previous == (Image *) NULL)
1142 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1144 if (status == MagickFalse)
1153 Line interlacing: BBB...GGG...RRR...RRR...GGG...BBB...
1155 for (y=0; y < (ssize_t) image->rows; y++)
1157 register const Quantum
1160 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1161 if (p == (const Quantum *) NULL)
1163 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1164 BlueQuantum,pixels,exception);
1165 count=WriteBlob(image,length,pixels);
1166 if (count != (ssize_t) length)
1168 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1169 GreenQuantum,pixels,exception);
1170 count=WriteBlob(image,length,pixels);
1171 if (count != (ssize_t) length)
1173 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1174 RedQuantum,pixels,exception);
1175 count=WriteBlob(image,length,pixels);
1176 if (count != (ssize_t) length)
1178 if (quantum_type == BGRAQuantum)
1180 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1181 AlphaQuantum,pixels,exception);
1182 count=WriteBlob(image,length,pixels);
1183 if (count != (ssize_t) length)
1186 if (image->previous == (Image *) NULL)
1188 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1190 if (status == MagickFalse)
1196 case PlaneInterlace:
1199 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
1201 for (y=0; y < (ssize_t) image->rows; y++)
1203 register const Quantum
1206 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1207 if (p == (const Quantum *) NULL)
1209 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1210 RedQuantum,pixels,exception);
1211 count=WriteBlob(image,length,pixels);
1212 if (count != (ssize_t) length)
1215 if (image->previous == (Image *) NULL)
1217 status=SetImageProgress(image,SaveImageTag,1,6);
1218 if (status == MagickFalse)
1221 for (y=0; y < (ssize_t) image->rows; y++)
1223 register const Quantum
1226 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1227 if (p == (const Quantum *) NULL)
1229 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1230 GreenQuantum,pixels,exception);
1231 count=WriteBlob(image,length,pixels);
1232 if (count != (ssize_t) length)
1235 if (image->previous == (Image *) NULL)
1237 status=SetImageProgress(image,SaveImageTag,2,6);
1238 if (status == MagickFalse)
1241 for (y=0; y < (ssize_t) image->rows; y++)
1243 register const Quantum
1246 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1247 if (p == (const Quantum *) NULL)
1249 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1250 BlueQuantum,pixels,exception);
1251 count=WriteBlob(image,length,pixels);
1252 if (count != (ssize_t) length)
1255 if (image->previous == (Image *) NULL)
1257 status=SetImageProgress(image,SaveImageTag,3,6);
1258 if (status == MagickFalse)
1261 if (quantum_type == BGRAQuantum)
1263 for (y=0; y < (ssize_t) image->rows; y++)
1265 register const Quantum
1268 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1269 if (p == (const Quantum *) NULL)
1271 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1272 AlphaQuantum,pixels,exception);
1273 count=WriteBlob(image,length,pixels);
1274 if (count != (ssize_t) length)
1277 if (image->previous == (Image *) NULL)
1279 status=SetImageProgress(image,SaveImageTag,5,6);
1280 if (status == MagickFalse)
1284 if (image_info->interlace == PartitionInterlace)
1285 (void) CopyMagickString(image->filename,image_info->filename,
1287 if (image->previous == (Image *) NULL)
1289 status=SetImageProgress(image,SaveImageTag,6,6);
1290 if (status == MagickFalse)
1295 case PartitionInterlace:
1298 Partition interlacing: BBBBBB..., GGGGGG..., RRRRRR...
1300 AppendImageFormat("B",image->filename);
1301 status=OpenBlob(image_info,image,scene == 0 ? WriteBinaryBlobMode :
1302 AppendBinaryBlobMode,exception);
1303 if (status == MagickFalse)
1305 for (y=0; y < (ssize_t) image->rows; y++)
1307 register const Quantum
1310 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1311 if (p == (const Quantum *) NULL)
1313 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1314 BlueQuantum,pixels,exception);
1315 count=WriteBlob(image,length,pixels);
1316 if (count != (ssize_t) length)
1319 if (image->previous == (Image *) NULL)
1321 status=SetImageProgress(image,SaveImageTag,1,6);
1322 if (status == MagickFalse)
1325 (void) CloseBlob(image);
1326 AppendImageFormat("G",image->filename);
1327 status=OpenBlob(image_info,image,scene == 0 ? WriteBinaryBlobMode :
1328 AppendBinaryBlobMode,exception);
1329 if (status == MagickFalse)
1331 for (y=0; y < (ssize_t) image->rows; y++)
1333 register const Quantum
1336 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1337 if (p == (const Quantum *) NULL)
1339 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1340 GreenQuantum,pixels,exception);
1341 count=WriteBlob(image,length,pixels);
1342 if (count != (ssize_t) length)
1345 if (image->previous == (Image *) NULL)
1347 status=SetImageProgress(image,SaveImageTag,2,6);
1348 if (status == MagickFalse)
1351 (void) CloseBlob(image);
1352 AppendImageFormat("R",image->filename);
1353 status=OpenBlob(image_info,image,scene == 0 ? WriteBinaryBlobMode :
1354 AppendBinaryBlobMode,exception);
1355 if (status == MagickFalse)
1357 for (y=0; y < (ssize_t) image->rows; y++)
1359 register const Quantum
1362 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1363 if (p == (const Quantum *) NULL)
1365 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1366 RedQuantum,pixels,exception);
1367 count=WriteBlob(image,length,pixels);
1368 if (count != (ssize_t) length)
1371 if (image->previous == (Image *) NULL)
1373 status=SetImageProgress(image,SaveImageTag,3,6);
1374 if (status == MagickFalse)
1377 (void) CloseBlob(image);
1378 if (quantum_type == BGRAQuantum)
1380 (void) CloseBlob(image);
1381 AppendImageFormat("A",image->filename);
1382 status=OpenBlob(image_info,image,scene == 0 ? WriteBinaryBlobMode :
1383 AppendBinaryBlobMode,exception);
1384 if (status == MagickFalse)
1386 for (y=0; y < (ssize_t) image->rows; y++)
1388 register const Quantum
1391 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1392 if (p == (const Quantum *) NULL)
1394 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1395 AlphaQuantum,pixels,exception);
1396 count=WriteBlob(image,length,pixels);
1397 if (count != (ssize_t) length)
1400 if (image->previous == (Image *) NULL)
1402 status=SetImageProgress(image,SaveImageTag,5,6);
1403 if (status == MagickFalse)
1407 (void) CloseBlob(image);
1408 (void) CopyMagickString(image->filename,image_info->filename,
1410 if (image->previous == (Image *) NULL)
1412 status=SetImageProgress(image,SaveImageTag,6,6);
1413 if (status == MagickFalse)
1419 quantum_info=DestroyQuantumInfo(quantum_info);
1420 if (GetNextImageInList(image) == (Image *) NULL)
1422 image=SyncNextImageInList(image);
1423 status=SetImageProgress(image,SaveImagesTag,scene++,
1424 GetImageListLength(image));
1425 if (status == MagickFalse)
1427 } while (image_info->adjoin != MagickFalse);
1428 (void) CloseBlob(image);