cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % AAA AAA IIIII % |
| 7 | % A A A A I % |
| 8 | % AAAAA AAAAA I % |
| 9 | % A A A A I % |
| 10 | % A A A A IIIII % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write AAI X Image Format % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame^] | 20 | % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization % |
cristy | 397bede | 2011-01-18 23:59:59 +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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 47 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 48 | #include "MagickCore/exception.h" |
| 49 | #include "MagickCore/exception-private.h" |
| 50 | #include "MagickCore/image.h" |
| 51 | #include "MagickCore/image-private.h" |
| 52 | #include "MagickCore/list.h" |
| 53 | #include "MagickCore/magick.h" |
| 54 | #include "MagickCore/memory_.h" |
| 55 | #include "MagickCore/monitor.h" |
| 56 | #include "MagickCore/monitor-private.h" |
| 57 | #include "MagickCore/pixel.h" |
| 58 | #include "MagickCore/pixel-accessor.h" |
| 59 | #include "MagickCore/quantum-private.h" |
| 60 | #include "MagickCore/static.h" |
| 61 | #include "MagickCore/string_.h" |
| 62 | #include "MagickCore/module.h" |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | Forward declarations. |
| 66 | */ |
| 67 | static MagickBooleanType |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 68 | WriteAAIImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 | % % |
| 73 | % % |
| 74 | % % |
| 75 | % R e a d A A I I m a g e % |
| 76 | % % |
| 77 | % % |
| 78 | % % |
| 79 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 80 | % |
| 81 | % ReadAAIImage() reads an AAI Dune image file and returns it. It |
| 82 | % allocates the memory necessary for the new Image structure and returns a |
| 83 | % pointer to the new image. |
| 84 | % |
| 85 | % The format of the ReadAAIImage method is: |
| 86 | % |
| 87 | % Image *ReadAAIImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 88 | % |
| 89 | % A description of each parameter follows: |
| 90 | % |
| 91 | % o image_info: the image info. |
| 92 | % |
| 93 | % o exception: return any errors or warnings in this structure. |
| 94 | % |
| 95 | */ |
| 96 | static Image *ReadAAIImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 97 | { |
| 98 | Image |
| 99 | *image; |
| 100 | |
| 101 | MagickBooleanType |
| 102 | status; |
| 103 | |
| 104 | register ssize_t |
| 105 | x; |
| 106 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 107 | register Quantum |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 108 | *q; |
| 109 | |
| 110 | register unsigned char |
| 111 | *p; |
| 112 | |
| 113 | size_t |
| 114 | height, |
| 115 | length, |
| 116 | width; |
| 117 | |
| 118 | ssize_t |
| 119 | count, |
| 120 | y; |
| 121 | |
| 122 | unsigned char |
| 123 | *pixels; |
| 124 | |
| 125 | /* |
| 126 | Open image file. |
| 127 | */ |
| 128 | assert(image_info != (const ImageInfo *) NULL); |
| 129 | assert(image_info->signature == MagickSignature); |
| 130 | if (image_info->debug != MagickFalse) |
| 131 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 132 | image_info->filename); |
| 133 | assert(exception != (ExceptionInfo *) NULL); |
| 134 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 135 | image=AcquireImage(image_info,exception); |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 136 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 137 | if (status == MagickFalse) |
| 138 | { |
| 139 | image=DestroyImageList(image); |
| 140 | return((Image *) NULL); |
| 141 | } |
| 142 | /* |
| 143 | Read AAI Dune image. |
| 144 | */ |
| 145 | width=ReadBlobLSBLong(image); |
| 146 | height=ReadBlobLSBLong(image); |
| 147 | if (EOFBlob(image) != MagickFalse) |
| 148 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 149 | if ((width == 0UL) || (height == 0UL)) |
| 150 | ThrowReaderException(CorruptImageError,"ImproperImageHeader"); |
| 151 | do |
| 152 | { |
| 153 | /* |
| 154 | Convert AAI raster image to pixel packets. |
| 155 | */ |
| 156 | image->columns=width; |
| 157 | image->rows=height; |
| 158 | image->depth=8; |
| 159 | if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0)) |
| 160 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 161 | break; |
| 162 | pixels=(unsigned char *) AcquireQuantumMemory(image->columns, |
| 163 | 4*sizeof(*pixels)); |
| 164 | if (pixels == (unsigned char *) NULL) |
| 165 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 166 | length=(size_t) 4*image->columns; |
| 167 | for (y=0; y < (ssize_t) image->rows; y++) |
| 168 | { |
| 169 | count=ReadBlob(image,length,pixels); |
| 170 | if ((size_t) count != length) |
| 171 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
| 172 | p=pixels; |
| 173 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 174 | if (q == (Quantum *) NULL) |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 175 | break; |
| 176 | for (x=0; x < (ssize_t) image->columns; x++) |
| 177 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 178 | SetPixelBlue(image,ScaleCharToQuantum(*p++),q); |
| 179 | SetPixelGreen(image,ScaleCharToQuantum(*p++),q); |
| 180 | SetPixelRed(image,ScaleCharToQuantum(*p++),q); |
cristy | 520bce8 | 2011-01-19 19:41:27 +0000 | [diff] [blame] | 181 | if (*p == 254) |
| 182 | *p=255; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 183 | SetPixelAlpha(image,ScaleCharToQuantum(*p++),q); |
| 184 | if (GetPixelAlpha(image,q) != OpaqueAlpha) |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 185 | image->alpha_trait=BlendPixelTrait; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 186 | q+=GetPixelChannels(image); |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 187 | } |
| 188 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 189 | break; |
cristy | bdadf31 | 2011-04-23 23:16:21 +0000 | [diff] [blame] | 190 | if (image->previous == (Image *) NULL) |
| 191 | { |
| 192 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 193 | image->rows); |
| 194 | if (status == MagickFalse) |
| 195 | break; |
| 196 | } |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 197 | } |
| 198 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 199 | if (EOFBlob(image) != MagickFalse) |
| 200 | { |
| 201 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 202 | image->filename); |
| 203 | break; |
| 204 | } |
| 205 | /* |
| 206 | Proceed to next image. |
| 207 | */ |
| 208 | if (image_info->number_scenes != 0) |
| 209 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 210 | break; |
| 211 | width=ReadBlobLSBLong(image); |
| 212 | height=ReadBlobLSBLong(image); |
| 213 | if ((width != 0UL) && (height != 0UL)) |
| 214 | { |
| 215 | /* |
| 216 | Allocate next image structure. |
| 217 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 218 | AcquireNextImage(image_info,image,exception); |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 219 | if (GetNextImageInList(image) == (Image *) NULL) |
| 220 | { |
| 221 | image=DestroyImageList(image); |
| 222 | return((Image *) NULL); |
| 223 | } |
| 224 | image=SyncNextImageInList(image); |
| 225 | status=SetImageProgress(image,LoadImagesTag,TellBlob(image), |
| 226 | GetBlobSize(image)); |
| 227 | if (status == MagickFalse) |
| 228 | break; |
| 229 | } |
| 230 | } while ((width != 0UL) && (height != 0UL)); |
| 231 | (void) CloseBlob(image); |
| 232 | return(GetFirstImageInList(image)); |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 237 | % % |
| 238 | % % |
| 239 | % % |
| 240 | % R e g i s t e r A A I I m a g e % |
| 241 | % % |
| 242 | % % |
| 243 | % % |
| 244 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 245 | % |
| 246 | % RegisterAAIImage() adds attributes for the AAI Dune image format to the list |
| 247 | % of supported formats. The attributes include the image format tag, a |
| 248 | % method to read and/or write the format, whether the format supports the |
| 249 | % saving of more than one frame to the same file or blob, whether the format |
| 250 | % supports native in-memory I/O, and a brief description of the format. |
| 251 | % |
| 252 | % The format of the RegisterAAIImage method is: |
| 253 | % |
| 254 | % size_t RegisterAAIImage(void) |
| 255 | % |
| 256 | */ |
| 257 | ModuleExport size_t RegisterAAIImage(void) |
| 258 | { |
| 259 | MagickInfo |
| 260 | *entry; |
| 261 | |
| 262 | entry=SetMagickInfo("AAI"); |
| 263 | entry->decoder=(DecodeImageHandler *) ReadAAIImage; |
| 264 | entry->encoder=(EncodeImageHandler *) WriteAAIImage; |
| 265 | entry->description=ConstantString("AAI Dune image"); |
| 266 | entry->module=ConstantString("AAI"); |
| 267 | (void) RegisterMagickInfo(entry); |
| 268 | return(MagickImageCoderSignature); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 273 | % % |
| 274 | % % |
| 275 | % % |
| 276 | % U n r e g i s t e r A A I I m a g e % |
| 277 | % % |
| 278 | % % |
| 279 | % % |
| 280 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 281 | % |
| 282 | % UnregisterAAIImage() removes format registrations made by the |
| 283 | % AAI module from the list of supported formats. |
| 284 | % |
| 285 | % The format of the UnregisterAAIImage method is: |
| 286 | % |
| 287 | % UnregisterAAIImage(void) |
| 288 | % |
| 289 | */ |
| 290 | ModuleExport void UnregisterAAIImage(void) |
| 291 | { |
| 292 | (void) UnregisterMagickInfo("AAI"); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 297 | % % |
| 298 | % % |
| 299 | % % |
| 300 | % W r i t e A A I I m a g e % |
| 301 | % % |
| 302 | % % |
| 303 | % % |
| 304 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 305 | % |
| 306 | % WriteAAIImage() writes an image to a file in AAI Dune image format. |
| 307 | % |
| 308 | % The format of the WriteAAIImage method is: |
| 309 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 310 | % MagickBooleanType WriteAAIImage(const ImageInfo *image_info, |
| 311 | % Image *image,ExceptionInfo *exception) |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 312 | % |
| 313 | % A description of each parameter follows. |
| 314 | % |
| 315 | % o image_info: the image info. |
| 316 | % |
| 317 | % o image: The image. |
| 318 | % |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 319 | % o exception: return any errors or warnings in this structure. |
| 320 | % |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 321 | */ |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 322 | static MagickBooleanType WriteAAIImage(const ImageInfo *image_info,Image *image, |
| 323 | ExceptionInfo *exception) |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 324 | { |
| 325 | MagickBooleanType |
| 326 | status; |
| 327 | |
| 328 | MagickOffsetType |
| 329 | scene; |
| 330 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 331 | register const Quantum |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 332 | *restrict p; |
| 333 | |
| 334 | register ssize_t |
| 335 | x; |
| 336 | |
| 337 | register unsigned char |
| 338 | *restrict q; |
| 339 | |
| 340 | ssize_t |
| 341 | count, |
| 342 | y; |
| 343 | |
| 344 | unsigned char |
| 345 | *pixels; |
| 346 | |
| 347 | /* |
| 348 | Open output image file. |
| 349 | */ |
| 350 | assert(image_info != (const ImageInfo *) NULL); |
| 351 | assert(image_info->signature == MagickSignature); |
| 352 | assert(image != (Image *) NULL); |
| 353 | assert(image->signature == MagickSignature); |
| 354 | if (image->debug != MagickFalse) |
| 355 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 356 | assert(exception != (ExceptionInfo *) NULL); |
| 357 | assert(exception->signature == MagickSignature); |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 358 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 359 | if (status == MagickFalse) |
| 360 | return(status); |
| 361 | scene=0; |
| 362 | do |
| 363 | { |
| 364 | /* |
| 365 | Write AAI header. |
| 366 | */ |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 367 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 8d95109 | 2012-02-08 18:54:56 +0000 | [diff] [blame] | 368 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 369 | (void) WriteBlobLSBLong(image,(unsigned int) image->columns); |
| 370 | (void) WriteBlobLSBLong(image,(unsigned int) image->rows); |
| 371 | /* |
| 372 | Allocate memory for pixels. |
| 373 | */ |
| 374 | pixels=(unsigned char *) AcquireQuantumMemory((size_t) image->columns, |
| 375 | 4*sizeof(*pixels)); |
| 376 | if (pixels == (unsigned char *) NULL) |
| 377 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 378 | /* |
| 379 | Convert MIFF to AAI raster pixels. |
| 380 | */ |
| 381 | for (y=0; y < (ssize_t) image->rows; y++) |
| 382 | { |
cristy | 1e178e7 | 2011-08-28 19:44:34 +0000 | [diff] [blame] | 383 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 384 | if (p == (const Quantum *) NULL) |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 385 | break; |
| 386 | q=pixels; |
| 387 | for (x=0; x < (ssize_t) image->columns; x++) |
| 388 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 389 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
| 390 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 391 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 392 | *q=ScaleQuantumToChar((Quantum) (image->alpha_trait == BlendPixelTrait ? |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 393 | GetPixelAlpha(image,p) : OpaqueAlpha)); |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 394 | if (*q == 255) |
| 395 | *q=254; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 396 | p+=GetPixelChannels(image); |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 397 | q++; |
| 398 | } |
| 399 | count=WriteBlob(image,(size_t) (q-pixels),pixels); |
| 400 | if (count != (ssize_t) (q-pixels)) |
| 401 | break; |
cristy | bdadf31 | 2011-04-23 23:16:21 +0000 | [diff] [blame] | 402 | if (image->previous == (Image *) NULL) |
| 403 | { |
| 404 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 405 | image->rows); |
| 406 | if (status == MagickFalse) |
| 407 | break; |
| 408 | } |
cristy | 397bede | 2011-01-18 23:59:59 +0000 | [diff] [blame] | 409 | } |
| 410 | pixels=(unsigned char *) RelinquishMagickMemory(pixels); |
| 411 | if (GetNextImageInList(image) == (Image *) NULL) |
| 412 | break; |
| 413 | image=SyncNextImageInList(image); |
| 414 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 415 | GetImageListLength(image)); |
| 416 | if (status == MagickFalse) |
| 417 | break; |
| 418 | } while (image_info->adjoin != MagickFalse); |
| 419 | (void) CloseBlob(image); |
| 420 | return(MagickTrue); |
| 421 | } |