dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % V V IIIII PPPP SSSSS % |
| 7 | % V V I P P SS % |
| 8 | % V V I PPPP SSS % |
| 9 | % V V I P SS % |
| 10 | % V IIIII P SSSSS % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write VIPS Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % Dirk Lemstra % |
| 17 | % April 2014 % |
| 18 | % % |
| 19 | % % |
cristy | b56bb24 | 2014-11-25 17:12:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization % |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 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. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
| 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" |
| 64 | |
| 65 | /* |
| 66 | Define declaractions. |
| 67 | */ |
| 68 | #define VIPS_MAGIC_LSB 0x08f2a6b6U |
| 69 | #define VIPS_MAGIC_MSB 0xb6a6f208U |
| 70 | |
| 71 | typedef enum |
| 72 | { |
| 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) */ |
| 84 | } VIPSBandFormat; |
| 85 | |
| 86 | typedef enum |
| 87 | { |
| 88 | VIPSCodingNONE = 0, /* VIPS computation format */ |
| 89 | VIPSCodingLABQ = 2, /* LABQ storage format */ |
| 90 | VIPSCodingRAD = 6 /* Radiance storage format */ |
| 91 | } VIPSCoding; |
| 92 | |
| 93 | typedef enum |
| 94 | { |
| 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 */ |
| 111 | } VIPSType; |
| 112 | |
| 113 | /* |
| 114 | Forward declarations. |
| 115 | */ |
| 116 | static MagickBooleanType |
| 117 | WriteVIPSImage(const ImageInfo *,Image *,ExceptionInfo *); |
| 118 | |
| 119 | /* |
| 120 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 121 | % % |
| 122 | % % |
| 123 | % % |
| 124 | % I s V I P S % |
| 125 | % % |
| 126 | % % |
| 127 | % % |
| 128 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 129 | % |
| 130 | % IsVIPS() returns MagickTrue if the image format type, identified by the |
| 131 | % magick string, is VIPS. |
| 132 | % |
| 133 | % The format of the IsVIPS method is: |
| 134 | % |
| 135 | % MagickBooleanType IsVIPS(const unsigned char *magick,const size_t length) |
| 136 | % |
| 137 | % A description of each parameter follows: |
| 138 | % |
| 139 | % o magick: compare image format pattern against these bytes. |
| 140 | % |
| 141 | % o length: Specifies the length of the magick string. |
| 142 | % |
| 143 | */ |
| 144 | static MagickBooleanType IsVIPS(const unsigned char *magick,const size_t length) |
| 145 | { |
| 146 | if (length < 4) |
| 147 | return(MagickFalse); |
| 148 | |
| 149 | if (memcmp(magick,"\010\362\246\266",4) == 0) |
| 150 | return(MagickTrue); |
| 151 | |
| 152 | if (memcmp(magick,"\266\246\362\010",4) == 0) |
| 153 | return(MagickTrue); |
| 154 | |
| 155 | return(MagickFalse); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 160 | % % |
| 161 | % % |
| 162 | % % |
| 163 | % R e a d V I P S I m a g e % |
| 164 | % % |
| 165 | % % |
| 166 | % % |
| 167 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 168 | % |
| 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 |
| 171 | % new image. |
| 172 | % |
| 173 | % The format of the ReadVIPSImage method is: |
| 174 | % |
| 175 | % Image *ReadVIPSmage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 176 | % |
| 177 | % A description of each parameter follows: |
| 178 | % |
| 179 | % o image_info: the image info. |
| 180 | % |
| 181 | % o exception: return any errors or warnings in this structure. |
| 182 | % |
| 183 | */ |
| 184 | |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 185 | static inline MagickBooleanType IsSupportedCombination( |
| 186 | const VIPSBandFormat format,const VIPSType type) |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 187 | { |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 188 | switch(type) |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 189 | { |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 190 | case VIPSTypeB_W: |
| 191 | case VIPSTypeCMYK: |
| 192 | case VIPSTypeRGB: |
| 193 | case VIPSTypesRGB: |
| 194 | return(MagickTrue); |
| 195 | case VIPSTypeGREY16: |
| 196 | case VIPSTypeRGB16: |
| 197 | switch(format) |
| 198 | { |
| 199 | case VIPSBandFormatUSHORT: |
| 200 | case VIPSBandFormatSHORT: |
| 201 | case VIPSBandFormatUINT: |
| 202 | case VIPSBandFormatINT: |
| 203 | case VIPSBandFormatFLOAT: |
| 204 | case VIPSBandFormatDOUBLE: |
| 205 | return(MagickTrue); |
dirk | fdec0e7 | 2014-05-09 18:45:41 +0000 | [diff] [blame] | 206 | default: |
| 207 | return(MagickFalse); |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 208 | } |
dirk | fdec0e7 | 2014-05-09 18:45:41 +0000 | [diff] [blame] | 209 | default: |
| 210 | return(MagickFalse); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 211 | } |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static inline Quantum ReadVIPSPixelNONE(Image *image, |
| 215 | const VIPSBandFormat format,const VIPSType type) |
| 216 | { |
| 217 | switch(type) |
| 218 | { |
| 219 | case VIPSTypeB_W: |
| 220 | case VIPSTypeRGB: |
| 221 | { |
| 222 | unsigned char |
dirk | fdec0e7 | 2014-05-09 18:45:41 +0000 | [diff] [blame] | 223 | c; |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 224 | |
| 225 | switch(format) |
| 226 | { |
| 227 | case VIPSBandFormatUCHAR: |
| 228 | case VIPSBandFormatCHAR: |
| 229 | c=(unsigned char) ReadBlobByte(image); |
| 230 | break; |
| 231 | case VIPSBandFormatUSHORT: |
| 232 | case VIPSBandFormatSHORT: |
| 233 | c=(unsigned char) ReadBlobShort(image); |
| 234 | break; |
| 235 | case VIPSBandFormatUINT: |
| 236 | case VIPSBandFormatINT: |
| 237 | c=(unsigned char) ReadBlobLong(image); |
| 238 | break; |
| 239 | case VIPSBandFormatFLOAT: |
| 240 | c=(unsigned char) ReadBlobFloat(image); |
| 241 | break; |
| 242 | case VIPSBandFormatDOUBLE: |
| 243 | c=(unsigned char) ReadBlobDouble(image); |
| 244 | break; |
dirk | fdec0e7 | 2014-05-09 18:45:41 +0000 | [diff] [blame] | 245 | default: |
| 246 | c=0; |
| 247 | break; |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 248 | } |
| 249 | return(ScaleCharToQuantum(c)); |
| 250 | } |
| 251 | case VIPSTypeGREY16: |
| 252 | case VIPSTypeRGB16: |
| 253 | { |
| 254 | unsigned short |
dirk | fdec0e7 | 2014-05-09 18:45:41 +0000 | [diff] [blame] | 255 | s; |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 256 | |
| 257 | switch(format) |
| 258 | { |
| 259 | case VIPSBandFormatUSHORT: |
| 260 | case VIPSBandFormatSHORT: |
| 261 | s=(unsigned short) ReadBlobShort(image); |
| 262 | break; |
| 263 | case VIPSBandFormatUINT: |
| 264 | case VIPSBandFormatINT: |
| 265 | s=(unsigned short) ReadBlobLong(image); |
| 266 | break; |
| 267 | case VIPSBandFormatFLOAT: |
| 268 | s=(unsigned short) ReadBlobFloat(image); |
| 269 | break; |
| 270 | case VIPSBandFormatDOUBLE: |
| 271 | s=(unsigned short) ReadBlobDouble(image); |
| 272 | break; |
dirk | fdec0e7 | 2014-05-09 18:45:41 +0000 | [diff] [blame] | 273 | default: |
| 274 | s=0; |
| 275 | break; |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 276 | } |
| 277 | return(ScaleShortToQuantum(s)); |
| 278 | } |
| 279 | case VIPSTypeCMYK: |
| 280 | case VIPSTypesRGB: |
| 281 | switch(format) |
| 282 | { |
| 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( |
| 296 | image)/1.0))); |
dirk | fdec0e7 | 2014-05-09 18:45:41 +0000 | [diff] [blame] | 297 | default: |
| 298 | return((Quantum) 0); |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 299 | } |
dirk | fdec0e7 | 2014-05-09 18:45:41 +0000 | [diff] [blame] | 300 | default: |
| 301 | return((Quantum) 0); |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 302 | } |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static MagickBooleanType ReadVIPSPixelsNONE(Image *image, |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 306 | const VIPSBandFormat format,const VIPSType type,const unsigned int channels, |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 307 | ExceptionInfo *exception) |
| 308 | { |
| 309 | Quantum |
| 310 | pixel; |
| 311 | |
| 312 | register Quantum |
| 313 | *q; |
| 314 | |
| 315 | register ssize_t |
| 316 | x; |
| 317 | |
| 318 | ssize_t |
| 319 | y; |
| 320 | |
| 321 | for (y = 0; y < (ssize_t) image->rows; y++) |
| 322 | { |
| 323 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
| 324 | if (q == (Quantum *) NULL) |
| 325 | return MagickFalse; |
| 326 | for (x=0; x < (ssize_t) image->columns; x++) |
| 327 | { |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 328 | pixel=ReadVIPSPixelNONE(image,format,type); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 329 | SetPixelRed(image,pixel,q); |
| 330 | if (channels < 3) |
| 331 | { |
| 332 | SetPixelGreen(image,pixel,q); |
| 333 | SetPixelBlue(image,pixel,q); |
| 334 | if (channels == 2) |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 335 | SetPixelAlpha(image,ReadVIPSPixelNONE(image,format,type),q); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 336 | } |
| 337 | else |
| 338 | { |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 339 | SetPixelGreen(image,ReadVIPSPixelNONE(image,format,type),q); |
| 340 | SetPixelBlue(image,ReadVIPSPixelNONE(image,format,type),q); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 341 | if (channels == 4) |
| 342 | { |
| 343 | if (image->colorspace == CMYKColorspace) |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 344 | SetPixelIndex(image,ReadVIPSPixelNONE(image,format,type),q); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 345 | else |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 346 | SetPixelAlpha(image,ReadVIPSPixelNONE(image,format,type),q); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 347 | } |
| 348 | else if (channels == 5) |
| 349 | { |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 350 | SetPixelIndex(image,ReadVIPSPixelNONE(image,format,type),q); |
| 351 | SetPixelAlpha(image,ReadVIPSPixelNONE(image,format,type),q); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | q+=GetPixelChannels(image); |
| 355 | } |
| 356 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 357 | return MagickFalse; |
| 358 | } |
| 359 | return(MagickTrue); |
| 360 | } |
| 361 | |
| 362 | static Image *ReadVIPSImage(const ImageInfo *image_info, |
| 363 | ExceptionInfo *exception) |
| 364 | { |
| 365 | char |
| 366 | buffer[MaxTextExtent], |
| 367 | *metadata; |
| 368 | |
| 369 | Image |
| 370 | *image; |
| 371 | |
| 372 | MagickBooleanType |
| 373 | status; |
| 374 | |
| 375 | ssize_t |
| 376 | n; |
| 377 | |
| 378 | unsigned int |
| 379 | channels, |
| 380 | marker; |
| 381 | |
| 382 | VIPSBandFormat |
| 383 | format; |
| 384 | |
| 385 | VIPSCoding |
| 386 | coding; |
| 387 | |
| 388 | VIPSType |
| 389 | type; |
| 390 | |
| 391 | assert(image_info != (const ImageInfo *) NULL); |
| 392 | assert(image_info->signature == MagickSignature); |
| 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 == MagickSignature); |
| 398 | |
| 399 | image=AcquireImage(image_info,exception); |
| 400 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 401 | if (status == MagickFalse) |
| 402 | { |
| 403 | image=DestroyImageList(image); |
| 404 | return((Image *) NULL); |
| 405 | } |
| 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; |
| 411 | else |
| 412 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 413 | image->columns=(size_t) ReadBlobLong(image); |
| 414 | image->rows=(size_t) ReadBlobLong(image); |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 415 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 416 | if (status == MagickFalse) |
| 417 | return(DestroyImageList(image)); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 418 | channels=ReadBlobLong(image); |
| 419 | (void) ReadBlobLong(image); /* Legacy */ |
| 420 | format=(VIPSBandFormat) ReadBlobLong(image); |
| 421 | switch(format) |
| 422 | { |
| 423 | case VIPSBandFormatUCHAR: |
| 424 | case VIPSBandFormatCHAR: |
| 425 | image->depth=8; |
| 426 | break; |
| 427 | case VIPSBandFormatUSHORT: |
| 428 | case VIPSBandFormatSHORT: |
| 429 | image->depth=16; |
| 430 | break; |
| 431 | case VIPSBandFormatUINT: |
| 432 | case VIPSBandFormatINT: |
| 433 | case VIPSBandFormatFLOAT: |
| 434 | image->depth=32; |
| 435 | break; |
| 436 | case VIPSBandFormatDOUBLE: |
| 437 | image->depth=64; |
| 438 | break; |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 439 | default: |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 440 | case VIPSBandFormatCOMPLEX: |
| 441 | case VIPSBandFormatDPCOMPLEX: |
| 442 | case VIPSBandFormatNOTSET: |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 443 | ThrowReaderException(CoderError,"Unsupported band format"); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 444 | } |
| 445 | coding=(VIPSCoding) ReadBlobLong(image); |
| 446 | type=(VIPSType) ReadBlobLong(image); |
| 447 | switch(type) |
| 448 | { |
| 449 | case VIPSTypeCMYK: |
| 450 | SetImageColorspace(image,CMYKColorspace,exception); |
| 451 | if (channels == 5) |
| 452 | image->alpha_trait=BlendPixelTrait; |
| 453 | break; |
| 454 | case VIPSTypeB_W: |
| 455 | case VIPSTypeGREY16: |
| 456 | SetImageColorspace(image,GRAYColorspace,exception); |
| 457 | if (channels == 2) |
| 458 | image->alpha_trait=BlendPixelTrait; |
| 459 | break; |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 460 | case VIPSTypeRGB: |
| 461 | case VIPSTypeRGB16: |
| 462 | SetImageColorspace(image,RGBColorspace,exception); |
| 463 | if (channels == 4) |
| 464 | image->alpha_trait=BlendPixelTrait; |
| 465 | break; |
| 466 | case VIPSTypesRGB: |
| 467 | SetImageColorspace(image,sRGBColorspace,exception); |
| 468 | if (channels == 4) |
| 469 | image->alpha_trait=BlendPixelTrait; |
| 470 | break; |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 471 | default: |
| 472 | case VIPSTypeFOURIER: |
| 473 | case VIPSTypeHISTOGRAM: |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 474 | case VIPSTypeLAB: |
| 475 | case VIPSTypeLABS: |
| 476 | case VIPSTypeLABQ: |
| 477 | case VIPSTypeLCH: |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 478 | case VIPSTypeMULTIBAND: |
| 479 | case VIPSTypeUCS: |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 480 | case VIPSTypeXYZ: |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 481 | case VIPSTypeYXY: |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 482 | ThrowReaderException(CoderError,"Unsupported colorspace"); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 483 | } |
| 484 | image->units=PixelsPerCentimeterResolution; |
| 485 | image->resolution.x=ReadBlobFloat(image)*10; |
| 486 | image->resolution.y=ReadBlobFloat(image)*10; |
| 487 | /* |
| 488 | Legacy, offsets, future |
| 489 | */ |
| 490 | (void) ReadBlobLongLong(image); |
| 491 | (void) ReadBlobLongLong(image); |
| 492 | (void) ReadBlobLongLong(image); |
| 493 | if (image_info->ping != MagickFalse) |
| 494 | return(image); |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 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"); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 500 | if (coding == VIPSCodingNONE) |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 501 | status=ReadVIPSPixelsNONE(image,format,type,channels,exception); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 502 | else |
dirk | c8b6713 | 2014-05-06 20:08:37 +0000 | [diff] [blame] | 503 | ThrowReaderException(CoderError,"Unsupported coding"); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 504 | metadata=(char *) NULL; |
| 505 | while ((n=ReadBlob(image,MaxTextExtent-1,(unsigned char *) buffer)) != 0) |
| 506 | { |
| 507 | buffer[n]='\0'; |
| 508 | if (metadata == (char *) NULL) |
| 509 | metadata=ConstantString(buffer); |
| 510 | else |
| 511 | (void) ConcatenateString(&metadata,buffer); |
| 512 | } |
| 513 | if (metadata != (char *) NULL) |
| 514 | SetImageProperty(image,"vips:metadata",metadata,exception); |
| 515 | (void) CloseBlob(image); |
| 516 | if (status == MagickFalse) |
| 517 | return((Image *) NULL); |
| 518 | return(image); |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 523 | % % |
| 524 | % % |
| 525 | % % |
| 526 | % R e g i s t e r V I P S I m a g e % |
| 527 | % % |
| 528 | % % |
| 529 | % % |
| 530 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 531 | % |
| 532 | % RegisterVIPSmage() adds attributes for the VIPS image format to the list |
| 533 | % of supported formats. The attributes include the image format tag, a |
| 534 | % method to read and/or write the format, whether the format supports the |
| 535 | % saving of more than one frame to the same file or blob, whether the format |
| 536 | % supports native in-memory I/O, and a brief description of the format. |
| 537 | % |
| 538 | % The format of the RegisterVIPSImage method is: |
| 539 | % |
| 540 | % size_t RegisterVIPSImage(void) |
| 541 | % |
| 542 | */ |
| 543 | ModuleExport size_t RegisterVIPSImage(void) |
| 544 | { |
| 545 | MagickInfo |
| 546 | *entry; |
| 547 | |
| 548 | entry=SetMagickInfo("VIPS"); |
| 549 | entry->decoder=(DecodeImageHandler *) ReadVIPSImage; |
| 550 | entry->encoder=(EncodeImageHandler *) WriteVIPSImage; |
| 551 | entry->magick=(IsImageFormatHandler *) IsVIPS; |
| 552 | entry->description=ConstantString("VIPS image"); |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame^] | 553 | entry->flags|=CoderEndianSupportFlag; |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 554 | entry->module=ConstantString("VIPS"); |
| 555 | (void) RegisterMagickInfo(entry); |
| 556 | return(MagickImageCoderSignature); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 561 | % % |
| 562 | % % |
| 563 | % % |
| 564 | % U n r e g i s t e r V I P S I m a g e % |
| 565 | % % |
| 566 | % % |
| 567 | % % |
| 568 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 569 | % |
| 570 | % UnregisterVIPSImage() removes format registrations made by the |
| 571 | % VIPS module from the list of supported formats. |
| 572 | % |
| 573 | % The format of the UnregisterVIPSImage method is: |
| 574 | % |
| 575 | % UnregisterVIPSImage(void) |
| 576 | % |
| 577 | */ |
| 578 | ModuleExport void UnregisterVIPSImage(void) |
| 579 | { |
| 580 | (void) UnregisterMagickInfo("VIPS"); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 585 | % % |
| 586 | % % |
| 587 | % % |
| 588 | % W r i t e V I P S I m a g e % |
| 589 | % % |
| 590 | % % |
| 591 | % % |
| 592 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 593 | % |
| 594 | % WriteVIPSImage() writes an image to a file in VIPS image format. |
| 595 | % |
| 596 | % The format of the WriteVIPSImage method is: |
| 597 | % |
| 598 | % MagickBooleanType WriteVIPSImage(const ImageInfo *image_info,Image *image) |
| 599 | % |
| 600 | % A description of each parameter follows. |
| 601 | % |
| 602 | % o image_info: the image info. |
| 603 | % |
| 604 | % o image: The image. |
| 605 | % |
| 606 | */ |
| 607 | |
| 608 | static inline void WriteVIPSPixel(Image *image, const Quantum value) |
| 609 | { |
| 610 | if (image->depth == 16) |
| 611 | (void) WriteBlobShort(image,ScaleQuantumToShort(value)); |
| 612 | else |
| 613 | (void) WriteBlobByte(image,ScaleQuantumToChar(value)); |
| 614 | } |
| 615 | |
| 616 | static MagickBooleanType WriteVIPSImage(const ImageInfo *image_info, |
| 617 | Image *image,ExceptionInfo *exception) |
| 618 | { |
| 619 | const char |
| 620 | *metadata; |
| 621 | |
| 622 | MagickBooleanType |
| 623 | status; |
| 624 | |
| 625 | register const Quantum |
| 626 | *p; |
| 627 | |
| 628 | register ssize_t |
| 629 | x; |
| 630 | |
| 631 | ssize_t |
| 632 | y; |
| 633 | |
| 634 | unsigned int |
| 635 | channels; |
| 636 | |
| 637 | assert(image_info != (const ImageInfo *) NULL); |
| 638 | assert(image_info->signature == MagickSignature); |
| 639 | assert(image != (Image *) NULL); |
| 640 | assert(image->signature == MagickSignature); |
| 641 | if (image->debug != MagickFalse) |
| 642 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 643 | |
| 644 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
| 645 | if (status == MagickFalse) |
| 646 | return(status); |
| 647 | if (image->endian == LSBEndian) |
| 648 | (void) WriteBlobLSBLong(image,VIPS_MAGIC_LSB); |
| 649 | else |
| 650 | (void) WriteBlobLSBLong(image,VIPS_MAGIC_MSB); |
cristy | 2544971 | 2014-07-25 16:10:29 +0000 | [diff] [blame] | 651 | (void) WriteBlobLong(image,(unsigned int) image->columns); |
| 652 | (void) WriteBlobLong(image,(unsigned int) image->rows); |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 653 | (void) SetImageStorageClass(image,DirectClass,exception); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 654 | channels=image->alpha_trait != UndefinedPixelTrait ? 4 : 3; |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 655 | if (IsImageGray(image,exception) != MagickFalse) |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 656 | channels=image->alpha_trait != UndefinedPixelTrait ? 2 : 1; |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 657 | else if (image->colorspace == CMYKColorspace) |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 658 | channels=image->alpha_trait != UndefinedPixelTrait ? 5 : 4; |
dirk | e303984 | 2014-04-26 18:22:06 +0000 | [diff] [blame] | 659 | (void) WriteBlobLong(image,channels); |
| 660 | (void) WriteBlobLong(image,0); |
| 661 | if (image->depth == 16) |
| 662 | (void) WriteBlobLong(image,(unsigned int) VIPSBandFormatUSHORT); |
| 663 | else |
| 664 | { |
| 665 | image->depth=8; |
| 666 | (void) WriteBlobLong(image,(unsigned int) VIPSBandFormatUCHAR); |
| 667 | } |
| 668 | (void) WriteBlobLong(image,VIPSCodingNONE); |
| 669 | switch(image->colorspace) |
| 670 | { |
| 671 | case CMYKColorspace: |
| 672 | (void) WriteBlobLong(image,VIPSTypeCMYK); |
| 673 | break; |
| 674 | case GRAYColorspace: |
| 675 | if (image->depth == 16) |
| 676 | (void) WriteBlobLong(image, VIPSTypeGREY16); |
| 677 | else |
| 678 | (void) WriteBlobLong(image, VIPSTypeB_W); |
| 679 | break; |
| 680 | case LabColorspace: |
| 681 | (void) WriteBlobLong(image,VIPSTypeLAB); |
| 682 | break; |
| 683 | case LCHColorspace: |
| 684 | (void) WriteBlobLong(image,VIPSTypeLCH); |
| 685 | break; |
| 686 | case RGBColorspace: |
| 687 | if (image->depth == 16) |
| 688 | (void) WriteBlobLong(image, VIPSTypeRGB16); |
| 689 | else |
| 690 | (void) WriteBlobLong(image, VIPSTypeRGB); |
| 691 | break; |
| 692 | case XYZColorspace: |
| 693 | (void) WriteBlobLong(image,VIPSTypeXYZ); |
| 694 | break; |
| 695 | default: |
| 696 | case sRGBColorspace: |
| 697 | (void) SetImageColorspace(image,sRGBColorspace,exception); |
| 698 | (void) WriteBlobLong(image,VIPSTypesRGB); |
| 699 | break; |
| 700 | } |
| 701 | if (image->units == PixelsPerCentimeterResolution) |
| 702 | { |
| 703 | (void) WriteBlobFloat(image,(image->resolution.x / 10)); |
| 704 | (void) WriteBlobFloat(image,(image->resolution.y / 10)); |
| 705 | } |
| 706 | else if (image->units == PixelsPerInchResolution) |
| 707 | { |
| 708 | (void) WriteBlobFloat(image,(image->resolution.x / 25.4)); |
| 709 | (void) WriteBlobFloat(image,(image->resolution.y / 25.4)); |
| 710 | } |
| 711 | else |
| 712 | { |
| 713 | (void) WriteBlobLong(image,0); |
| 714 | (void) WriteBlobLong(image,0); |
| 715 | } |
| 716 | /* |
| 717 | Legacy, Offsets, Future |
| 718 | */ |
| 719 | for (y=0; y < 24; y++) |
| 720 | (void) WriteBlobByte(image,0); |
| 721 | for (y=0; y < (ssize_t) image->rows; y++) |
| 722 | { |
| 723 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| 724 | if (p == (const Quantum *) NULL) |
| 725 | break; |
| 726 | for (x=0; x < (ssize_t) image->columns; x++) |
| 727 | { |
| 728 | WriteVIPSPixel(image,GetPixelRed(image,p)); |
| 729 | if (channels == 2) |
| 730 | WriteVIPSPixel(image,GetPixelAlpha(image,p)); |
| 731 | else |
| 732 | { |
| 733 | WriteVIPSPixel(image,GetPixelGreen(image,p)); |
| 734 | WriteVIPSPixel(image,GetPixelBlue(image,p)); |
| 735 | if (channels >= 4) |
| 736 | { |
| 737 | if (image->colorspace == CMYKColorspace) |
| 738 | WriteVIPSPixel(image,GetPixelIndex(image,p)); |
| 739 | else |
| 740 | WriteVIPSPixel(image,GetPixelAlpha(image,p)); |
| 741 | } |
| 742 | else if (channels == 5) |
| 743 | { |
| 744 | WriteVIPSPixel(image,GetPixelIndex(image,p)); |
| 745 | WriteVIPSPixel(image,GetPixelAlpha(image,p)); |
| 746 | } |
| 747 | } |
| 748 | p+=GetPixelChannels(image); |
| 749 | } |
| 750 | } |
| 751 | metadata=GetImageProperty(image,"vips:metadata",exception); |
| 752 | if (metadata != (const char*) NULL) |
| 753 | WriteBlobString(image,metadata); |
| 754 | (void) CloseBlob(image); |
| 755 | return(status); |
| 756 | } |