00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "magick/studio.h"
00042 #include "magick/property.h"
00043 #include "magick/blob.h"
00044 #include "magick/blob-private.h"
00045 #include "magick/color-private.h"
00046 #include "magick/exception.h"
00047 #include "magick/exception-private.h"
00048 #include "magick/cache.h"
00049 #include "magick/constitute.h"
00050 #include "magick/delegate.h"
00051 #include "magick/geometry.h"
00052 #include "magick/list.h"
00053 #include "magick/magick.h"
00054 #include "magick/memory_.h"
00055 #include "magick/monitor.h"
00056 #include "magick/option.h"
00057 #include "magick/pixel.h"
00058 #include "magick/pixel-private.h"
00059 #include "magick/quantum.h"
00060 #include "magick/quantum-private.h"
00061 #include "magick/resource_.h"
00062 #include "magick/semaphore.h"
00063 #include "magick/statistic.h"
00064 #include "magick/stream.h"
00065 #include "magick/string_.h"
00066 #include "magick/string-private.h"
00067 #include "magick/thread-private.h"
00068 #include "magick/utility.h"
00069
00070
00071
00072
00073 #define QuantumSignature 0xab
00074
00075
00076
00077
00078 static void
00079 DestroyQuantumPixels(QuantumInfo *);
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 static inline unsigned long MagickMax(const unsigned long x,
00107 const unsigned long y)
00108 {
00109 if (x > y)
00110 return(x);
00111 return(y);
00112 }
00113
00114 MagickExport QuantumInfo *AcquireQuantumInfo(const ImageInfo *image_info,
00115 Image *image)
00116 {
00117 MagickBooleanType
00118 status;
00119
00120 QuantumInfo
00121 *quantum_info;
00122
00123 quantum_info=(QuantumInfo *) AcquireAlignedMemory(1,sizeof(*quantum_info));
00124 if (quantum_info == (QuantumInfo *) NULL)
00125 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00126 quantum_info->signature=MagickSignature;
00127 GetQuantumInfo(image_info,quantum_info);
00128 if (image == (const Image *) NULL)
00129 return(quantum_info);
00130 status=SetQuantumDepth(image,quantum_info,image->depth);
00131 if (status == MagickFalse)
00132 quantum_info=DestroyQuantumInfo(quantum_info);
00133 return(quantum_info);
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 static MagickBooleanType AcquireQuantumPixels(QuantumInfo *quantum_info,
00162 const size_t extent)
00163 {
00164 register long
00165 i;
00166
00167 assert(quantum_info != (QuantumInfo *) NULL);
00168 assert(quantum_info->signature == MagickSignature);
00169 quantum_info->number_threads=GetOpenMPMaximumThreads();
00170 quantum_info->pixels=(unsigned char **) AcquireQuantumMemory(
00171 quantum_info->number_threads,sizeof(*quantum_info->pixels));
00172 if (quantum_info->pixels == (unsigned char **) NULL)
00173 return(MagickFalse);
00174 quantum_info->extent=extent;
00175 (void) ResetMagickMemory(quantum_info->pixels,0,
00176 sizeof(*quantum_info->pixels));
00177 for (i=0; i < (long) quantum_info->number_threads; i++)
00178 {
00179 quantum_info->pixels[i]=(unsigned char *) AcquireQuantumMemory(extent+1,
00180 sizeof(**quantum_info->pixels));
00181 if (quantum_info->pixels[i] == (unsigned char *) NULL)
00182 return(MagickFalse);
00183 (void) ResetMagickMemory(quantum_info->pixels[i],0,(extent+1)*
00184 sizeof(**quantum_info->pixels));
00185 quantum_info->pixels[i][extent]=QuantumSignature;
00186 }
00187 return(MagickTrue);
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 MagickExport QuantumInfo *DestroyQuantumInfo(QuantumInfo *quantum_info)
00214 {
00215 assert(quantum_info != (QuantumInfo *) NULL);
00216 assert(quantum_info->signature == MagickSignature);
00217 if (quantum_info->pixels != (unsigned char **) NULL)
00218 DestroyQuantumPixels(quantum_info);
00219 if (quantum_info->semaphore != (SemaphoreInfo *) NULL)
00220 DestroySemaphoreInfo(&quantum_info->semaphore);
00221 quantum_info->signature=(~MagickSignature);
00222 quantum_info=(QuantumInfo *) RelinquishMagickMemory(quantum_info);
00223 return(quantum_info);
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 static void DestroyQuantumPixels(QuantumInfo *quantum_info)
00249 {
00250 register long
00251 i;
00252
00253 assert(quantum_info != (QuantumInfo *) NULL);
00254 assert(quantum_info->signature == MagickSignature);
00255 assert(quantum_info->pixels != (unsigned char **) NULL);
00256 for (i=0; i < (long) quantum_info->number_threads; i++)
00257 {
00258 assert(quantum_info->pixels[i][quantum_info->extent] == QuantumSignature);
00259 quantum_info->pixels[i]=(unsigned char *) RelinquishMagickMemory(
00260 quantum_info->pixels[i]);
00261 }
00262 quantum_info->pixels=(unsigned char **) RelinquishMagickMemory(
00263 quantum_info->pixels);
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 MagickExport size_t GetQuantumExtent(const Image *image,
00295 const QuantumInfo *quantum_info,const QuantumType quantum_type)
00296 {
00297 size_t
00298 packet_size;
00299
00300 assert(quantum_info != (QuantumInfo *) NULL);
00301 assert(quantum_info->signature == MagickSignature);
00302 packet_size=1;
00303 switch (quantum_type)
00304 {
00305 case GrayAlphaQuantum: packet_size=2; break;
00306 case IndexAlphaQuantum: packet_size=2; break;
00307 case RGBQuantum: packet_size=3; break;
00308 case RGBAQuantum: packet_size=4; break;
00309 case BGRAQuantum: packet_size=4; break;
00310 case RGBOQuantum: packet_size=4; break;
00311 case CMYKQuantum: packet_size=4; break;
00312 case CMYKAQuantum: packet_size=5; break;
00313 default: break;
00314 }
00315 if (quantum_info->pack == MagickFalse)
00316 return((size_t) (packet_size*image->columns*((quantum_info->depth+7)/8)));
00317 return((size_t) ((packet_size*image->columns*quantum_info->depth+7)/8));
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 MagickExport void GetQuantumInfo(const ImageInfo *image_info,
00345 QuantumInfo *quantum_info)
00346 {
00347 const char
00348 *option;
00349
00350 assert(quantum_info != (QuantumInfo *) NULL);
00351 (void) ResetMagickMemory(quantum_info,0,sizeof(*quantum_info));
00352 quantum_info->quantum=8;
00353 quantum_info->maximum=1.0;
00354 quantum_info->scale=QuantumRange;
00355 quantum_info->pack=MagickTrue;
00356 quantum_info->semaphore=AllocateSemaphoreInfo();
00357 quantum_info->signature=MagickSignature;
00358 if (image_info == (const ImageInfo *) NULL)
00359 return;
00360 option=GetImageOption(image_info,"quantum:format");
00361 if (option != (char *) NULL)
00362 quantum_info->format=(QuantumFormatType) ParseMagickOption(
00363 MagickQuantumFormatOptions,MagickFalse,option);
00364 option=GetImageOption(image_info,"quantum:minimum");
00365 if (option != (char *) NULL)
00366 quantum_info->minimum=StringToDouble(option);
00367 option=GetImageOption(image_info,"quantum:maximum");
00368 if (option != (char *) NULL)
00369 quantum_info->maximum=StringToDouble(option);
00370 if ((quantum_info->minimum == 0.0) && (quantum_info->maximum == 0.0))
00371 quantum_info->scale=0.0;
00372 else
00373 if (quantum_info->minimum == quantum_info->maximum)
00374 {
00375 quantum_info->scale=(MagickRealType) QuantumRange/quantum_info->minimum;
00376 quantum_info->minimum=0.0;
00377 }
00378 else
00379 quantum_info->scale=(MagickRealType) QuantumRange/(quantum_info->maximum-
00380 quantum_info->minimum);
00381 option=GetImageOption(image_info,"quantum:scale");
00382 if (option != (char *) NULL)
00383 quantum_info->scale=StringToDouble(option);
00384 option=GetImageOption(image_info,"quantum:polarity");
00385 if (option != (char *) NULL)
00386 quantum_info->min_is_white=LocaleCompare(option,"min-is-white") == 0 ?
00387 MagickTrue : MagickFalse;
00388 }
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 MagickExport unsigned char *GetQuantumPixels(const QuantumInfo *quantum_info)
00414 {
00415 long
00416 id;
00417
00418 assert(quantum_info != (QuantumInfo *) NULL);
00419 assert(quantum_info->signature == MagickSignature);
00420 id=GetOpenMPThreadId();
00421 return(quantum_info->pixels[id]);
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 MagickExport QuantumType GetQuantumType(Image *image,ExceptionInfo *exception)
00447 {
00448 QuantumType
00449 quantum_type;
00450
00451 assert(image != (Image *) NULL);
00452 assert(image->signature == MagickSignature);
00453 if (image->debug != MagickFalse)
00454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00455 quantum_type=RGBQuantum;
00456 if (image->matte != MagickFalse)
00457 quantum_type=RGBAQuantum;
00458 if (image->colorspace == CMYKColorspace)
00459 {
00460 quantum_type=CMYKQuantum;
00461 if (image->matte != MagickFalse)
00462 quantum_type=CMYKAQuantum;
00463 }
00464 if (IsGrayImage(image,exception) != MagickFalse)
00465 {
00466 quantum_type=GrayQuantum;
00467 if (image->matte != MagickFalse)
00468 quantum_type=GrayAlphaQuantum;
00469 }
00470 else
00471 if (image->storage_class == PseudoClass)
00472 {
00473 quantum_type=IndexQuantum;
00474 if (image->matte != MagickFalse)
00475 quantum_type=IndexAlphaQuantum;
00476 }
00477 return(quantum_type);
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505 MagickExport void SetQuantumAlphaType(QuantumInfo *quantum_info,
00506 const QuantumAlphaType type)
00507 {
00508 assert(quantum_info != (QuantumInfo *) NULL);
00509 assert(quantum_info->signature == MagickSignature);
00510 quantum_info->alpha_type=type;
00511 }
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540 MagickExport MagickBooleanType SetQuantumDepth(const Image *image,
00541 QuantumInfo *quantum_info,const unsigned long depth)
00542 {
00543 MagickBooleanType
00544 status;
00545
00546
00547
00548
00549 assert(image != (Image *) NULL);
00550 assert(image->signature == MagickSignature);
00551 if (image->debug != MagickFalse)
00552 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00553 assert(quantum_info != (QuantumInfo *) NULL);
00554 assert(quantum_info->signature == MagickSignature);
00555 quantum_info->depth=depth;
00556 if (quantum_info->format == FloatingPointQuantumFormat)
00557 {
00558 if (quantum_info->depth > 32)
00559 quantum_info->depth=64;
00560 else
00561 if (quantum_info->depth > 16)
00562 quantum_info->depth=32;
00563 else
00564 quantum_info->depth=16;
00565 }
00566 if (quantum_info->pixels != (unsigned char **) NULL)
00567 DestroyQuantumPixels(quantum_info);
00568 status=AcquireQuantumPixels(quantum_info,(quantum_info->pad+5)*image->columns*
00569 ((quantum_info->depth+7)/8));
00570 return(status);
00571 }
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600 MagickExport MagickBooleanType SetQuantumFormat(const Image *image,
00601 QuantumInfo *quantum_info,const QuantumFormatType format)
00602 {
00603 assert(image != (Image *) NULL);
00604 assert(image->signature == MagickSignature);
00605 if (image->debug != MagickFalse)
00606 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00607 assert(quantum_info != (QuantumInfo *) NULL);
00608 assert(quantum_info->signature == MagickSignature);
00609 quantum_info->format=format;
00610 return(SetQuantumDepth(image,quantum_info,quantum_info->depth));
00611 }
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639 MagickExport void SetQuantumImageType(Image *image,
00640 const QuantumType quantum_type)
00641 {
00642 assert(image != (Image *) NULL);
00643 assert(image->signature == MagickSignature);
00644 if (image->debug != MagickFalse)
00645 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00646 switch (quantum_type)
00647 {
00648 case IndexQuantum:
00649 case IndexAlphaQuantum:
00650 {
00651 image->type=PaletteType;
00652 break;
00653 }
00654 case GrayQuantum:
00655 case GrayAlphaQuantum:
00656 {
00657 image->type=GrayscaleType;
00658 if (image->depth == 1)
00659 image->type=BilevelType;
00660 break;
00661 }
00662 case CyanQuantum:
00663 case MagentaQuantum:
00664 case YellowQuantum:
00665 case BlackQuantum:
00666 case CMYKQuantum:
00667 case CMYKAQuantum:
00668 {
00669 image->type=ColorSeparationType;
00670 break;
00671 }
00672 default:
00673 {
00674 image->type=TrueColorType;
00675 break;
00676 }
00677 }
00678 }
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705 MagickExport void SetQuantumPack(QuantumInfo *quantum_info,
00706 const MagickBooleanType pack)
00707 {
00708 assert(quantum_info != (QuantumInfo *) NULL);
00709 assert(quantum_info->signature == MagickSignature);
00710 quantum_info->pack=pack;
00711 }
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741 MagickExport MagickBooleanType SetQuantumPad(const Image *image,
00742 QuantumInfo *quantum_info,const unsigned long pad)
00743 {
00744 assert(image != (Image *) NULL);
00745 assert(image->signature == MagickSignature);
00746 if (image->debug != MagickFalse)
00747 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00748 assert(quantum_info != (QuantumInfo *) NULL);
00749 assert(quantum_info->signature == MagickSignature);
00750 quantum_info->pad=pad;
00751 return(SetQuantumDepth(image,quantum_info,quantum_info->depth));
00752 }
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779 MagickExport void SetQuantumMinIsWhite(QuantumInfo *quantum_info,
00780 const MagickBooleanType min_is_white)
00781 {
00782 assert(quantum_info != (QuantumInfo *) NULL);
00783 assert(quantum_info->signature == MagickSignature);
00784 quantum_info->min_is_white=min_is_white;
00785 }
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812 MagickExport void SetQuantumQuantum(QuantumInfo *quantum_info,
00813 const unsigned long quantum)
00814 {
00815 assert(quantum_info != (QuantumInfo *) NULL);
00816 assert(quantum_info->signature == MagickSignature);
00817 quantum_info->quantum=quantum;
00818 }
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844 MagickExport void SetQuantumScale(QuantumInfo *quantum_info,const double scale)
00845 {
00846 assert(quantum_info != (QuantumInfo *) NULL);
00847 assert(quantum_info->signature == MagickSignature);
00848 quantum_info->scale=scale;
00849 }