cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % CCCC OOO N N SSSSS TTTTT IIIII TTTTT U U TTTTT EEEEE % |
| 7 | % C O O NN N SS T I T U U T E % |
| 8 | % C O O N N N ESSS T I T U U T EEE % |
| 9 | % C O O N NN SS T I T U U T E % |
| 10 | % CCCC OOO N N SSSSS T IIIII T UUU T EEEEE % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Methods to Consitute an Image % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % October 1998 % |
| 18 | % % |
| 19 | % % |
cristy | 16af1cb | 2009-12-11 21:38:29 +0000 | [diff] [blame] | 20 | % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +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 "magick/studio.h" |
| 43 | #include "magick/blob.h" |
| 44 | #include "magick/blob-private.h" |
| 45 | #include "magick/exception.h" |
| 46 | #include "magick/exception-private.h" |
| 47 | #include "magick/cache.h" |
| 48 | #include "magick/client.h" |
| 49 | #include "magick/constitute.h" |
| 50 | #include "magick/delegate.h" |
| 51 | #include "magick/geometry.h" |
| 52 | #include "magick/identify.h" |
| 53 | #include "magick/image-private.h" |
| 54 | #include "magick/list.h" |
| 55 | #include "magick/magick.h" |
| 56 | #include "magick/memory_.h" |
| 57 | #include "magick/monitor.h" |
cristy | 36507e6 | 2010-02-11 00:26:48 +0000 | [diff] [blame] | 58 | #include "magick/monitor-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 59 | #include "magick/option.h" |
| 60 | #include "magick/pixel.h" |
| 61 | #include "magick/policy.h" |
| 62 | #include "magick/profile.h" |
| 63 | #include "magick/property.h" |
| 64 | #include "magick/quantum.h" |
| 65 | #include "magick/resize.h" |
| 66 | #include "magick/resource_.h" |
| 67 | #include "magick/semaphore.h" |
| 68 | #include "magick/statistic.h" |
| 69 | #include "magick/stream.h" |
| 70 | #include "magick/string_.h" |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 71 | #include "magick/string-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 72 | #include "magick/timer.h" |
| 73 | #include "magick/transform.h" |
| 74 | #include "magick/utility.h" |
| 75 | |
| 76 | static SemaphoreInfo |
| 77 | *constitute_semaphore = (SemaphoreInfo *) NULL; |
| 78 | |
| 79 | /* |
| 80 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 81 | % % |
| 82 | % % |
| 83 | % % |
cristy | f34a145 | 2009-10-24 22:29:27 +0000 | [diff] [blame] | 84 | + C o n s t i t u t e C o m p o n e n t G e n e s i s % |
| 85 | % % |
| 86 | % % |
| 87 | % % |
| 88 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 89 | % |
| 90 | % ConstituteComponentGenesis() instantiates the constitute component. |
| 91 | % |
| 92 | % The format of the ConstituteComponentGenesis method is: |
| 93 | % |
| 94 | % MagickBooleanType ConstituteComponentGenesis(void) |
| 95 | % |
| 96 | */ |
| 97 | MagickExport MagickBooleanType ConstituteComponentGenesis(void) |
| 98 | { |
cristy | 165b609 | 2009-10-26 13:52:10 +0000 | [diff] [blame] | 99 | AcquireSemaphoreInfo(&constitute_semaphore); |
cristy | f34a145 | 2009-10-24 22:29:27 +0000 | [diff] [blame] | 100 | return(MagickTrue); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 105 | % % |
| 106 | % % |
| 107 | % % |
| 108 | + C o n s t i t u t e C o m p o n e n t T e r m i n u s % |
| 109 | % % |
| 110 | % % |
| 111 | % % |
| 112 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 113 | % |
| 114 | % ConstituteComponentTerminus() destroys the constitute component. |
| 115 | % |
| 116 | % The format of the ConstituteComponentTerminus method is: |
| 117 | % |
| 118 | % ConstituteComponentTerminus(void) |
| 119 | % |
| 120 | */ |
| 121 | MagickExport void ConstituteComponentTerminus(void) |
| 122 | { |
cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 123 | if (constitute_semaphore == (SemaphoreInfo *) NULL) |
| 124 | AcquireSemaphoreInfo(&constitute_semaphore); |
| 125 | DestroySemaphoreInfo(&constitute_semaphore); |
cristy | f34a145 | 2009-10-24 22:29:27 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* |
| 129 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 130 | % % |
| 131 | % % |
| 132 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 133 | % C o n s t i t u t e I m a g e % |
| 134 | % % |
| 135 | % % |
| 136 | % % |
| 137 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 138 | % |
| 139 | % ConstituteImage() returns an image from the pixel data you supply. |
| 140 | % The pixel data must be in scanline order top-to-bottom. The data can be |
| 141 | % char, short int, int, float, or double. Float and double require the |
| 142 | % pixels to be normalized [0..1], otherwise [0..QuantumRange]. For example, to |
| 143 | % create a 640x480 image from unsigned red-green-blue character data, use: |
| 144 | % |
| 145 | % image = ConstituteImage(640,480,"RGB",CharPixel,pixels,&exception); |
| 146 | % |
| 147 | % The format of the ConstituteImage method is: |
| 148 | % |
cristy | 0758f9e | 2010-05-28 01:44:10 +0000 | [diff] [blame^] | 149 | % Image *ConstituteImage(const size_t columns,const size_t rows, |
| 150 | % const char *map,const StorageType storage,const void *pixels, |
| 151 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 152 | % |
| 153 | % A description of each parameter follows: |
| 154 | % |
| 155 | % o columns: width in pixels of the image. |
| 156 | % |
| 157 | % o rows: height in pixels of the image. |
| 158 | % |
| 159 | % o map: This string reflects the expected ordering of the pixel array. |
| 160 | % It can be any combination or order of R = red, G = green, B = blue, |
| 161 | % A = alpha (0 is transparent), O = opacity (0 is opaque), C = cyan, |
| 162 | % Y = yellow, M = magenta, K = black, I = intensity (for grayscale), |
| 163 | % P = pad. |
| 164 | % |
| 165 | % o storage: Define the data type of the pixels. Float and double types are |
| 166 | % expected to be normalized [0..1] otherwise [0..QuantumRange]. Choose |
| 167 | % from these types: CharPixel, DoublePixel, FloatPixel, IntegerPixel, |
| 168 | % LongPixel, QuantumPixel, or ShortPixel. |
| 169 | % |
| 170 | % o pixels: This array of values contain the pixel components as defined by |
| 171 | % map and type. You must preallocate this array where the expected |
| 172 | % length varies depending on the values of width, height, map, and type. |
| 173 | % |
| 174 | % o exception: return any errors or warnings in this structure. |
| 175 | % |
| 176 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 177 | MagickExport Image *ConstituteImage(const size_t columns, |
| 178 | const size_t rows,const char *map,const StorageType storage, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 179 | const void *pixels,ExceptionInfo *exception) |
| 180 | { |
| 181 | Image |
| 182 | *image; |
| 183 | |
| 184 | MagickBooleanType |
| 185 | status; |
| 186 | |
| 187 | /* |
| 188 | Allocate image structure. |
| 189 | */ |
| 190 | assert(map != (const char *) NULL); |
| 191 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",map); |
| 192 | assert(pixels != (void *) NULL); |
| 193 | assert(exception != (ExceptionInfo *) NULL); |
| 194 | assert(exception->signature == MagickSignature); |
| 195 | image=AcquireImage((ImageInfo *) NULL); |
| 196 | if (image == (Image *) NULL) |
| 197 | return((Image *) NULL); |
| 198 | if ((columns == 0) || (rows == 0)) |
| 199 | ThrowImageException(OptionError,"NonZeroWidthAndHeightRequired"); |
| 200 | image->columns=columns; |
| 201 | image->rows=rows; |
| 202 | (void) SetImageBackgroundColor(image); |
| 203 | status=ImportImagePixels(image,0,0,columns,rows,map,storage,pixels); |
| 204 | if (status == MagickFalse) |
| 205 | { |
| 206 | InheritException(exception,&image->exception); |
| 207 | image=DestroyImage(image); |
| 208 | } |
| 209 | return(image); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 214 | % % |
| 215 | % % |
| 216 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | % P i n g I m a g e % |
| 218 | % % |
| 219 | % % |
| 220 | % % |
| 221 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 222 | % |
| 223 | % PingImage() returns all the properties of an image or image sequence |
| 224 | % except for the pixels. It is much faster and consumes far less memory |
| 225 | % than ReadImage(). On failure, a NULL image is returned and exception |
| 226 | % describes the reason for the failure. |
| 227 | % |
| 228 | % The format of the PingImage method is: |
| 229 | % |
| 230 | % Image *PingImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 231 | % |
| 232 | % A description of each parameter follows: |
| 233 | % |
| 234 | % o image_info: Ping the image defined by the file or filename members of |
| 235 | % this structure. |
| 236 | % |
| 237 | % o exception: return any errors or warnings in this structure. |
| 238 | % |
| 239 | */ |
| 240 | |
| 241 | #if defined(__cplusplus) || defined(c_plusplus) |
| 242 | extern "C" { |
| 243 | #endif |
| 244 | |
| 245 | static size_t PingStream(const Image *magick_unused(image), |
| 246 | const void *magick_unused(pixels),const size_t columns) |
| 247 | { |
| 248 | return(columns); |
| 249 | } |
| 250 | |
| 251 | #if defined(__cplusplus) || defined(c_plusplus) |
| 252 | } |
| 253 | #endif |
| 254 | |
| 255 | MagickExport Image *PingImage(const ImageInfo *image_info, |
| 256 | ExceptionInfo *exception) |
| 257 | { |
| 258 | Image |
| 259 | *image; |
| 260 | |
| 261 | ImageInfo |
| 262 | *ping_info; |
| 263 | |
| 264 | assert(image_info != (ImageInfo *) NULL); |
| 265 | assert(image_info->signature == MagickSignature); |
| 266 | if (image_info->debug != MagickFalse) |
| 267 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 268 | image_info->filename); |
| 269 | assert(exception != (ExceptionInfo *) NULL); |
| 270 | ping_info=CloneImageInfo(image_info); |
| 271 | ping_info->ping=MagickTrue; |
| 272 | image=ReadStream(ping_info,&PingStream,exception); |
| 273 | if (image != (Image *) NULL) |
| 274 | { |
| 275 | ResetTimer(&image->timer); |
| 276 | if (ping_info->verbose != MagickFalse) |
| 277 | (void) IdentifyImage(image,stdout,MagickFalse); |
| 278 | } |
| 279 | ping_info=DestroyImageInfo(ping_info); |
| 280 | return(image); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 285 | % % |
| 286 | % % |
| 287 | % % |
| 288 | % P i n g I m a g e s % |
| 289 | % % |
| 290 | % % |
| 291 | % % |
| 292 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 293 | % |
| 294 | % PingImages() pings one or more images and returns them as an image list. |
| 295 | % |
| 296 | % The format of the PingImage method is: |
| 297 | % |
| 298 | % Image *PingImages(const ImageInfo *image_info,ExceptionInfo *exception) |
| 299 | % |
| 300 | % A description of each parameter follows: |
| 301 | % |
| 302 | % o image_info: the image info. |
| 303 | % |
| 304 | % o exception: return any errors or warnings in this structure. |
| 305 | % |
| 306 | */ |
| 307 | MagickExport Image *PingImages(const ImageInfo *image_info, |
| 308 | ExceptionInfo *exception) |
| 309 | { |
| 310 | char |
| 311 | filename[MaxTextExtent]; |
| 312 | |
| 313 | Image |
| 314 | *image, |
| 315 | *images; |
| 316 | |
| 317 | ImageInfo |
| 318 | *read_info; |
| 319 | |
| 320 | /* |
| 321 | Ping image list from a file. |
| 322 | */ |
| 323 | assert(image_info != (ImageInfo *) NULL); |
| 324 | assert(image_info->signature == MagickSignature); |
| 325 | if (image_info->debug != MagickFalse) |
| 326 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 327 | image_info->filename); |
| 328 | assert(exception != (ExceptionInfo *) NULL); |
| 329 | (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename, |
| 330 | (int) image_info->scene,filename); |
| 331 | if (LocaleCompare(filename,image_info->filename) != 0) |
| 332 | { |
| 333 | ExceptionInfo |
| 334 | *sans; |
| 335 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 336 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 337 | extent, |
| 338 | scene; |
| 339 | |
| 340 | /* |
| 341 | Images of the form image-%d.png[1-5]. |
| 342 | */ |
| 343 | read_info=CloneImageInfo(image_info); |
| 344 | sans=AcquireExceptionInfo(); |
cristy | d965a42 | 2010-03-03 17:47:35 +0000 | [diff] [blame] | 345 | (void) SetImageInfo(read_info,0,sans); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 346 | sans=DestroyExceptionInfo(sans); |
| 347 | (void) CopyMagickString(filename,read_info->filename,MaxTextExtent); |
| 348 | images=NewImageList(); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 349 | extent=(ssize_t) (read_info->scene+read_info->number_scenes); |
| 350 | for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 351 | { |
| 352 | (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int) |
| 353 | scene,read_info->filename); |
| 354 | image=PingImage(read_info,exception); |
| 355 | if (image == (Image *) NULL) |
| 356 | continue; |
| 357 | AppendImageToList(&images,image); |
| 358 | } |
| 359 | read_info=DestroyImageInfo(read_info); |
| 360 | return(images); |
| 361 | } |
| 362 | return(PingImage(image_info,exception)); |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 367 | % % |
| 368 | % % |
| 369 | % % |
| 370 | % R e a d I m a g e % |
| 371 | % % |
| 372 | % % |
| 373 | % % |
| 374 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 375 | % |
| 376 | % ReadImage() reads an image or image sequence from a file or file handle. |
| 377 | % The method returns a NULL if there is a memory shortage or if the image |
| 378 | % cannot be read. On failure, a NULL image is returned and exception |
| 379 | % describes the reason for the failure. |
| 380 | % |
| 381 | % The format of the ReadImage method is: |
| 382 | % |
| 383 | % Image *ReadImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 384 | % |
| 385 | % A description of each parameter follows: |
| 386 | % |
| 387 | % o image_info: Read the image defined by the file or filename members of |
| 388 | % this structure. |
| 389 | % |
| 390 | % o exception: return any errors or warnings in this structure. |
| 391 | % |
| 392 | */ |
| 393 | MagickExport Image *ReadImage(const ImageInfo *image_info, |
| 394 | ExceptionInfo *exception) |
| 395 | { |
| 396 | char |
| 397 | filename[MaxTextExtent], |
| 398 | magick[MaxTextExtent], |
| 399 | magick_filename[MaxTextExtent]; |
| 400 | |
| 401 | const char |
| 402 | *value; |
| 403 | |
| 404 | const DelegateInfo |
| 405 | *delegate_info; |
| 406 | |
| 407 | const MagickInfo |
| 408 | *magick_info; |
| 409 | |
| 410 | ExceptionInfo |
| 411 | *sans_exception; |
| 412 | |
| 413 | GeometryInfo |
| 414 | geometry_info; |
| 415 | |
| 416 | Image |
| 417 | *image, |
| 418 | *next; |
| 419 | |
| 420 | ImageInfo |
| 421 | *read_info; |
| 422 | |
| 423 | MagickStatusType |
| 424 | flags, |
| 425 | thread_support; |
| 426 | |
| 427 | PolicyDomain |
| 428 | domain; |
| 429 | |
| 430 | PolicyRights |
| 431 | rights; |
| 432 | |
| 433 | /* |
| 434 | Determine image type from filename prefix or suffix (e.g. image.jpg). |
| 435 | */ |
| 436 | assert(image_info != (ImageInfo *) NULL); |
| 437 | assert(image_info->signature == MagickSignature); |
| 438 | assert(image_info->filename != (char *) NULL); |
| 439 | if (image_info->debug != MagickFalse) |
| 440 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 441 | image_info->filename); |
| 442 | assert(exception != (ExceptionInfo *) NULL); |
| 443 | read_info=CloneImageInfo(image_info); |
| 444 | (void) CopyMagickString(magick_filename,read_info->filename,MaxTextExtent); |
cristy | d965a42 | 2010-03-03 17:47:35 +0000 | [diff] [blame] | 445 | (void) SetImageInfo(read_info,0,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 446 | (void) CopyMagickString(filename,read_info->filename,MaxTextExtent); |
| 447 | (void) CopyMagickString(magick,read_info->magick,MaxTextExtent); |
| 448 | domain=CoderPolicyDomain; |
| 449 | rights=ReadPolicyRights; |
| 450 | if (IsRightsAuthorized(domain,rights,read_info->magick) == MagickFalse) |
| 451 | { |
cristy | a9197f6 | 2010-01-12 02:23:34 +0000 | [diff] [blame] | 452 | errno=EPERM; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 453 | (void) ThrowMagickException(exception,GetMagickModule(),PolicyError, |
| 454 | "NotAuthorized","`%s'",read_info->filename); |
| 455 | return((Image *) NULL); |
| 456 | } |
| 457 | /* |
| 458 | Call appropriate image reader based on image type. |
| 459 | */ |
| 460 | sans_exception=AcquireExceptionInfo(); |
| 461 | magick_info=GetMagickInfo(read_info->magick,sans_exception); |
| 462 | sans_exception=DestroyExceptionInfo(sans_exception); |
| 463 | if (magick_info != (const MagickInfo *) NULL) |
| 464 | { |
| 465 | if (GetMagickEndianSupport(magick_info) == MagickFalse) |
| 466 | read_info->endian=UndefinedEndian; |
| 467 | else |
| 468 | if ((image_info->endian == UndefinedEndian) && |
| 469 | (GetMagickRawSupport(magick_info) != MagickFalse)) |
| 470 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 471 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 472 | lsb_first; |
| 473 | |
| 474 | lsb_first=1; |
| 475 | read_info->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : |
| 476 | MSBEndian; |
| 477 | } |
| 478 | } |
| 479 | if ((magick_info != (const MagickInfo *) NULL) && |
| 480 | (GetMagickSeekableStream(magick_info) != MagickFalse)) |
| 481 | { |
| 482 | MagickBooleanType |
| 483 | status; |
| 484 | |
| 485 | image=AcquireImage(read_info); |
| 486 | (void) CopyMagickString(image->filename,read_info->filename, |
| 487 | MaxTextExtent); |
| 488 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 489 | if (status == MagickFalse) |
| 490 | { |
| 491 | read_info=DestroyImageInfo(read_info); |
| 492 | image=DestroyImage(image); |
| 493 | return((Image *) NULL); |
| 494 | } |
| 495 | if (IsBlobSeekable(image) == MagickFalse) |
| 496 | { |
| 497 | /* |
| 498 | Coder requires a seekable stream. |
| 499 | */ |
| 500 | *read_info->filename='\0'; |
| 501 | status=ImageToFile(image,read_info->filename,exception); |
| 502 | if (status == MagickFalse) |
| 503 | { |
| 504 | (void) CloseBlob(image); |
| 505 | read_info=DestroyImageInfo(read_info); |
| 506 | image=DestroyImage(image); |
| 507 | return((Image *) NULL); |
| 508 | } |
| 509 | read_info->temporary=MagickTrue; |
| 510 | } |
| 511 | (void) CloseBlob(image); |
| 512 | image=DestroyImage(image); |
| 513 | } |
| 514 | image=NewImageList(); |
cristy | 4e1dff6 | 2009-10-25 20:36:03 +0000 | [diff] [blame] | 515 | if (constitute_semaphore == (SemaphoreInfo *) NULL) |
| 516 | AcquireSemaphoreInfo(&constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 517 | if ((magick_info != (const MagickInfo *) NULL) && |
| 518 | (GetImageDecoder(magick_info) != (DecodeImageHandler *) NULL)) |
| 519 | { |
| 520 | thread_support=GetMagickThreadSupport(magick_info); |
| 521 | if ((thread_support & DecoderThreadSupport) == 0) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 522 | LockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 523 | image=GetImageDecoder(magick_info)(read_info,exception); |
| 524 | if ((thread_support & DecoderThreadSupport) == 0) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 525 | UnlockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | } |
| 527 | else |
| 528 | { |
| 529 | delegate_info=GetDelegateInfo(read_info->magick,(char *) NULL,exception); |
| 530 | if (delegate_info == (const DelegateInfo *) NULL) |
| 531 | { |
cristy | 3a7b2b8 | 2010-04-28 22:14:19 +0000 | [diff] [blame] | 532 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 533 | MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'", |
| 534 | read_info->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 535 | if (read_info->temporary != MagickFalse) |
| 536 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 537 | read_info=DestroyImageInfo(read_info); |
| 538 | return((Image *) NULL); |
| 539 | } |
| 540 | /* |
| 541 | Let our decoding delegate process the image. |
| 542 | */ |
| 543 | image=AcquireImage(read_info); |
| 544 | if (image == (Image *) NULL) |
| 545 | { |
| 546 | read_info=DestroyImageInfo(read_info); |
| 547 | return((Image *) NULL); |
| 548 | } |
| 549 | (void) CopyMagickString(image->filename,read_info->filename, |
| 550 | MaxTextExtent); |
| 551 | *read_info->filename='\0'; |
| 552 | if (GetDelegateThreadSupport(delegate_info) == MagickFalse) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 553 | LockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 554 | (void) InvokeDelegate(read_info,image,read_info->magick,(char *) NULL, |
| 555 | exception); |
| 556 | if (GetDelegateThreadSupport(delegate_info) == MagickFalse) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 557 | UnlockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | image=DestroyImageList(image); |
| 559 | read_info->temporary=MagickTrue; |
cristy | d965a42 | 2010-03-03 17:47:35 +0000 | [diff] [blame] | 560 | (void) SetImageInfo(read_info,0,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 561 | magick_info=GetMagickInfo(read_info->magick,exception); |
| 562 | if ((magick_info == (const MagickInfo *) NULL) || |
| 563 | (GetImageDecoder(magick_info) == (DecodeImageHandler *) NULL)) |
| 564 | { |
| 565 | if (IsPathAccessible(read_info->filename) != MagickFalse) |
| 566 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 567 | MissingDelegateError,"NoDecodeDelegateForThisImageFormat","`%s'", |
| 568 | read_info->filename); |
| 569 | else |
| 570 | ThrowFileException(exception,FileOpenError,"UnableToOpenFile", |
| 571 | read_info->filename); |
| 572 | read_info=DestroyImageInfo(read_info); |
| 573 | return((Image *) NULL); |
| 574 | } |
| 575 | thread_support=GetMagickThreadSupport(magick_info); |
| 576 | if ((thread_support & DecoderThreadSupport) == 0) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 577 | LockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | image=(Image *) (GetImageDecoder(magick_info))(read_info,exception); |
| 579 | if ((thread_support & DecoderThreadSupport) == 0) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 580 | UnlockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 581 | } |
| 582 | if (read_info->temporary != MagickFalse) |
| 583 | { |
| 584 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 585 | read_info->temporary=MagickFalse; |
| 586 | if (image != (Image *) NULL) |
| 587 | (void) CopyMagickString(image->filename,filename,MaxTextExtent); |
| 588 | } |
| 589 | if (image == (Image *) NULL) |
| 590 | { |
| 591 | read_info=DestroyImageInfo(read_info); |
| 592 | return(image); |
| 593 | } |
| 594 | if (exception->severity >= ErrorException) |
| 595 | (void) LogMagickEvent(ExceptionEvent,GetMagickModule(), |
| 596 | "Coder (%s) generated an image despite an error (%d), " |
| 597 | "notify the developers",image->magick,exception->severity); |
| 598 | if (IsBlobTemporary(image) != MagickFalse) |
| 599 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 600 | if ((GetNextImageInList(image) != (Image *) NULL) && |
| 601 | (IsSceneGeometry(read_info->scenes,MagickFalse) != MagickFalse)) |
| 602 | { |
| 603 | Image |
| 604 | *clones; |
| 605 | |
| 606 | clones=CloneImages(image,read_info->scenes,exception); |
| 607 | if (clones == (Image *) NULL) |
| 608 | (void) ThrowMagickException(exception,GetMagickModule(),OptionError, |
| 609 | "SubimageSpecificationReturnsNoImages","`%s'",read_info->filename); |
| 610 | else |
| 611 | { |
| 612 | image=DestroyImageList(image); |
| 613 | image=GetFirstImageInList(clones); |
| 614 | } |
| 615 | } |
| 616 | if (GetBlobError(image) != MagickFalse) |
| 617 | { |
| 618 | ThrowFileException(exception,FileOpenError, |
| 619 | "AnErrorHasOccurredReadingFromFile",read_info->filename); |
| 620 | image=DestroyImageList(image); |
| 621 | read_info=DestroyImageInfo(read_info); |
| 622 | return((Image *) NULL); |
| 623 | } |
| 624 | for (next=image; next != (Image *) NULL; next=GetNextImageInList(next)) |
| 625 | { |
| 626 | char |
| 627 | timestamp[MaxTextExtent]; |
| 628 | |
| 629 | const StringInfo |
| 630 | *profile; |
| 631 | |
| 632 | next->taint=MagickFalse; |
| 633 | if (next->magick_columns == 0) |
| 634 | next->magick_columns=next->columns; |
| 635 | if (next->magick_rows == 0) |
| 636 | next->magick_rows=next->rows; |
| 637 | if ((LocaleCompare(magick,"HTTP") != 0) && |
| 638 | (LocaleCompare(magick,"FTP") != 0)) |
| 639 | (void) CopyMagickString(next->magick,magick,MaxTextExtent); |
| 640 | (void) CopyMagickString(next->magick_filename,magick_filename, |
| 641 | MaxTextExtent); |
| 642 | if (IsBlobTemporary(image) != MagickFalse) |
| 643 | (void) CopyMagickString(next->filename,filename,MaxTextExtent); |
| 644 | value=GetImageProperty(next,"tiff:Orientation"); |
| 645 | if (value == (char *) NULL) |
| 646 | value=GetImageProperty(next,"exif:Orientation"); |
| 647 | if (value != (char *) NULL) |
| 648 | { |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 649 | next->orientation=(OrientationType) StringToLong(value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 650 | (void) DeleteImageProperty(next,"tiff:Orientation"); |
| 651 | (void) DeleteImageProperty(next,"exif:Orientation"); |
| 652 | } |
cristy | 9a98aa4 | 2010-05-14 16:07:28 +0000 | [diff] [blame] | 653 | value=GetImageProperty(next,"exif:XResolution"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 654 | if (value != (char *) NULL) |
| 655 | { |
| 656 | geometry_info.rho=next->x_resolution; |
| 657 | geometry_info.sigma=1.0; |
| 658 | flags=ParseGeometry(value,&geometry_info); |
| 659 | if (geometry_info.sigma != 0) |
| 660 | next->x_resolution=geometry_info.rho/geometry_info.sigma; |
| 661 | (void) DeleteImageProperty(next,"exif:XResolution"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 662 | } |
cristy | 9a98aa4 | 2010-05-14 16:07:28 +0000 | [diff] [blame] | 663 | value=GetImageProperty(next,"exif:YResolution"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 664 | if (value != (char *) NULL) |
| 665 | { |
| 666 | geometry_info.rho=next->y_resolution; |
| 667 | geometry_info.sigma=1.0; |
| 668 | flags=ParseGeometry(value,&geometry_info); |
| 669 | if (geometry_info.sigma != 0) |
| 670 | next->y_resolution=geometry_info.rho/geometry_info.sigma; |
| 671 | (void) DeleteImageProperty(next,"exif:YResolution"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 672 | } |
| 673 | value=GetImageProperty(next,"tiff:ResolutionUnit"); |
| 674 | if (value == (char *) NULL) |
| 675 | value=GetImageProperty(next,"exif:ResolutionUnit"); |
| 676 | if (value != (char *) NULL) |
| 677 | { |
cristy | f2f2727 | 2009-12-17 14:48:46 +0000 | [diff] [blame] | 678 | next->units=(ResolutionType) (StringToLong(value)-1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 679 | (void) DeleteImageProperty(next,"exif:ResolutionUnit"); |
| 680 | (void) DeleteImageProperty(next,"tiff:ResolutionUnit"); |
| 681 | } |
| 682 | if (next->page.width == 0) |
| 683 | next->page.width=next->columns; |
| 684 | if (next->page.height == 0) |
| 685 | next->page.height=next->rows; |
| 686 | if (*read_info->filename != '\0') |
| 687 | { |
| 688 | char |
| 689 | *property; |
| 690 | |
| 691 | const char |
| 692 | *option; |
| 693 | |
| 694 | option=GetImageOption(read_info,"caption"); |
| 695 | if (option != (const char *) NULL) |
| 696 | { |
| 697 | property=InterpretImageProperties(read_info,next,option); |
| 698 | (void) SetImageProperty(next,"caption",property); |
| 699 | property=DestroyString(property); |
| 700 | } |
| 701 | option=GetImageOption(read_info,"comment"); |
| 702 | if (option != (const char *) NULL) |
| 703 | { |
| 704 | property=InterpretImageProperties(read_info,next,option); |
| 705 | (void) SetImageProperty(next,"comment",property); |
| 706 | property=DestroyString(property); |
| 707 | } |
| 708 | option=GetImageOption(read_info,"label"); |
| 709 | if (option != (const char *) NULL) |
| 710 | { |
| 711 | property=InterpretImageProperties(read_info,next,option); |
| 712 | (void) SetImageProperty(next,"label",property); |
| 713 | property=DestroyString(property); |
| 714 | } |
| 715 | } |
| 716 | if (LocaleCompare(next->magick,"TEXT") == 0) |
| 717 | (void) ParseAbsoluteGeometry("0x0+0+0",&next->page); |
| 718 | if ((read_info->extract != (char *) NULL) && |
| 719 | (read_info->stream == (StreamHandler) NULL)) |
| 720 | { |
| 721 | RectangleInfo |
| 722 | geometry; |
| 723 | |
| 724 | flags=ParseAbsoluteGeometry(read_info->extract,&geometry); |
| 725 | if ((next->columns != geometry.width) || |
| 726 | (next->rows != geometry.height)) |
| 727 | { |
| 728 | if (((flags & XValue) != 0) || ((flags & YValue) != 0)) |
| 729 | { |
| 730 | Image |
| 731 | *crop_image; |
| 732 | |
| 733 | crop_image=CropImage(next,&geometry,exception); |
| 734 | if (crop_image != (Image *) NULL) |
| 735 | ReplaceImageInList(&next,crop_image); |
| 736 | } |
| 737 | else |
| 738 | if (((flags & WidthValue) != 0) || ((flags & HeightValue) != 0)) |
| 739 | { |
| 740 | Image |
| 741 | *size_image; |
| 742 | |
| 743 | flags=ParseRegionGeometry(next,read_info->extract,&geometry, |
| 744 | exception); |
| 745 | size_image=ResizeImage(next,geometry.width,geometry.height, |
| 746 | next->filter,next->blur,exception); |
| 747 | if (size_image != (Image *) NULL) |
| 748 | ReplaceImageInList(&next,size_image); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | profile=GetImageProfile(next,"icc"); |
| 753 | if (profile == (const StringInfo *) NULL) |
| 754 | profile=GetImageProfile(next,"icm"); |
| 755 | if (profile != (const StringInfo *) NULL) |
| 756 | { |
| 757 | next->color_profile.length=GetStringInfoLength(profile); |
| 758 | next->color_profile.info=GetStringInfoDatum(profile); |
| 759 | } |
| 760 | profile=GetImageProfile(next,"iptc"); |
| 761 | if (profile == (const StringInfo *) NULL) |
| 762 | profile=GetImageProfile(next,"8bim"); |
| 763 | if (profile != (const StringInfo *) NULL) |
| 764 | { |
| 765 | next->iptc_profile.length=GetStringInfoLength(profile); |
| 766 | next->iptc_profile.info=GetStringInfoDatum(profile); |
| 767 | } |
| 768 | (void) FormatMagickTime(GetBlobProperties(next)->st_mtime,MaxTextExtent, |
| 769 | timestamp); |
| 770 | (void) SetImageProperty(next,"date:modify",timestamp); |
| 771 | (void) FormatMagickTime(GetBlobProperties(next)->st_ctime,MaxTextExtent, |
| 772 | timestamp); |
| 773 | (void) SetImageProperty(next,"date:create",timestamp); |
| 774 | if (read_info->verbose != MagickFalse) |
| 775 | (void) IdentifyImage(next,stdout,MagickFalse); |
| 776 | image=next; |
| 777 | } |
| 778 | read_info=DestroyImageInfo(read_info); |
| 779 | return(GetFirstImageInList(image)); |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 784 | % % |
| 785 | % % |
| 786 | % % |
| 787 | % R e a d I m a g e s % |
| 788 | % % |
| 789 | % % |
| 790 | % % |
| 791 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 792 | % |
| 793 | % ReadImages() reads one or more images and returns them as an image list. |
| 794 | % |
| 795 | % The format of the ReadImage method is: |
| 796 | % |
| 797 | % Image *ReadImages(const ImageInfo *image_info,ExceptionInfo *exception) |
| 798 | % |
| 799 | % A description of each parameter follows: |
| 800 | % |
| 801 | % o image_info: the image info. |
| 802 | % |
| 803 | % o exception: return any errors or warnings in this structure. |
| 804 | % |
| 805 | */ |
| 806 | MagickExport Image *ReadImages(const ImageInfo *image_info, |
| 807 | ExceptionInfo *exception) |
| 808 | { |
| 809 | char |
| 810 | filename[MaxTextExtent]; |
| 811 | |
| 812 | Image |
| 813 | *image, |
| 814 | *images; |
| 815 | |
| 816 | ImageInfo |
| 817 | *read_info; |
| 818 | |
| 819 | /* |
| 820 | Read image list from a file. |
| 821 | */ |
| 822 | assert(image_info != (ImageInfo *) NULL); |
| 823 | assert(image_info->signature == MagickSignature); |
| 824 | if (image_info->debug != MagickFalse) |
| 825 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 826 | image_info->filename); |
| 827 | assert(exception != (ExceptionInfo *) NULL); |
| 828 | (void) InterpretImageFilename(image_info,(Image *) NULL,image_info->filename, |
| 829 | (int) image_info->scene,filename); |
| 830 | if (LocaleCompare(filename,image_info->filename) != 0) |
| 831 | { |
| 832 | ExceptionInfo |
| 833 | *sans; |
| 834 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 835 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 836 | extent, |
| 837 | scene; |
| 838 | |
| 839 | /* |
| 840 | Images of the form image-%d.png[1-5]. |
| 841 | */ |
| 842 | read_info=CloneImageInfo(image_info); |
| 843 | sans=AcquireExceptionInfo(); |
cristy | d965a42 | 2010-03-03 17:47:35 +0000 | [diff] [blame] | 844 | (void) SetImageInfo(read_info,0,sans); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 845 | sans=DestroyExceptionInfo(sans); |
| 846 | (void) CopyMagickString(filename,read_info->filename,MaxTextExtent); |
| 847 | images=NewImageList(); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 848 | extent=(ssize_t) (read_info->scene+read_info->number_scenes); |
| 849 | for (scene=(ssize_t) read_info->scene; scene < (ssize_t) extent; scene++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 850 | { |
| 851 | (void) InterpretImageFilename(image_info,(Image *) NULL,filename,(int) |
| 852 | scene,read_info->filename); |
| 853 | image=ReadImage(read_info,exception); |
| 854 | if (image == (Image *) NULL) |
| 855 | continue; |
| 856 | AppendImageToList(&images,image); |
| 857 | } |
| 858 | read_info=DestroyImageInfo(read_info); |
| 859 | return(images); |
| 860 | } |
| 861 | return(ReadImage(image_info,exception)); |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 866 | % % |
| 867 | % % |
| 868 | % % |
| 869 | + R e a d I n l i n e I m a g e % |
| 870 | % % |
| 871 | % % |
| 872 | % % |
| 873 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 874 | % |
| 875 | % ReadInlineImage() reads a Base64-encoded inline image or image sequence. |
| 876 | % The method returns a NULL if there is a memory shortage or if the image |
| 877 | % cannot be read. On failure, a NULL image is returned and exception |
| 878 | % describes the reason for the failure. |
| 879 | % |
| 880 | % The format of the ReadInlineImage method is: |
| 881 | % |
| 882 | % Image *ReadInlineImage(const ImageInfo *image_info,const char *content, |
| 883 | % ExceptionInfo *exception) |
| 884 | % |
| 885 | % A description of each parameter follows: |
| 886 | % |
| 887 | % o image_info: the image info. |
| 888 | % |
| 889 | % o content: the image encoded in Base64. |
| 890 | % |
| 891 | % o exception: return any errors or warnings in this structure. |
| 892 | % |
| 893 | */ |
| 894 | MagickExport Image *ReadInlineImage(const ImageInfo *image_info, |
| 895 | const char *content,ExceptionInfo *exception) |
| 896 | { |
| 897 | Image |
| 898 | *image; |
| 899 | |
| 900 | ImageInfo |
| 901 | *read_info; |
| 902 | |
| 903 | unsigned char |
| 904 | *blob; |
| 905 | |
| 906 | size_t |
| 907 | length; |
| 908 | |
| 909 | register const char |
| 910 | *p; |
| 911 | |
| 912 | image=NewImageList(); |
| 913 | for (p=content; (*p != ',') && (*p != '\0'); p++) ; |
| 914 | if (*p == '\0') |
| 915 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 916 | p++; |
| 917 | length=0; |
| 918 | blob=Base64Decode(p,&length); |
| 919 | if (length == 0) |
| 920 | ThrowReaderException(CorruptImageError,"CorruptImage"); |
| 921 | read_info=CloneImageInfo(image_info); |
| 922 | (void) SetImageInfoProgressMonitor(read_info,(MagickProgressMonitor) NULL, |
| 923 | (void *) NULL); |
| 924 | image=BlobToImage(read_info,blob,length,exception); |
| 925 | blob=(unsigned char *) RelinquishMagickMemory(blob); |
| 926 | read_info=DestroyImageInfo(read_info); |
| 927 | return(image); |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 932 | % % |
| 933 | % % |
| 934 | % % |
| 935 | % W r i t e I m a g e % |
| 936 | % % |
| 937 | % % |
| 938 | % % |
| 939 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 940 | % |
| 941 | % WriteImage() writes an image or an image sequence to a file or filehandle. |
| 942 | % If writing to a file on disk, the name is defined by the filename member of |
| 943 | % the image structure. Write() returns MagickFalse is these is a memory |
| 944 | % shortage or if the image cannot be written. Check the exception member of |
| 945 | % image to determine the cause for any failure. |
| 946 | % |
| 947 | % The format of the WriteImage method is: |
| 948 | % |
| 949 | % MagickBooleanType WriteImage(const ImageInfo *image_info,Image *image) |
| 950 | % |
| 951 | % A description of each parameter follows: |
| 952 | % |
| 953 | % o image_info: the image info. |
| 954 | % |
| 955 | % o image: the image. |
| 956 | % |
| 957 | */ |
| 958 | MagickExport MagickBooleanType WriteImage(const ImageInfo *image_info, |
| 959 | Image *image) |
| 960 | { |
| 961 | char |
| 962 | filename[MaxTextExtent]; |
| 963 | |
cristy | 4c49134 | 2009-11-02 01:46:10 +0000 | [diff] [blame] | 964 | const char |
| 965 | *option; |
| 966 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 967 | const DelegateInfo |
| 968 | *delegate_info; |
| 969 | |
| 970 | const MagickInfo |
| 971 | *magick_info; |
| 972 | |
| 973 | ExceptionInfo |
| 974 | *sans_exception; |
| 975 | |
| 976 | ImageInfo |
| 977 | *write_info; |
| 978 | |
| 979 | MagickBooleanType |
| 980 | status, |
| 981 | temporary; |
| 982 | |
| 983 | MagickStatusType |
| 984 | thread_support; |
| 985 | |
| 986 | PolicyDomain |
| 987 | domain; |
| 988 | |
| 989 | PolicyRights |
| 990 | rights; |
| 991 | |
| 992 | /* |
| 993 | Determine image type from filename prefix or suffix (e.g. image.jpg). |
| 994 | */ |
| 995 | assert(image_info != (ImageInfo *) NULL); |
| 996 | assert(image_info->signature == MagickSignature); |
| 997 | if (image->debug != MagickFalse) |
| 998 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 999 | image_info->filename); |
| 1000 | assert(image != (Image *) NULL); |
| 1001 | assert(image->signature == MagickSignature); |
| 1002 | sans_exception=AcquireExceptionInfo(); |
| 1003 | write_info=CloneImageInfo(image_info); |
| 1004 | (void) CopyMagickString(write_info->filename,image->filename,MaxTextExtent); |
| 1005 | if (*write_info->magick == '\0') |
| 1006 | (void) CopyMagickString(write_info->magick,image->magick,MaxTextExtent); |
cristy | d965a42 | 2010-03-03 17:47:35 +0000 | [diff] [blame] | 1007 | (void) SetImageInfo(write_info,1,sans_exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1008 | if (LocaleCompare(write_info->magick,"clipmask") == 0) |
| 1009 | { |
| 1010 | if (image->clip_mask == (Image *) NULL) |
| 1011 | { |
| 1012 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1013 | OptionError,"NoClipPathDefined","`%s'",image->filename); |
| 1014 | return(MagickFalse); |
| 1015 | } |
| 1016 | image=image->clip_mask; |
cristy | d965a42 | 2010-03-03 17:47:35 +0000 | [diff] [blame] | 1017 | (void) SetImageInfo(write_info,1,sans_exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1018 | } |
| 1019 | (void) CopyMagickString(filename,image->filename,MaxTextExtent); |
| 1020 | (void) CopyMagickString(image->filename,write_info->filename,MaxTextExtent); |
| 1021 | domain=CoderPolicyDomain; |
| 1022 | rights=WritePolicyRights; |
| 1023 | if (IsRightsAuthorized(domain,rights,write_info->magick) == MagickFalse) |
| 1024 | { |
| 1025 | sans_exception=DestroyExceptionInfo(sans_exception); |
cristy | a9197f6 | 2010-01-12 02:23:34 +0000 | [diff] [blame] | 1026 | errno=EPERM; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1027 | ThrowBinaryException(PolicyError,"NotAuthorized",filename); |
| 1028 | } |
| 1029 | magick_info=GetMagickInfo(write_info->magick,sans_exception); |
| 1030 | sans_exception=DestroyExceptionInfo(sans_exception); |
| 1031 | if (magick_info != (const MagickInfo *) NULL) |
| 1032 | { |
| 1033 | if (GetMagickEndianSupport(magick_info) == MagickFalse) |
| 1034 | image->endian=UndefinedEndian; |
| 1035 | else |
| 1036 | if ((image_info->endian == UndefinedEndian) && |
| 1037 | (GetMagickRawSupport(magick_info) != MagickFalse)) |
| 1038 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1039 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1040 | lsb_first; |
| 1041 | |
| 1042 | lsb_first=1; |
| 1043 | image->endian=(*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian; |
| 1044 | } |
| 1045 | } |
| 1046 | (void) SyncImageProfiles(image); |
cristy | 4c49134 | 2009-11-02 01:46:10 +0000 | [diff] [blame] | 1047 | option=GetImageOption(image_info,"delegate:bimodal"); |
| 1048 | if ((option != (const char *) NULL) && |
| 1049 | (IsMagickTrue(option) != MagickFalse) && |
| 1050 | (write_info->page == (char *) NULL) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1051 | (GetPreviousImageInList(image) == (Image *) NULL) && |
| 1052 | (GetNextImageInList(image) == (Image *) NULL) && |
| 1053 | (IsTaintImage(image) == MagickFalse)) |
| 1054 | { |
| 1055 | delegate_info=GetDelegateInfo(image->magick,write_info->magick, |
| 1056 | &image->exception); |
| 1057 | if ((delegate_info != (const DelegateInfo *) NULL) && |
| 1058 | (GetDelegateMode(delegate_info) == 0) && |
| 1059 | (IsPathAccessible(image->magick_filename) != MagickFalse)) |
| 1060 | { |
| 1061 | /* |
| 1062 | Process image with bi-modal delegate. |
| 1063 | */ |
| 1064 | (void) CopyMagickString(image->filename,image->magick_filename, |
| 1065 | MaxTextExtent); |
| 1066 | status=InvokeDelegate(write_info,image,image->magick, |
| 1067 | write_info->magick,&image->exception); |
| 1068 | write_info=DestroyImageInfo(write_info); |
| 1069 | (void) CopyMagickString(image->filename,filename,MaxTextExtent); |
| 1070 | return(status); |
| 1071 | } |
| 1072 | } |
| 1073 | status=MagickFalse; |
| 1074 | temporary=MagickFalse; |
| 1075 | if ((magick_info != (const MagickInfo *) NULL) && |
| 1076 | (GetMagickSeekableStream(magick_info) != MagickFalse)) |
| 1077 | { |
| 1078 | char |
| 1079 | filename[MaxTextExtent]; |
| 1080 | |
| 1081 | (void) CopyMagickString(filename,image->filename,MaxTextExtent); |
| 1082 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception); |
| 1083 | (void) CopyMagickString(image->filename,filename,MaxTextExtent); |
| 1084 | if (status != MagickFalse) |
| 1085 | { |
cristy | e92a6fd | 2010-02-04 01:36:45 +0000 | [diff] [blame] | 1086 | if (IsBlobSeekable(image) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1087 | { |
| 1088 | /* |
| 1089 | A seekable stream is required by the encoder. |
| 1090 | */ |
| 1091 | (void) CopyMagickString(write_info->filename,image->filename, |
| 1092 | MaxTextExtent); |
| 1093 | (void) AcquireUniqueFilename(image->filename); |
| 1094 | temporary=MagickTrue; |
| 1095 | } |
| 1096 | (void) CloseBlob(image); |
| 1097 | } |
| 1098 | } |
cristy | 4e1dff6 | 2009-10-25 20:36:03 +0000 | [diff] [blame] | 1099 | if (constitute_semaphore == (SemaphoreInfo *) NULL) |
| 1100 | AcquireSemaphoreInfo(&constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1101 | if ((magick_info != (const MagickInfo *) NULL) && |
| 1102 | (GetImageEncoder(magick_info) != (EncodeImageHandler *) NULL)) |
| 1103 | { |
| 1104 | /* |
| 1105 | Call appropriate image writer based on image type. |
| 1106 | */ |
| 1107 | thread_support=GetMagickThreadSupport(magick_info); |
| 1108 | if ((thread_support & EncoderThreadSupport) == 0) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 1109 | LockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1110 | status=GetImageEncoder(magick_info)(write_info,image); |
| 1111 | if ((thread_support & EncoderThreadSupport) == 0) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 1112 | UnlockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | delegate_info=GetDelegateInfo((char *) NULL,write_info->magick, |
| 1117 | &image->exception); |
| 1118 | if (delegate_info != (DelegateInfo *) NULL) |
| 1119 | { |
| 1120 | /* |
| 1121 | Process the image with delegate. |
| 1122 | */ |
| 1123 | *write_info->filename='\0'; |
| 1124 | if (GetDelegateThreadSupport(delegate_info) == MagickFalse) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 1125 | LockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1126 | status=InvokeDelegate(write_info,image,(char *) NULL, |
| 1127 | write_info->magick,&image->exception); |
| 1128 | if (GetDelegateThreadSupport(delegate_info) == MagickFalse) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 1129 | UnlockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1130 | (void) CopyMagickString(image->filename,filename,MaxTextExtent); |
| 1131 | } |
| 1132 | else |
| 1133 | { |
| 1134 | sans_exception=AcquireExceptionInfo(); |
| 1135 | magick_info=GetMagickInfo(write_info->magick,sans_exception); |
| 1136 | sans_exception=DestroyExceptionInfo(sans_exception); |
| 1137 | if ((write_info->affirm == MagickFalse) && |
| 1138 | (magick_info == (const MagickInfo *) NULL)) |
| 1139 | { |
| 1140 | (void) CopyMagickString(write_info->magick,image->magick, |
| 1141 | MaxTextExtent); |
| 1142 | magick_info=GetMagickInfo(write_info->magick,&image->exception); |
| 1143 | } |
| 1144 | if ((magick_info == (const MagickInfo *) NULL) || |
| 1145 | (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL)) |
cristy | 1a8fced | 2010-03-15 18:05:43 +0000 | [diff] [blame] | 1146 | { |
cristy | 924c872 | 2010-03-15 17:50:24 +0000 | [diff] [blame] | 1147 | magick_info=GetMagickInfo(image->magick,&image->exception); |
cristy | 1a8fced | 2010-03-15 18:05:43 +0000 | [diff] [blame] | 1148 | (void) CopyMagickString(image->filename,filename,MaxTextExtent); |
cristy | 924c872 | 2010-03-15 17:50:24 +0000 | [diff] [blame] | 1149 | } |
cristy | 1a8fced | 2010-03-15 18:05:43 +0000 | [diff] [blame] | 1150 | if ((magick_info == (const MagickInfo *) NULL) || |
| 1151 | (GetImageEncoder(magick_info) == (EncodeImageHandler *) NULL)) |
| 1152 | (void) ThrowMagickException(&image->exception,GetMagickModule(), |
| 1153 | MissingDelegateError,"NoEncodeDelegateForThisImageFormat","`%s'", |
| 1154 | image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1155 | else |
| 1156 | { |
| 1157 | /* |
| 1158 | Call appropriate image writer based on image type. |
| 1159 | */ |
| 1160 | thread_support=GetMagickThreadSupport(magick_info); |
| 1161 | if ((thread_support & EncoderThreadSupport) == 0) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 1162 | LockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1163 | status=GetImageEncoder(magick_info)(write_info,image); |
| 1164 | if ((thread_support & EncoderThreadSupport) == 0) |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 1165 | UnlockSemaphoreInfo(constitute_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | if (GetBlobError(image) != MagickFalse) |
| 1170 | ThrowFileException(&image->exception,FileOpenError, |
| 1171 | "AnErrorHasOccurredWritingToFile",image->filename); |
| 1172 | if (temporary == MagickTrue) |
| 1173 | { |
| 1174 | /* |
| 1175 | Copy temporary image file to permanent. |
| 1176 | */ |
| 1177 | status=OpenBlob(write_info,image,ReadBinaryBlobMode,&image->exception); |
| 1178 | if (status != MagickFalse) |
| 1179 | status=ImageToFile(image,write_info->filename,&image->exception); |
| 1180 | (void) RelinquishUniqueFileResource(image->filename); |
| 1181 | (void) CopyMagickString(image->filename,write_info->filename, |
| 1182 | MaxTextExtent); |
| 1183 | (void) CloseBlob(image); |
| 1184 | } |
| 1185 | if ((LocaleCompare(write_info->magick,"info") != 0) && |
| 1186 | (write_info->verbose != MagickFalse)) |
| 1187 | (void) IdentifyImage(image,stdout,MagickFalse); |
| 1188 | write_info=DestroyImageInfo(write_info); |
| 1189 | return(status); |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1194 | % % |
| 1195 | % % |
| 1196 | % % |
| 1197 | % W r i t e I m a g e s % |
| 1198 | % % |
| 1199 | % % |
| 1200 | % % |
| 1201 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1202 | % |
| 1203 | % WriteImages() writes an image sequence. |
| 1204 | % |
| 1205 | % The format of the WriteImages method is: |
| 1206 | % |
| 1207 | % MagickBooleanType WriteImages(const ImageInfo *image_info,Image *images, |
| 1208 | % const char *filename,ExceptionInfo *exception) |
| 1209 | % |
| 1210 | % A description of each parameter follows: |
| 1211 | % |
| 1212 | % o image_info: the image info. |
| 1213 | % |
| 1214 | % o images: the image list. |
| 1215 | % |
| 1216 | % o filename: the image filename. |
| 1217 | % |
| 1218 | % o exception: return any errors or warnings in this structure. |
| 1219 | % |
| 1220 | */ |
| 1221 | MagickExport MagickBooleanType WriteImages(const ImageInfo *image_info, |
| 1222 | Image *images,const char *filename,ExceptionInfo *exception) |
| 1223 | { |
cristy | 36507e6 | 2010-02-11 00:26:48 +0000 | [diff] [blame] | 1224 | #define WriteImageTag "Write/Image" |
| 1225 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1226 | BlobInfo |
| 1227 | *blob; |
| 1228 | |
| 1229 | ExceptionInfo |
| 1230 | *sans_exception; |
| 1231 | |
| 1232 | ImageInfo |
| 1233 | *write_info; |
| 1234 | |
cristy | 36507e6 | 2010-02-11 00:26:48 +0000 | [diff] [blame] | 1235 | MagickBooleanType |
| 1236 | proceed; |
| 1237 | |
| 1238 | MagickOffsetType |
| 1239 | i; |
| 1240 | |
| 1241 | MagickProgressMonitor |
| 1242 | progress_monitor; |
| 1243 | |
| 1244 | MagickSizeType |
| 1245 | number_images; |
| 1246 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1247 | MagickStatusType |
| 1248 | status; |
| 1249 | |
| 1250 | register Image |
| 1251 | *p; |
| 1252 | |
| 1253 | assert(image_info != (const ImageInfo *) NULL); |
| 1254 | assert(image_info->signature == MagickSignature); |
| 1255 | assert(images != (Image *) NULL); |
| 1256 | assert(images->signature == MagickSignature); |
| 1257 | if (images->debug != MagickFalse) |
| 1258 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename); |
| 1259 | assert(exception != (ExceptionInfo *) NULL); |
| 1260 | write_info=CloneImageInfo(image_info); |
| 1261 | images=GetFirstImageInList(images); |
| 1262 | blob=CloneBlobInfo(images->blob); /* thread specific I/O handler */ |
| 1263 | DestroyBlob(images); |
| 1264 | images->blob=blob; |
| 1265 | if (filename != (const char *) NULL) |
| 1266 | for (p=images; p != (Image *) NULL; p=GetNextImageInList(p)) |
| 1267 | (void) CopyMagickString(p->filename,filename,MaxTextExtent); |
| 1268 | (void) CopyMagickString(write_info->filename,images->filename,MaxTextExtent); |
| 1269 | if (*write_info->magick == '\0') |
| 1270 | (void) CopyMagickString(write_info->magick,images->magick,MaxTextExtent); |
| 1271 | sans_exception=AcquireExceptionInfo(); |
cristy | d965a42 | 2010-03-03 17:47:35 +0000 | [diff] [blame] | 1272 | (void) SetImageInfo(write_info,(unsigned int) GetImageListLength(images), |
| 1273 | sans_exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1274 | sans_exception=DestroyExceptionInfo(sans_exception); |
| 1275 | p=images; |
| 1276 | for ( ; GetNextImageInList(p) != (Image *) NULL; p=GetNextImageInList(p)) |
| 1277 | if (p->scene >= GetNextImageInList(p)->scene) |
| 1278 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1279 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1280 | i; |
| 1281 | |
| 1282 | /* |
| 1283 | Generate consistent scene numbers. |
| 1284 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1285 | i=(ssize_t) images->scene; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1286 | for (p=images; p != (Image *) NULL; p=GetNextImageInList(p)) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1287 | p->scene=(size_t) i++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1288 | break; |
| 1289 | } |
| 1290 | /* |
| 1291 | Write images. |
| 1292 | */ |
| 1293 | status=MagickTrue; |
cristy | 466802f | 2010-02-24 14:18:37 +0000 | [diff] [blame] | 1294 | progress_monitor=(MagickProgressMonitor) NULL; |
cristy | 36507e6 | 2010-02-11 00:26:48 +0000 | [diff] [blame] | 1295 | i=0; |
| 1296 | number_images=GetImageListLength(images); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1297 | for (p=images; p != (Image *) NULL; p=GetNextImageInList(p)) |
| 1298 | { |
cristy | 8b27a6d | 2010-02-14 03:31:15 +0000 | [diff] [blame] | 1299 | if (number_images != 1) |
| 1300 | progress_monitor=SetImageProgressMonitor(p,(MagickProgressMonitor) NULL, |
| 1301 | p->client_data); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1302 | status&=WriteImage(write_info,p); |
| 1303 | GetImageException(p,exception); |
cristy | 8b27a6d | 2010-02-14 03:31:15 +0000 | [diff] [blame] | 1304 | if (number_images != 1) |
| 1305 | (void) SetImageProgressMonitor(p,progress_monitor,p->client_data); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1306 | if (write_info->adjoin != MagickFalse) |
| 1307 | break; |
cristy | 8b27a6d | 2010-02-14 03:31:15 +0000 | [diff] [blame] | 1308 | if (number_images != 1) |
| 1309 | { |
| 1310 | proceed=SetImageProgress(p,WriteImageTag,i++,number_images); |
| 1311 | if (proceed == MagickFalse) |
| 1312 | break; |
| 1313 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1314 | } |
| 1315 | write_info=DestroyImageInfo(write_info); |
| 1316 | return(status != 0 ? MagickTrue : MagickFalse); |
| 1317 | } |