2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % V V IIIII PPPP SSSSS %
13 % Read/Write VIPS Image Format %
20 % Copyright 1999-2018 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 % https://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/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/colorspace.h"
48 #include "MagickCore/colorspace-private.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/property.h"
60 #include "MagickCore/quantum-private.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
68 #define VIPS_MAGIC_LSB 0x08f2a6b6U
69 #define VIPS_MAGIC_MSB 0xb6a6f208U
73 VIPSBandFormatNOTSET = -1,
74 VIPSBandFormatUCHAR = 0, /* Unsigned 8-bit int */
75 VIPSBandFormatCHAR = 1, /* Signed 8-bit int */
76 VIPSBandFormatUSHORT = 2, /* Unsigned 16-bit int */
77 VIPSBandFormatSHORT = 3, /* Signed 16-bit int */
78 VIPSBandFormatUINT = 4, /* Unsigned 32-bit int */
79 VIPSBandFormatINT = 5, /* Signed 32-bit int */
80 VIPSBandFormatFLOAT = 6, /* 32-bit IEEE float */
81 VIPSBandFormatCOMPLEX = 7, /* Complex (2 floats) */
82 VIPSBandFormatDOUBLE = 8, /* 64-bit IEEE double */
83 VIPSBandFormatDPCOMPLEX = 9 /* Complex (2 doubles) */
88 VIPSCodingNONE = 0, /* VIPS computation format */
89 VIPSCodingLABQ = 2, /* LABQ storage format */
90 VIPSCodingRAD = 6 /* Radiance storage format */
95 VIPSTypeMULTIBAND = 0, /* Some multiband image */
96 VIPSTypeB_W = 1, /* Some single band image */
97 VIPSTypeHISTOGRAM = 10, /* Histogram or LUT */
98 VIPSTypeFOURIER = 24, /* Image in Fourier space */
99 VIPSTypeXYZ = 12, /* CIE XYZ colour space */
100 VIPSTypeLAB = 13, /* CIE LAB colour space */
101 VIPSTypeCMYK = 15, /* im_icc_export() */
102 VIPSTypeLABQ = 16, /* 32-bit CIE LAB */
103 VIPSTypeRGB = 17, /* Some RGB */
104 VIPSTypeUCS = 18, /* UCS(1:1) colour space */
105 VIPSTypeLCH = 19, /* CIE LCh colour space */
106 VIPSTypeLABS = 21, /* 48-bit CIE LAB */
107 VIPSTypesRGB = 22, /* sRGB colour space */
108 VIPSTypeYXY = 23, /* CIE Yxy colour space */
109 VIPSTypeRGB16 = 25, /* 16-bit RGB */
110 VIPSTypeGREY16 = 26 /* 16-bit monochrome */
114 Forward declarations.
116 static MagickBooleanType
117 WriteVIPSImage(const ImageInfo *,Image *,ExceptionInfo *);
\f
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % IsVIPS() returns MagickTrue if the image format type, identified by the
131 % magick string, is VIPS.
133 % The format of the IsVIPS method is:
135 % MagickBooleanType IsVIPS(const unsigned char *magick,const size_t length)
137 % A description of each parameter follows:
139 % o magick: compare image format pattern against these bytes.
141 % o length: Specifies the length of the magick string.
144 static MagickBooleanType IsVIPS(const unsigned char *magick,const size_t length)
149 if (memcmp(magick,"\010\362\246\266",4) == 0)
152 if (memcmp(magick,"\266\246\362\010",4) == 0)
159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 % R e a d V I P S I m a g e %
167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 % ReadVIPSImage() reads a VIPS image file and returns it. It allocates the
170 % memory necessary for the new Image structure and returns a pointer to the
173 % The format of the ReadVIPSImage method is:
175 % Image *ReadVIPSmage(const ImageInfo *image_info,ExceptionInfo *exception)
177 % A description of each parameter follows:
179 % o image_info: the image info.
181 % o exception: return any errors or warnings in this structure.
185 static inline MagickBooleanType IsSupportedCombination(
186 const VIPSBandFormat format,const VIPSType type)
199 case VIPSBandFormatUSHORT:
200 case VIPSBandFormatSHORT:
201 case VIPSBandFormatUINT:
202 case VIPSBandFormatINT:
203 case VIPSBandFormatFLOAT:
204 case VIPSBandFormatDOUBLE:
214 static inline Quantum ReadVIPSPixelNONE(Image *image,
215 const VIPSBandFormat format,const VIPSType type)
227 case VIPSBandFormatUCHAR:
228 case VIPSBandFormatCHAR:
229 c=(unsigned char) ReadBlobByte(image);
231 case VIPSBandFormatUSHORT:
232 case VIPSBandFormatSHORT:
233 c=(unsigned char) ReadBlobShort(image);
235 case VIPSBandFormatUINT:
236 case VIPSBandFormatINT:
237 c=(unsigned char) ReadBlobLong(image);
239 case VIPSBandFormatFLOAT:
240 c=(unsigned char) ReadBlobFloat(image);
242 case VIPSBandFormatDOUBLE:
243 c=(unsigned char) ReadBlobDouble(image);
249 return(ScaleCharToQuantum(c));
259 case VIPSBandFormatUSHORT:
260 case VIPSBandFormatSHORT:
261 s=(unsigned short) ReadBlobShort(image);
263 case VIPSBandFormatUINT:
264 case VIPSBandFormatINT:
265 s=(unsigned short) ReadBlobLong(image);
267 case VIPSBandFormatFLOAT:
268 s=(unsigned short) ReadBlobFloat(image);
270 case VIPSBandFormatDOUBLE:
271 s=(unsigned short) ReadBlobDouble(image);
277 return(ScaleShortToQuantum(s));
283 case VIPSBandFormatUCHAR:
284 case VIPSBandFormatCHAR:
285 return(ScaleCharToQuantum((unsigned char) ReadBlobByte(image)));
286 case VIPSBandFormatUSHORT:
287 case VIPSBandFormatSHORT:
288 return(ScaleShortToQuantum(ReadBlobShort(image)));
289 case VIPSBandFormatUINT:
290 case VIPSBandFormatINT:
291 return(ScaleLongToQuantum(ReadBlobLong(image)));
292 case VIPSBandFormatFLOAT:
293 return((Quantum) ((float) QuantumRange*(ReadBlobFloat(image)/1.0)));
294 case VIPSBandFormatDOUBLE:
295 return((Quantum) ((double) QuantumRange*(ReadBlobDouble(
305 static MagickBooleanType ReadVIPSPixelsNONE(Image *image,
306 const VIPSBandFormat format,const VIPSType type,const unsigned int channels,
307 ExceptionInfo *exception)
321 for (y = 0; y < (ssize_t) image->rows; y++)
323 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
324 if (q == (Quantum *) NULL)
326 for (x=0; x < (ssize_t) image->columns; x++)
328 pixel=ReadVIPSPixelNONE(image,format,type);
329 SetPixelRed(image,pixel,q);
332 SetPixelGreen(image,pixel,q);
333 SetPixelBlue(image,pixel,q);
335 SetPixelAlpha(image,ReadVIPSPixelNONE(image,format,type),q);
339 SetPixelGreen(image,ReadVIPSPixelNONE(image,format,type),q);
340 SetPixelBlue(image,ReadVIPSPixelNONE(image,format,type),q);
343 if (image->colorspace == CMYKColorspace)
344 SetPixelIndex(image,ReadVIPSPixelNONE(image,format,type),q);
346 SetPixelAlpha(image,ReadVIPSPixelNONE(image,format,type),q);
348 else if (channels == 5)
350 SetPixelIndex(image,ReadVIPSPixelNONE(image,format,type),q);
351 SetPixelAlpha(image,ReadVIPSPixelNONE(image,format,type),q);
354 q+=GetPixelChannels(image);
356 if (SyncAuthenticPixels(image,exception) == MagickFalse)
362 static Image *ReadVIPSImage(const ImageInfo *image_info,
363 ExceptionInfo *exception)
366 buffer[MagickPathExtent],
391 assert(image_info != (const ImageInfo *) NULL);
392 assert(image_info->signature == MagickCoreSignature);
393 if (image_info->debug != MagickFalse)
394 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
395 image_info->filename);
396 assert(exception != (ExceptionInfo *) NULL);
397 assert(exception->signature == MagickCoreSignature);
399 image=AcquireImage(image_info,exception);
400 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
401 if (status == MagickFalse)
403 image=DestroyImageList(image);
404 return((Image *) NULL);
406 marker=ReadBlobLSBLong(image);
407 if (marker == VIPS_MAGIC_LSB)
408 image->endian=LSBEndian;
409 else if (marker == VIPS_MAGIC_MSB)
410 image->endian=MSBEndian;
412 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
413 image->columns=(size_t) ReadBlobLong(image);
414 image->rows=(size_t) ReadBlobLong(image);
415 status=SetImageExtent(image,image->columns,image->rows,exception);
416 if (status == MagickFalse)
417 return(DestroyImageList(image));
418 channels=ReadBlobLong(image);
419 (void) ReadBlobLong(image); /* Legacy */
420 format=(VIPSBandFormat) ReadBlobLong(image);
423 case VIPSBandFormatUCHAR:
424 case VIPSBandFormatCHAR:
427 case VIPSBandFormatUSHORT:
428 case VIPSBandFormatSHORT:
431 case VIPSBandFormatUINT:
432 case VIPSBandFormatINT:
433 case VIPSBandFormatFLOAT:
436 case VIPSBandFormatDOUBLE:
440 case VIPSBandFormatCOMPLEX:
441 case VIPSBandFormatDPCOMPLEX:
442 case VIPSBandFormatNOTSET:
443 ThrowReaderException(CoderError,"Unsupported band format");
445 coding=(VIPSCoding) ReadBlobLong(image);
446 type=(VIPSType) ReadBlobLong(image);
450 SetImageColorspace(image,CMYKColorspace,exception);
452 image->alpha_trait=BlendPixelTrait;
456 SetImageColorspace(image,GRAYColorspace,exception);
458 image->alpha_trait=BlendPixelTrait;
462 SetImageColorspace(image,RGBColorspace,exception);
464 image->alpha_trait=BlendPixelTrait;
467 SetImageColorspace(image,sRGBColorspace,exception);
469 image->alpha_trait=BlendPixelTrait;
472 case VIPSTypeFOURIER:
473 case VIPSTypeHISTOGRAM:
478 case VIPSTypeMULTIBAND:
482 ThrowReaderException(CoderError,"Unsupported colorspace");
484 image->units=PixelsPerCentimeterResolution;
485 image->resolution.x=ReadBlobFloat(image)*10;
486 image->resolution.y=ReadBlobFloat(image)*10;
488 Legacy, offsets, future
490 (void) ReadBlobLongLong(image);
491 (void) ReadBlobLongLong(image);
492 (void) ReadBlobLongLong(image);
493 if (image_info->ping != MagickFalse)
495 if (IsSupportedCombination(format,type) == MagickFalse)
496 ThrowReaderException(CoderError,
497 "Unsupported combination of band format and colorspace");
498 if (channels == 0 || channels > 5)
499 ThrowReaderException(CoderError,"Unsupported number of channels");
500 if (coding == VIPSCodingNONE)
501 status=ReadVIPSPixelsNONE(image,format,type,channels,exception);
503 ThrowReaderException(CoderError,"Unsupported coding");
504 metadata=(char *) NULL;
505 while ((n=ReadBlob(image,MagickPathExtent-1,(unsigned char *) buffer)) != 0)
508 if (metadata == (char *) NULL)
509 metadata=ConstantString(buffer);
511 (void) ConcatenateString(&metadata,buffer);
513 if (metadata != (char *) NULL)
515 SetImageProperty(image,"vips:metadata",metadata,exception);
516 metadata=(char *) RelinquishMagickMemory(metadata);
518 (void) CloseBlob(image);
519 if (status == MagickFalse)
520 return((Image *) NULL);
525 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529 % R e g i s t e r V I P S I m a g e %
533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 % RegisterVIPSmage() adds attributes for the VIPS image format to the list
536 % of supported formats. The attributes include the image format tag, a
537 % method to read and/or write the format, whether the format supports the
538 % saving of more than one frame to the same file or blob, whether the format
539 % supports native in-memory I/O, and a brief description of the format.
541 % The format of the RegisterVIPSImage method is:
543 % size_t RegisterVIPSImage(void)
546 ModuleExport size_t RegisterVIPSImage(void)
551 entry=AcquireMagickInfo("VIPS","VIPS","VIPS image");
552 entry->decoder=(DecodeImageHandler *) ReadVIPSImage;
553 entry->encoder=(EncodeImageHandler *) WriteVIPSImage;
554 entry->magick=(IsImageFormatHandler *) IsVIPS;
555 entry->flags|=CoderEndianSupportFlag;
556 (void) RegisterMagickInfo(entry);
557 return(MagickImageCoderSignature);
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % U n r e g i s t e r V I P S I m a g e %
569 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 % UnregisterVIPSImage() removes format registrations made by the
572 % VIPS module from the list of supported formats.
574 % The format of the UnregisterVIPSImage method is:
576 % UnregisterVIPSImage(void)
579 ModuleExport void UnregisterVIPSImage(void)
581 (void) UnregisterMagickInfo("VIPS");
585 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
589 % W r i t e V I P S I m a g e %
593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
595 % WriteVIPSImage() writes an image to a file in VIPS image format.
597 % The format of the WriteVIPSImage method is:
599 % MagickBooleanType WriteVIPSImage(const ImageInfo *image_info,Image *image)
601 % A description of each parameter follows.
603 % o image_info: the image info.
605 % o image: The image.
609 static inline void WriteVIPSPixel(Image *image, const Quantum value)
611 if (image->depth == 16)
612 (void) WriteBlobShort(image,ScaleQuantumToShort(value));
614 (void) WriteBlobByte(image,ScaleQuantumToChar(value));
617 static MagickBooleanType WriteVIPSImage(const ImageInfo *image_info,
618 Image *image,ExceptionInfo *exception)
626 register const Quantum
638 assert(image_info != (const ImageInfo *) NULL);
639 assert(image_info->signature == MagickCoreSignature);
640 assert(image != (Image *) NULL);
641 assert(image->signature == MagickCoreSignature);
642 if (image->debug != MagickFalse)
643 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
645 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
646 if (status == MagickFalse)
648 if (image->endian == LSBEndian)
649 (void) WriteBlobLSBLong(image,VIPS_MAGIC_LSB);
651 (void) WriteBlobLSBLong(image,VIPS_MAGIC_MSB);
652 (void) WriteBlobLong(image,(unsigned int) image->columns);
653 (void) WriteBlobLong(image,(unsigned int) image->rows);
654 (void) SetImageStorageClass(image,DirectClass,exception);
655 channels=image->alpha_trait != UndefinedPixelTrait ? 4 : 3;
656 if (SetImageGray(image,exception) != MagickFalse)
657 channels=image->alpha_trait != UndefinedPixelTrait ? 2 : 1;
658 else if (image->colorspace == CMYKColorspace)
659 channels=image->alpha_trait != UndefinedPixelTrait ? 5 : 4;
660 (void) WriteBlobLong(image,channels);
661 (void) WriteBlobLong(image,0);
662 if (image->depth == 16)
663 (void) WriteBlobLong(image,(unsigned int) VIPSBandFormatUSHORT);
667 (void) WriteBlobLong(image,(unsigned int) VIPSBandFormatUCHAR);
669 (void) WriteBlobLong(image,VIPSCodingNONE);
670 switch(image->colorspace)
673 (void) WriteBlobLong(image,VIPSTypeCMYK);
676 if (image->depth == 16)
677 (void) WriteBlobLong(image, VIPSTypeGREY16);
679 (void) WriteBlobLong(image, VIPSTypeB_W);
682 (void) WriteBlobLong(image,VIPSTypeLAB);
685 (void) WriteBlobLong(image,VIPSTypeLCH);
688 if (image->depth == 16)
689 (void) WriteBlobLong(image, VIPSTypeRGB16);
691 (void) WriteBlobLong(image, VIPSTypeRGB);
694 (void) WriteBlobLong(image,VIPSTypeXYZ);
698 (void) SetImageColorspace(image,sRGBColorspace,exception);
699 (void) WriteBlobLong(image,VIPSTypesRGB);
702 if (image->units == PixelsPerCentimeterResolution)
704 (void) WriteBlobFloat(image,(image->resolution.x / 10));
705 (void) WriteBlobFloat(image,(image->resolution.y / 10));
707 else if (image->units == PixelsPerInchResolution)
709 (void) WriteBlobFloat(image,(image->resolution.x / 25.4));
710 (void) WriteBlobFloat(image,(image->resolution.y / 25.4));
714 (void) WriteBlobLong(image,0);
715 (void) WriteBlobLong(image,0);
718 Legacy, Offsets, Future
720 for (y=0; y < 24; y++)
721 (void) WriteBlobByte(image,0);
722 for (y=0; y < (ssize_t) image->rows; y++)
724 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
725 if (p == (const Quantum *) NULL)
727 for (x=0; x < (ssize_t) image->columns; x++)
729 WriteVIPSPixel(image,GetPixelRed(image,p));
731 WriteVIPSPixel(image,GetPixelAlpha(image,p));
734 WriteVIPSPixel(image,GetPixelGreen(image,p));
735 WriteVIPSPixel(image,GetPixelBlue(image,p));
738 if (image->colorspace == CMYKColorspace)
739 WriteVIPSPixel(image,GetPixelIndex(image,p));
741 WriteVIPSPixel(image,GetPixelAlpha(image,p));
743 else if (channels == 5)
745 WriteVIPSPixel(image,GetPixelIndex(image,p));
746 WriteVIPSPixel(image,GetPixelAlpha(image,p));
749 p+=GetPixelChannels(image);
752 metadata=GetImageProperty(image,"vips:metadata",exception);
753 if (metadata != (const char*) NULL)
754 WriteBlobString(image,metadata);
755 (void) CloseBlob(image);