MagickCore  7.1.2-29
Convert, Edit, Or Compose Bitmap Images
quantum.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % QQQ U U AAA N N TTTTT U U M M %
7 % Q Q U U A A NN N T U U MM MM %
8 % Q Q U U AAAAA N N N T U U M M M %
9 % Q QQ U U A A N NN T U U M M %
10 % QQQQ UUU A A N N T UUU M M %
11 % %
12 % MagicCore Methods to Acquire / Destroy Quantum Pixels %
13 % %
14 % Software Design %
15 % Cristy %
16 % October 1998 %
17 % %
18 % %
19 % Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
21 % %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
24 % %
25 % https://imagemagick.org/license/ %
26 % %
27 % Unless required by applicable law or agreed to in writing, software %
28 % distributed under the License is distributed on an "AS IS" BASIS, %
29 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30 % See the License for the specific language governing permissions and %
31 % limitations under the License. %
32 % %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %
35 %
36 */
37 ␌
38 /*
39  Include declarations.
40 */
41 #include "MagickCore/studio.h"
42 #include "MagickCore/attribute.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/cache-private.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/delegate.h"
54 #include "MagickCore/geometry.h"
55 #include "MagickCore/list.h"
56 #include "MagickCore/magick.h"
57 #include "MagickCore/memory_.h"
58 #include "MagickCore/memory-private.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/option.h"
61 #include "MagickCore/pixel.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/property.h"
64 #include "MagickCore/quantum.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/resource_.h"
67 #include "MagickCore/semaphore.h"
68 #include "MagickCore/statistic.h"
69 #include "MagickCore/stream.h"
70 #include "MagickCore/string_.h"
71 #include "MagickCore/string-private.h"
72 #include "MagickCore/thread-private.h"
73 #include "MagickCore/utility.h"
74 ␌
75 /*
76  Define declarations.
77 */
78 #define QuantumSignature 0xab
79 ␌
80 /*
81  Forward declarations.
82 */
83 static void
84  DestroyQuantumPixels(QuantumInfo *);
85 ␌
86 /*
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % %
89 % %
90 % %
91 % A c q u i r e Q u a n t u m I n f o %
92 % %
93 % %
94 % %
95 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 %
97 % AcquireQuantumInfo() allocates the QuantumInfo structure.
98 %
99 % The format of the AcquireQuantumInfo method is:
100 %
101 % QuantumInfo *AcquireQuantumInfo(const ImageInfo *image_info,Image *image)
102 %
103 % A description of each parameter follows:
104 %
105 % o image_info: the image info.
106 %
107 % o image: the image.
108 %
109 */
110 MagickExport QuantumInfo *AcquireQuantumInfo(const ImageInfo *image_info,
111  Image *image)
112 {
113  MagickBooleanType
114  status;
115 
117  *quantum_info;
118 
119  quantum_info=(QuantumInfo *) AcquireCriticalMemory(sizeof(*quantum_info));
120  quantum_info->signature=MagickCoreSignature;
121  GetQuantumInfo(image_info,quantum_info);
122  if (image == (const Image *) NULL)
123  return(quantum_info);
124  status=SetQuantumDepth(image,quantum_info,image->depth);
125  quantum_info->endian=image->endian;
126  if (status == MagickFalse)
127  quantum_info=DestroyQuantumInfo(quantum_info);
128  return(quantum_info);
129 }
130 ␌
131 /*
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 % %
134 % %
135 % %
136 + A c q u i r e Q u a n t u m P i x e l s %
137 % %
138 % %
139 % %
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 %
142 % AcquireQuantumPixels() allocates the pixel staging areas.
143 %
144 % The format of the AcquireQuantumPixels method is:
145 %
146 % MagickBooleanType AcquireQuantumPixels(QuantumInfo *quantum_info,
147 % const size_t extent)
148 %
149 % A description of each parameter follows:
150 %
151 % o quantum_info: the quantum info.
152 %
153 % o extent: the quantum info.
154 %
155 */
156 static MagickBooleanType AcquireQuantumPixels(QuantumInfo *quantum_info,
157  const size_t extent)
158 {
159  ssize_t
160  i;
161 
162  assert(quantum_info != (QuantumInfo *) NULL);
163  assert(quantum_info->signature == MagickCoreSignature);
164  quantum_info->number_threads=(size_t) GetMagickResourceLimit(ThreadResource);
165  quantum_info->pixels=(MemoryInfo **) AcquireQuantumMemory(
166  quantum_info->number_threads,sizeof(*quantum_info->pixels));
167  if (quantum_info->pixels == (MemoryInfo **) NULL)
168  return(MagickFalse);
169  quantum_info->extent=extent;
170  (void) memset(quantum_info->pixels,0,quantum_info->number_threads*
171  sizeof(*quantum_info->pixels));
172  for (i=0; i < (ssize_t) quantum_info->number_threads; i++)
173  {
174  unsigned char
175  *pixels;
176 
177  quantum_info->pixels[i]=AcquireVirtualMemory(extent+1,sizeof(*pixels));
178  if (quantum_info->pixels[i] == (MemoryInfo *) NULL)
179  {
180  DestroyQuantumPixels(quantum_info);
181  return(MagickFalse);
182  }
183  pixels=(unsigned char *) GetVirtualMemoryBlob(quantum_info->pixels[i]);
184  (void) memset(pixels,0,(extent+1)*sizeof(*pixels));
185  pixels[extent]=QuantumSignature;
186  }
187  return(MagickTrue);
188 }
189 ␌
190 /*
191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 % %
193 % %
194 % %
195 % D e s t r o y Q u a n t u m I n f o %
196 % %
197 % %
198 % %
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 %
201 % DestroyQuantumInfo() deallocates memory associated with the QuantumInfo
202 % structure.
203 %
204 % The format of the DestroyQuantumInfo method is:
205 %
206 % QuantumInfo *DestroyQuantumInfo(QuantumInfo *quantum_info)
207 %
208 % A description of each parameter follows:
209 %
210 % o quantum_info: the quantum info.
211 %
212 */
213 MagickExport QuantumInfo *DestroyQuantumInfo(QuantumInfo *quantum_info)
214 {
215  assert(quantum_info != (QuantumInfo *) NULL);
216  assert(quantum_info->signature == MagickCoreSignature);
217  if (quantum_info->pixels != (MemoryInfo **) NULL)
218  DestroyQuantumPixels(quantum_info);
219  if (quantum_info->semaphore != (SemaphoreInfo *) NULL)
220  RelinquishSemaphoreInfo(&quantum_info->semaphore);
221  quantum_info->signature=(~MagickCoreSignature);
222  quantum_info=(QuantumInfo *) RelinquishMagickMemory(quantum_info);
223  return(quantum_info);
224 }
225 ␌
226 /*
227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 % %
229 % %
230 % %
231 + D e s t r o y Q u a n t u m P i x e l s %
232 % %
233 % %
234 % %
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 %
237 % DestroyQuantumPixels() destroys the quantum pixels.
238 %
239 % The format of the DestroyQuantumPixels() method is:
240 %
241 % void DestroyQuantumPixels(QuantumInfo *quantum_info)
242 %
243 % A description of each parameter follows:
244 %
245 % o quantum_info: the quantum info.
246 %
247 */
248 static void DestroyQuantumPixels(QuantumInfo *quantum_info)
249 {
250  ssize_t
251  i;
252 
253  assert(quantum_info != (QuantumInfo *) NULL);
254  assert(quantum_info->signature == MagickCoreSignature);
255  assert(quantum_info->pixels != (MemoryInfo **) NULL);
256  for (i=0; i < (ssize_t) quantum_info->number_threads; i++)
257  if (quantum_info->pixels[i] != (MemoryInfo *) NULL)
258  {
259 #ifndef NDEBUG
260  ssize_t
261  extent;
262 
263  unsigned char
264  *pixels;
265 
266  /*
267  Did we overrun our quantum buffer?
268  */
269  extent=(ssize_t) quantum_info->extent;
270  pixels=(unsigned char *) GetVirtualMemoryBlob(quantum_info->pixels[i]);
271  assert(pixels[extent] == QuantumSignature);
272 #endif
273  quantum_info->pixels[i]=RelinquishVirtualMemory(
274  quantum_info->pixels[i]);
275  }
276  quantum_info->pixels=(MemoryInfo **) RelinquishMagickMemory(
277  quantum_info->pixels);
278 }
279 ␌
280 /*
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % %
283 % %
284 % %
285 % G e t Q u a n t u m E x t e n t %
286 % %
287 % %
288 % %
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 %
291 % GetQuantumExtent() returns the quantum pixel buffer extent.
292 %
293 % The format of the GetQuantumExtent method is:
294 %
295 % size_t GetQuantumExtent(Image *image,const QuantumInfo *quantum_info,
296 % const QuantumType quantum_type)
297 %
298 % A description of each parameter follows:
299 %
300 % o image: the image.
301 %
302 % o quantum_info: the quantum info.
303 %
304 % o quantum_type: Declare which pixel components to transfer (red, green,
305 % blue, opacity, RGB, or RGBA).
306 %
307 */
308 MagickExport size_t GetQuantumExtent(const Image *image,
309  const QuantumInfo *quantum_info,const QuantumType quantum_type)
310 {
311  size_t
312  channels;
313 
314  assert(quantum_info != (QuantumInfo *) NULL);
315  assert(quantum_info->signature == MagickCoreSignature);
316  channels=1;
317  switch (quantum_type)
318  {
319  case GrayAlphaQuantum: channels=2; break;
320  case IndexAlphaQuantum: channels=2; break;
321  case RGBQuantum: channels=3; break;
322  case BGRQuantum: channels=3; break;
323  case RGBAQuantum: channels=4; break;
324  case RGBOQuantum: channels=4; break;
325  case BGRAQuantum: channels=4; break;
326  case CMYKQuantum: channels=4; break;
327  case CMYKAQuantum: channels=5; break;
328  case MultispectralQuantum: channels=GetImageChannels(image); break;
329  case CbYCrAQuantum: channels=4; break;
330  case CbYCrQuantum: channels=3; break;
331  case CbYCrYQuantum: channels=4; break;
332  default: break;
333  }
334  if (quantum_info->pack == MagickFalse)
335  return((size_t) (channels*image->columns*((quantum_info->depth+7)/8))+
336  (quantum_info->pad*image->columns));
337  return((size_t) ((channels*image->columns*quantum_info->depth+7)/8)+
338  (quantum_info->pad*image->columns));
339 }
340 ␌
341 /*
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 % %
344 % %
345 % %
346 % G e t Q u a n t u m E n d i a n %
347 % %
348 % %
349 % %
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351 %
352 % GetQuantumEndian() returns the quantum endian of the image.
353 %
354 % The endian of the GetQuantumEndian method is:
355 %
356 % EndianType GetQuantumEndian(const QuantumInfo *quantum_info)
357 %
358 % A description of each parameter follows:
359 %
360 % o quantum_info: the quantum info.
361 %
362 */
363 MagickExport EndianType GetQuantumEndian(const QuantumInfo *quantum_info)
364 {
365  assert(quantum_info != (QuantumInfo *) NULL);
366  assert(quantum_info->signature == MagickCoreSignature);
367  return(quantum_info->endian);
368 }
369 ␌
370 /*
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 % %
373 % %
374 % %
375 % G e t Q u a n t u m F o r m a t %
376 % %
377 % %
378 % %
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 %
381 % GetQuantumFormat() returns the quantum format of the image.
382 %
383 % The format of the GetQuantumFormat method is:
384 %
385 % QuantumFormatType GetQuantumFormat(const QuantumInfo *quantum_info)
386 %
387 % A description of each parameter follows:
388 %
389 % o quantum_info: the quantum info.
390 %
391 */
392 MagickExport QuantumFormatType GetQuantumFormat(const QuantumInfo *quantum_info)
393 {
394  assert(quantum_info != (QuantumInfo *) NULL);
395  assert(quantum_info->signature == MagickCoreSignature);
396  return(quantum_info->format);
397 }
398 ␌
399 /*
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 % %
402 % %
403 % %
404 % G e t Q u a n t u m I n f o %
405 % %
406 % %
407 % %
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 %
410 % GetQuantumInfo() initializes the QuantumInfo structure to default values.
411 %
412 % The format of the GetQuantumInfo method is:
413 %
414 % GetQuantumInfo(const ImageInfo *image_info,QuantumInfo *quantum_info)
415 %
416 % A description of each parameter follows:
417 %
418 % o image_info: the image info.
419 %
420 % o quantum_info: the quantum info.
421 %
422 */
423 MagickExport void GetQuantumInfo(const ImageInfo *image_info,
424  QuantumInfo *quantum_info)
425 {
426  const char
427  *option;
428 
429  assert(quantum_info != (QuantumInfo *) NULL);
430  (void) memset(quantum_info,0,sizeof(*quantum_info));
431  quantum_info->quantum=8;
432  quantum_info->maximum=1.0;
433  quantum_info->scale=QuantumRange;
434  quantum_info->pack=MagickTrue;
435  quantum_info->semaphore=AcquireSemaphoreInfo();
436  quantum_info->signature=MagickCoreSignature;
437  if (image_info == (const ImageInfo *) NULL)
438  return;
439  option=GetImageOption(image_info,"quantum:format");
440  if (option != (char *) NULL)
441  quantum_info->format=(QuantumFormatType) ParseCommandOption(
442  MagickQuantumFormatOptions,MagickFalse,option);
443  option=GetImageOption(image_info,"quantum:minimum");
444  if (option != (char *) NULL)
445  quantum_info->minimum=StringToDouble(option,(char **) NULL);
446  option=GetImageOption(image_info,"quantum:maximum");
447  if (option != (char *) NULL)
448  quantum_info->maximum=StringToDouble(option,(char **) NULL);
449  if ((quantum_info->minimum == 0.0) && (quantum_info->maximum == 0.0))
450  quantum_info->scale=0.0;
451  else
452  if (quantum_info->minimum == quantum_info->maximum)
453  {
454  quantum_info->scale=(double) QuantumRange/quantum_info->minimum;
455  quantum_info->minimum=0.0;
456  }
457  else
458  quantum_info->scale=(double) QuantumRange/(quantum_info->maximum-
459  quantum_info->minimum);
460  option=GetImageOption(image_info,"quantum:scale");
461  if (option != (char *) NULL)
462  quantum_info->scale=StringToDouble(option,(char **) NULL);
463  option=GetImageOption(image_info,"quantum:polarity");
464  if (option != (char *) NULL)
465  quantum_info->min_is_white=LocaleCompare(option,"min-is-white") == 0 ?
466  MagickTrue : MagickFalse;
467  quantum_info->endian=image_info->endian;
468  ResetQuantumState(quantum_info);
469 }
470 ␌
471 /*
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 % %
474 % %
475 % %
476 % G e t Q u a n t u m P i x e l s %
477 % %
478 % %
479 % %
480 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 %
482 % GetQuantumPixels() returns the quantum pixels.
483 %
484 % The format of the GetQuantumPixels method is:
485 %
486 % unsigned char *QuantumPixels GetQuantumPixels(
487 % const QuantumInfo *quantum_info)
488 %
489 % A description of each parameter follows:
490 %
491 % o image: the image.
492 %
493 */
494 MagickExport unsigned char *GetQuantumPixels(const QuantumInfo *quantum_info)
495 {
496  const int
497  id = GetOpenMPThreadId();
498 
499  assert(quantum_info != (QuantumInfo *) NULL);
500  assert(quantum_info->signature == MagickCoreSignature);
501  return((unsigned char *) GetVirtualMemoryBlob(quantum_info->pixels[id]));
502 }
503 ␌
504 /*
505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
506 % %
507 % %
508 % %
509 % G e t Q u a n t u m T y p e %
510 % %
511 % %
512 % %
513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 %
515 % GetQuantumType() returns the quantum type of the image.
516 %
517 % The format of the GetQuantumType method is:
518 %
519 % QuantumType GetQuantumType(Image *image,ExceptionInfo *exception)
520 %
521 % A description of each parameter follows:
522 %
523 % o image: the image.
524 %
525 % o exception: return any errors or warnings in this structure.
526 %
527 */
528 MagickExport QuantumType GetQuantumType(Image *image,ExceptionInfo *exception)
529 {
530  QuantumType
531  quantum_type;
532 
533  assert(image != (Image *) NULL);
534  assert(image->signature == MagickCoreSignature);
535  if (IsEventLogging() != MagickFalse)
536  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
537  (void) exception;
538  quantum_type=RGBQuantum;
539  if (image->alpha_trait != UndefinedPixelTrait)
540  quantum_type=RGBAQuantum;
541  if (image->colorspace == CMYKColorspace)
542  {
543  quantum_type=CMYKQuantum;
544  if (image->alpha_trait != UndefinedPixelTrait)
545  quantum_type=CMYKAQuantum;
546  }
547  if (IsGrayColorspace(image->colorspace) != MagickFalse)
548  {
549  quantum_type=GrayQuantum;
550  if (image->alpha_trait != UndefinedPixelTrait)
551  quantum_type=GrayAlphaQuantum;
552  }
553  if (image->storage_class == PseudoClass)
554  {
555  quantum_type=IndexQuantum;
556  if (image->alpha_trait != UndefinedPixelTrait)
557  quantum_type=IndexAlphaQuantum;
558  }
559  if (image->number_meta_channels != 0)
560  quantum_type=MultispectralQuantum;
561  return(quantum_type);
562 }
563 ␌
564 /*
565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % %
567 % %
568 % %
569 + R e s e t Q u a n t u m S t a t e %
570 % %
571 % %
572 % %
573 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 %
575 % ResetQuantumState() resets the quantum state.
576 %
577 % The format of the ResetQuantumState method is:
578 %
579 % void ResetQuantumState(QuantumInfo *quantum_info)
580 %
581 % A description of each parameter follows:
582 %
583 % o quantum_info: the quantum info.
584 %
585 */
586 MagickPrivate void ResetQuantumState(QuantumInfo *quantum_info)
587 {
588  static const unsigned int mask[32] =
589  {
590  0x00000000U, 0x00000001U, 0x00000003U, 0x00000007U, 0x0000000fU,
591  0x0000001fU, 0x0000003fU, 0x0000007fU, 0x000000ffU, 0x000001ffU,
592  0x000003ffU, 0x000007ffU, 0x00000fffU, 0x00001fffU, 0x00003fffU,
593  0x00007fffU, 0x0000ffffU, 0x0001ffffU, 0x0003ffffU, 0x0007ffffU,
594  0x000fffffU, 0x001fffffU, 0x003fffffU, 0x007fffffU, 0x00ffffffU,
595  0x01ffffffU, 0x03ffffffU, 0x07ffffffU, 0x0fffffffU, 0x1fffffffU,
596  0x3fffffffU, 0x7fffffffU
597  };
598 
599  assert(quantum_info != (QuantumInfo *) NULL);
600  assert(quantum_info->signature == MagickCoreSignature);
601  quantum_info->state.inverse_scale=1.0;
602  if (fabs(quantum_info->scale) >= MagickEpsilon)
603  quantum_info->state.inverse_scale/=quantum_info->scale;
604  quantum_info->state.pixel=0U;
605  quantum_info->state.bits=0U;
606  quantum_info->state.mask=mask;
607 }
608 ␌
609 /*
610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
611 % %
612 % %
613 % %
614 % S e t Q u a n t u m F o r m a t %
615 % %
616 % %
617 % %
618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619 %
620 % SetQuantumAlphaType() sets the quantum format.
621 %
622 % The format of the SetQuantumAlphaType method is:
623 %
624 % void SetQuantumAlphaType(QuantumInfo *quantum_info,
625 % const QuantumAlphaType type)
626 %
627 % A description of each parameter follows:
628 %
629 % o quantum_info: the quantum info.
630 %
631 % o type: the alpha type (e.g. associate).
632 %
633 */
634 MagickExport void SetQuantumAlphaType(QuantumInfo *quantum_info,
635  const QuantumAlphaType type)
636 {
637  assert(quantum_info != (QuantumInfo *) NULL);
638  assert(quantum_info->signature == MagickCoreSignature);
639  quantum_info->alpha_type=type;
640 }
641 ␌
642 /*
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % %
645 % %
646 % %
647 % S e t Q u a n t u m D e p t h %
648 % %
649 % %
650 % %
651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 %
653 % SetQuantumDepth() sets the quantum depth.
654 %
655 % The format of the SetQuantumDepth method is:
656 %
657 % MagickBooleanType SetQuantumDepth(const Image *image,
658 % QuantumInfo *quantum_info,const size_t depth)
659 %
660 % A description of each parameter follows:
661 %
662 % o image: the image.
663 %
664 % o quantum_info: the quantum info.
665 %
666 % o depth: the quantum depth.
667 %
668 */
669 MagickExport MagickBooleanType SetQuantumDepth(const Image *image,
670  QuantumInfo *quantum_info,const size_t depth)
671 {
672  /*
673  Allocate the quantum pixel buffer.
674  */
675  assert(image != (Image *) NULL);
676  assert(image->signature == MagickCoreSignature);
677  assert(quantum_info != (QuantumInfo *) NULL);
678  assert(quantum_info->signature == MagickCoreSignature);
679  if (IsEventLogging() != MagickFalse)
680  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
681  quantum_info->depth=MagickMin(depth,64);
682  if (quantum_info->format == FloatingPointQuantumFormat)
683  {
684  if (quantum_info->depth > 32)
685  quantum_info->depth=64;
686  else
687  if (quantum_info->depth > 24)
688  quantum_info->depth=32;
689  else
690  if (quantum_info->depth > 16)
691  quantum_info->depth=24;
692  else
693  quantum_info->depth=16;
694  }
695  return(SetQuantumExtent(image,quantum_info));
696 }
697 ␌
698 /*
699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 % %
701 % %
702 % %
703 % S e t Q u a n t u m E n d i a n %
704 % %
705 % %
706 % %
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
708 %
709 % SetQuantumEndian() sets the quantum endian.
710 %
711 % The endian of the SetQuantumEndian method is:
712 %
713 % MagickBooleanType SetQuantumEndian(const Image *image,
714 % QuantumInfo *quantum_info,const EndianType endian)
715 %
716 % A description of each parameter follows:
717 %
718 % o image: the image.
719 %
720 % o quantum_info: the quantum info.
721 %
722 % o endian: the quantum endian.
723 %
724 */
725 MagickExport MagickBooleanType SetQuantumEndian(const Image *image,
726  QuantumInfo *quantum_info,const EndianType endian)
727 {
728  assert(image != (Image *) NULL);
729  assert(image->signature == MagickCoreSignature);
730  assert(quantum_info != (QuantumInfo *) NULL);
731  assert(quantum_info->signature == MagickCoreSignature);
732  if (IsEventLogging() != MagickFalse)
733  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
734  quantum_info->endian=endian;
735  return(SetQuantumExtent(image,quantum_info));
736 }
737 ␌
738 /*
739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740 % %
741 % %
742 % %
743 % S e t Q u a n t u m E x t e n t %
744 % %
745 % %
746 % %
747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
748 %
749 % SetQuantumExtent() sets the quantum pixel buffer extent.
750 %
751 % The format of the SetQuantumExtent method is:
752 %
753 % MagickBooleanType SetQuantumExtent(Image *image,
754 % QuantumInfo *quantum_info)
755 %
756 % A description of each parameter follows:
757 %
758 % o image: the image.
759 %
760 % o quantum_info: the quantum info.
761 %
762 */
763 MagickExport MagickBooleanType SetQuantumExtent(const Image *image,
764  QuantumInfo *quantum_info)
765 {
766  size_t
767  extent,
768  quantum;
769 
770  /*
771  Speculative allocation since we don't yet know the quantum type.
772  */
773  assert(image != (Image *) NULL);
774  assert(image->signature == MagickCoreSignature);
775  assert(quantum_info != (QuantumInfo *) NULL);
776  assert(quantum_info->signature == MagickCoreSignature);
777  quantum=(GetPixelChannels(image)+quantum_info->pad+3)*
778  ((quantum_info->depth+7)/8)*sizeof(double);
779  extent=MagickMax(image->columns,image->rows)*quantum;
780  if ((MagickMax(image->columns,image->rows) != 0) &&
781  (quantum != (extent/MagickMax(image->columns,image->rows))))
782  return(MagickFalse);
783  if (quantum_info->pixels != (MemoryInfo **) NULL)
784  {
785  if (extent <= quantum_info->extent)
786  return(MagickTrue);
787  DestroyQuantumPixels(quantum_info);
788  }
789  return(AcquireQuantumPixels(quantum_info,extent));
790 }
791 ␌
792 /*
793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 % %
795 % %
796 % %
797 % S e t Q u a n t u m F o r m a t %
798 % %
799 % %
800 % %
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 %
803 % SetQuantumFormat() sets the quantum format.
804 %
805 % The format of the SetQuantumFormat method is:
806 %
807 % MagickBooleanType SetQuantumFormat(const Image *image,
808 % QuantumInfo *quantum_info,const QuantumFormatType format)
809 %
810 % A description of each parameter follows:
811 %
812 % o image: the image.
813 %
814 % o quantum_info: the quantum info.
815 %
816 % o format: the quantum format.
817 %
818 */
819 MagickExport MagickBooleanType SetQuantumFormat(const Image *image,
820  QuantumInfo *quantum_info,const QuantumFormatType format)
821 {
822  assert(image != (Image *) NULL);
823  assert(image->signature == MagickCoreSignature);
824  assert(quantum_info != (QuantumInfo *) NULL);
825  assert(quantum_info->signature == MagickCoreSignature);
826  if (IsEventLogging() != MagickFalse)
827  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
828  quantum_info->format=format;
829  return(SetQuantumExtent(image,quantum_info));
830 }
831 ␌
832 /*
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834 % %
835 % %
836 % %
837 % S e t Q u a n t u m I m a g e T y p e %
838 % %
839 % %
840 % %
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
842 %
843 % SetQuantumImageType() sets the image type based on the quantum type.
844 %
845 % The format of the SetQuantumImageType method is:
846 %
847 % void ImageType SetQuantumImageType(Image *image,
848 % const QuantumType quantum_type)
849 %
850 % A description of each parameter follows:
851 %
852 % o image: the image.
853 %
854 % o quantum_type: Declare which pixel components to transfer (red, green,
855 % blue, opacity, RGB, or RGBA).
856 %
857 */
858 MagickExport void SetQuantumImageType(Image *image,
859  const QuantumType quantum_type)
860 {
861  assert(image != (Image *) NULL);
862  assert(image->signature == MagickCoreSignature);
863  if (IsEventLogging() != MagickFalse)
864  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
865  switch (quantum_type)
866  {
867  case IndexQuantum:
868  case IndexAlphaQuantum:
869  {
870  image->type=PaletteType;
871  break;
872  }
873  case GrayQuantum:
874  case GrayAlphaQuantum:
875  {
876  image->type=GrayscaleType;
877  if (image->depth == 1)
878  image->type=BilevelType;
879  break;
880  }
881  case CyanQuantum:
882  case MagentaQuantum:
883  case YellowQuantum:
884  case BlackQuantum:
885  case CMYKQuantum:
886  case CMYKAQuantum:
887  case MultispectralQuantum:
888  {
889  image->type=ColorSeparationType;
890  break;
891  }
892  default:
893  {
894  image->type=TrueColorType;
895  break;
896  }
897  }
898 }
899 ␌
900 /*
901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902 % %
903 % %
904 % %
905 % S e t Q u a n t u m M e t a C h a n n e l %
906 % %
907 % %
908 % %
909 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 %
911 % SetQuantumMetaChannel() sets the quantum meta-channel that will exported or
912 % imported when the quantum type is MultispectralQuantum. To unset the
913 % meta-channel, set the meta-channel to -1.
914 %
915 % The format of the SetQuantumMetaChannel method is:
916 %
917 % void SetQuantumMetaChannel(const Image *image,QuantumInfo *quantum_info,
918 % const ssize_t meta_channel)
919 %
920 % A description of each parameter follows:
921 %
922 % o image: the image.
923 %
924 % o quantum_info: the quantum info.
925 %
926 % o meta_channel: the meta channel.
927 %
928 */
929 MagickExport MagickBooleanType SetQuantumMetaChannel(const Image *image,
930  QuantumInfo *quantum_info, const ssize_t meta_channel)
931 {
932  assert(quantum_info != (QuantumInfo *) NULL);
933  assert(quantum_info->signature == MagickCoreSignature);
934  if ((meta_channel < -1) ||
935  (meta_channel >= (ssize_t) image->number_meta_channels))
936  return(MagickFalse);
937  quantum_info->meta_channel=meta_channel+1;
938  return(MagickTrue);
939 }
940 ␌
941 /*
942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
943 % %
944 % %
945 % %
946 % S e t Q u a n t u m P a c k %
947 % %
948 % %
949 % %
950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
951 %
952 % SetQuantumPack() sets the quantum pack flag.
953 %
954 % The format of the SetQuantumPack method is:
955 %
956 % void SetQuantumPack(QuantumInfo *quantum_info,
957 % const MagickBooleanType pack)
958 %
959 % A description of each parameter follows:
960 %
961 % o quantum_info: the quantum info.
962 %
963 % o pack: the pack flag.
964 %
965 */
966 MagickExport void SetQuantumPack(QuantumInfo *quantum_info,
967  const MagickBooleanType pack)
968 {
969  assert(quantum_info != (QuantumInfo *) NULL);
970  assert(quantum_info->signature == MagickCoreSignature);
971  quantum_info->pack=pack;
972 }
973 ␌
974 /*
975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 % %
977 % %
978 % %
979 % S e t Q u a n t u m P a d %
980 % %
981 % %
982 % %
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984 %
985 % SetQuantumPad() sets the quantum pad.
986 %
987 % The format of the SetQuantumPad method is:
988 %
989 % MagickBooleanType SetQuantumPad(const Image *image,
990 % QuantumInfo *quantum_info,const size_t pad)
991 %
992 % A description of each parameter follows:
993 %
994 % o image: the image.
995 %
996 % o quantum_info: the quantum info.
997 %
998 % o pad: the quantum pad.
999 %
1000 */
1001 MagickExport MagickBooleanType SetQuantumPad(const Image *image,
1002  QuantumInfo *quantum_info,const size_t pad)
1003 {
1004  assert(image != (Image *) NULL);
1005  assert(image->signature == MagickCoreSignature);
1006  assert(quantum_info != (QuantumInfo *) NULL);
1007  assert(quantum_info->signature == MagickCoreSignature);
1008  if (IsEventLogging() != MagickFalse)
1009  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1010  if (pad >= (MAGICK_SSIZE_MAX/GetImageChannels(image)))
1011  return(MagickFalse);
1012  quantum_info->pad=pad;
1013  return(SetQuantumExtent(image,quantum_info));
1014 }
1015 ␌
1016 /*
1017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1018 % %
1019 % %
1020 % %
1021 % S e t Q u a n t u m M i n I s W h i t e %
1022 % %
1023 % %
1024 % %
1025 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1026 %
1027 % SetQuantumMinIsWhite() sets the quantum min-is-white flag.
1028 %
1029 % The format of the SetQuantumMinIsWhite method is:
1030 %
1031 % void SetQuantumMinIsWhite(QuantumInfo *quantum_info,
1032 % const MagickBooleanType min_is_white)
1033 %
1034 % A description of each parameter follows:
1035 %
1036 % o quantum_info: the quantum info.
1037 %
1038 % o min_is_white: the min-is-white flag.
1039 %
1040 */
1041 MagickExport void SetQuantumMinIsWhite(QuantumInfo *quantum_info,
1042  const MagickBooleanType min_is_white)
1043 {
1044  assert(quantum_info != (QuantumInfo *) NULL);
1045  assert(quantum_info->signature == MagickCoreSignature);
1046  quantum_info->min_is_white=min_is_white;
1047 }
1048 ␌
1049 /*
1050 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1051 % %
1052 % %
1053 % %
1054 % S e t Q u a n t u m Q u a n t u m %
1055 % %
1056 % %
1057 % %
1058 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1059 %
1060 % SetQuantumQuantum() sets the quantum quantum.
1061 %
1062 % The format of the SetQuantumQuantum method is:
1063 %
1064 % void SetQuantumQuantum(QuantumInfo *quantum_info,const size_t quantum)
1065 %
1066 % A description of each parameter follows:
1067 %
1068 % o quantum_info: the quantum info.
1069 %
1070 % o quantum: the quantum quantum.
1071 %
1072 */
1073 MagickExport void SetQuantumQuantum(QuantumInfo *quantum_info,
1074  const size_t quantum)
1075 {
1076  assert(quantum_info != (QuantumInfo *) NULL);
1077  assert(quantum_info->signature == MagickCoreSignature);
1078  quantum_info->quantum=quantum;
1079 }
1080 ␌
1081 /*
1082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1083 % %
1084 % %
1085 % %
1086 % S e t Q u a n t u m S c a l e %
1087 % %
1088 % %
1089 % %
1090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1091 %
1092 % SetQuantumScale() sets the quantum scale.
1093 %
1094 % The format of the SetQuantumScale method is:
1095 %
1096 % void SetQuantumScale(QuantumInfo *quantum_info,const double scale)
1097 %
1098 % A description of each parameter follows:
1099 %
1100 % o quantum_info: the quantum info.
1101 %
1102 % o scale: the quantum scale.
1103 %
1104 */
1105 MagickExport void SetQuantumScale(QuantumInfo *quantum_info,const double scale)
1106 {
1107  assert(quantum_info != (QuantumInfo *) NULL);
1108  assert(quantum_info->signature == MagickCoreSignature);
1109  quantum_info->scale=scale;
1110 }
Definition: image.h:132