MagickCore  6.9.9
Convert, Edit, Or Compose Bitmap Images
quantum-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization
3  dedicated to making software imaging solutions freely available.
4 
5  You may not use this file except in compliance with the License.
6  obtain a copy of the License at
7 
8  https://www.imagemagick.org/script/license.php
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16  MagickCore quantum inline methods.
17 */
18 #ifndef MAGICKCORE_QUANTUM_PRIVATE_H
19 #define MAGICKCORE_QUANTUM_PRIVATE_H
20 
21 #include "magick/memory_.h"
22 #include "magick/cache.h"
23 
24 #if defined(__cplusplus) || defined(c_plusplus)
25 extern "C" {
26 #endif
27 
28 typedef struct _QuantumState
29 {
30  double
32 
33  unsigned int
35 
36  size_t
38 
39  const unsigned int
40  *mask;
41 } QuantumState;
42 
44 {
45  size_t
46  depth,
47  quantum;
48 
51 
52  double
53  minimum,
54  maximum,
55  scale;
56 
57  size_t
58  pad;
59 
61  min_is_white,
62  pack;
63 
66 
67  size_t
69 
71  **pixels;
72 
73  size_t
75 
78 
81 
84 
85  size_t
87 };
88 
89 extern MagickPrivate void
91 
92 static inline MagickSizeType GetQuantumRange(const size_t depth)
93 {
95  one;
96 
97  one=1;
98  return((MagickSizeType) ((one << (depth-1))+((one << (depth-1))-1)));
99 }
100 
101 static inline float HalfToSinglePrecision(const unsigned short half)
102 {
103 #define ExponentBias (127-15)
104 #define ExponentMask 0x7c00
105 #define ExponentShift 23
106 #define SignBitShift 31
107 #define SignificandShift 13
108 #define SignificandMask 0x00000400
109 
110  typedef union _SinglePrecision
111  {
112  unsigned int
113  fixed_point;
114 
115  float
116  single_precision;
117  } SinglePrecision;
118 
119  register unsigned int
120  exponent,
121  significand,
122  sign_bit;
123 
124  SinglePrecision
125  map;
126 
127  unsigned int
128  value;
129 
130  /*
131  The IEEE 754 standard specifies half precision as having:
132 
133  Sign bit: 1 bit
134  Exponent width: 5 bits
135  Significand precision: 11 (10 explicitly stored)
136  */
137  sign_bit=(unsigned int) ((half >> 15) & 0x00000001);
138  exponent=(unsigned int) ((half >> 10) & 0x0000001f);
139  significand=(unsigned int) (half & 0x000003ff);
140  if (exponent == 0)
141  {
142  if (significand == 0)
143  value=sign_bit << SignBitShift;
144  else
145  {
146  while ((significand & SignificandMask) == 0)
147  {
148  significand<<=1;
149  exponent--;
150  }
151  exponent++;
152  significand&=(~SignificandMask);
153  exponent+=ExponentBias;
154  value=(sign_bit << SignBitShift) | (exponent << ExponentShift) |
155  (significand << SignificandShift);
156  }
157  }
158  else
159  if (exponent == SignBitShift)
160  {
161  value=(sign_bit << SignBitShift) | 0x7f800000;
162  if (significand != 0)
163  value|=(significand << SignificandShift);
164  }
165  else
166  {
167  exponent+=ExponentBias;
168  significand<<=SignificandShift;
169  value=(sign_bit << SignBitShift) | (exponent << ExponentShift) |
170  significand;
171  }
172  map.fixed_point=value;
173  return(map.single_precision);
174 }
175 
176 static inline unsigned char *PopCharPixel(const unsigned char pixel,
177  unsigned char *pixels)
178 {
179  *pixels++=pixel;
180  return(pixels);
181 }
182 
183 static inline unsigned char *PopLongPixel(const EndianType endian,
184  const unsigned int pixel,unsigned char *pixels)
185 {
186  register unsigned int
187  quantum;
188 
189  quantum=(unsigned int) pixel;
190  if (endian == LSBEndian)
191  {
192  *pixels++=(unsigned char) (quantum);
193  *pixels++=(unsigned char) (quantum >> 8);
194  *pixels++=(unsigned char) (quantum >> 16);
195  *pixels++=(unsigned char) (quantum >> 24);
196  return(pixels);
197  }
198  *pixels++=(unsigned char) (quantum >> 24);
199  *pixels++=(unsigned char) (quantum >> 16);
200  *pixels++=(unsigned char) (quantum >> 8);
201  *pixels++=(unsigned char) (quantum);
202  return(pixels);
203 }
204 
205 static inline unsigned char *PopShortPixel(const EndianType endian,
206  const unsigned short pixel,unsigned char *pixels)
207 {
208  register unsigned int
209  quantum;
210 
211  quantum=pixel;
212  if (endian == LSBEndian)
213  {
214  *pixels++=(unsigned char) (quantum);
215  *pixels++=(unsigned char) (quantum >> 8);
216  return(pixels);
217  }
218  *pixels++=(unsigned char) (quantum >> 8);
219  *pixels++=(unsigned char) (quantum);
220  return(pixels);
221 }
222 
223 static inline const unsigned char *PushCharPixel(const unsigned char *pixels,
224  unsigned char *pixel)
225 {
226  *pixel=(*pixels++);
227  return(pixels);
228 }
229 
230 static inline const unsigned char *PushLongPixel(const EndianType endian,
231  const unsigned char *pixels,unsigned int *pixel)
232 {
233  register unsigned int
234  quantum;
235 
236  if (endian == LSBEndian)
237  {
238  quantum=((unsigned int) *pixels++);
239  quantum|=((unsigned int) *pixels++ << 8);
240  quantum|=((unsigned int) *pixels++ << 16);
241  quantum|=((unsigned int) *pixels++ << 24);
242  *pixel=quantum;
243  return(pixels);
244  }
245  quantum=((unsigned int) *pixels++ << 24);
246  quantum|=((unsigned int) *pixels++ << 16);
247  quantum|=((unsigned int) *pixels++ << 8);
248  quantum|=((unsigned int) *pixels++);
249  *pixel=quantum;
250  return(pixels);
251 }
252 
253 static inline const unsigned char *PushShortPixel(const EndianType endian,
254  const unsigned char *pixels,unsigned short *pixel)
255 {
256  register unsigned int
257  quantum;
258 
259  if (endian == LSBEndian)
260  {
261  quantum=(unsigned int) *pixels++;
262  quantum|=(unsigned int) (*pixels++ << 8);
263  *pixel=(unsigned short) (quantum & 0xffff);
264  return(pixels);
265  }
266  quantum=(unsigned int) (*pixels++ << 8);
267  quantum|=(unsigned int) *pixels++;
268  *pixel=(unsigned short) (quantum & 0xffff);
269  return(pixels);
270 }
271 
272 static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum,
273  const QuantumAny range)
274 {
275  if (quantum > range)
276  return(QuantumRange);
277 #if !defined(MAGICKCORE_HDRI_SUPPORT)
278  return((Quantum) (((MagickRealType) QuantumRange*quantum)/range+0.5));
279 #else
280  return((Quantum) (((MagickRealType) QuantumRange*quantum)/range));
281 #endif
282 }
283 
284 static inline QuantumAny ScaleQuantumToAny(const Quantum quantum,
285  const QuantumAny range)
286 {
287  return((QuantumAny) (((MagickRealType) range*quantum)/QuantumRange+0.5));
288 }
289 
290 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
291 static inline Quantum ScaleCharToQuantum(const unsigned char value)
292 {
293  return((Quantum) value);
294 }
295 
296 static inline Quantum ScaleLongToQuantum(const unsigned int value)
297 {
298 #if !defined(MAGICKCORE_HDRI_SUPPORT)
299  return((Quantum) ((value+8421504UL)/16843009UL));
300 #else
301  return((Quantum) (value/16843009.0));
302 #endif
303 }
304 
305 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
306 {
307  if (value <= 0.0)
308  return((Quantum) 0);
309  if (value >= MaxMap)
310  return(QuantumRange);
311 #if !defined(MAGICKCORE_HDRI_SUPPORT)
312  return((Quantum) (value+0.5));
313 #else
314  return((Quantum) value);
315 #endif
316 }
317 
318 static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
319 {
320 #if !defined(MAGICKCORE_HDRI_SUPPORT)
321  return((unsigned int) (16843009UL*quantum));
322 #else
323  if (quantum <= 0.0)
324  return(0UL);
325  if ((16843009.0*quantum) >= 4294967295.0)
326  return(4294967295UL);
327  return((unsigned int) (16843009.0*quantum+0.5));
328 #endif
329 }
330 
331 static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
332 {
333  if (quantum >= (Quantum) MaxMap)
334  return((unsigned int) MaxMap);
335 #if !defined(MAGICKCORE_HDRI_SUPPORT)
336  return((unsigned int) quantum);
337 #else
338  if (quantum < 0.0)
339  return(0UL);
340  return((unsigned int) (quantum+0.5));
341 #endif
342 }
343 
344 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
345 {
346 #if !defined(MAGICKCORE_HDRI_SUPPORT)
347  return((unsigned short) (257UL*quantum));
348 #else
349  if (quantum <= 0.0)
350  return(0);
351  if ((257.0*quantum) >= 65535.0)
352  return(65535);
353  return((unsigned short) (257.0*quantum+0.5));
354 #endif
355 }
356 
357 static inline Quantum ScaleShortToQuantum(const unsigned short value)
358 {
359 #if !defined(MAGICKCORE_HDRI_SUPPORT)
360  return((Quantum) ((value+128U)/257U));
361 #else
362  return((Quantum) (value/257.0));
363 #endif
364 }
365 #elif (MAGICKCORE_QUANTUM_DEPTH == 16)
366 static inline Quantum ScaleCharToQuantum(const unsigned char value)
367 {
368 #if !defined(MAGICKCORE_HDRI_SUPPORT)
369  return((Quantum) (257U*value));
370 #else
371  return((Quantum) (257.0*value));
372 #endif
373 }
374 
375 static inline Quantum ScaleLongToQuantum(const unsigned int value)
376 {
377 #if !defined(MAGICKCORE_HDRI_SUPPORT)
378  return((Quantum) ((value+MagickULLConstant(32768))/
379  MagickULLConstant(65537)));
380 #else
381  return((Quantum) (value/65537.0));
382 #endif
383 }
384 
385 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
386 {
387  if (value <= 0.0)
388  return((Quantum) 0);
389  if (value >= MaxMap)
390  return(QuantumRange);
391 #if !defined(MAGICKCORE_HDRI_SUPPORT)
392  return((Quantum) (value+0.5));
393 #else
394  return((Quantum) value);
395 #endif
396 }
397 
398 static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
399 {
400 #if !defined(MAGICKCORE_HDRI_SUPPORT)
401  return((unsigned int) (65537UL*quantum));
402 #else
403  if (quantum <= 0.0)
404  return(0UL);
405  if ((65537.0*quantum) >= 4294967295.0)
406  return(4294967295U);
407  return((unsigned int) (65537.0*quantum+0.5));
408 #endif
409 }
410 
411 static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
412 {
413  if (quantum >= (Quantum) MaxMap)
414  return((unsigned int) MaxMap);
415 #if !defined(MAGICKCORE_HDRI_SUPPORT)
416  return((unsigned int) quantum);
417 #else
418  if (quantum < 0.0)
419  return(0UL);
420  return((unsigned int) (quantum+0.5));
421 #endif
422 }
423 
424 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
425 {
426 #if !defined(MAGICKCORE_HDRI_SUPPORT)
427  return((unsigned short) quantum);
428 #else
429  if (quantum <= 0.0)
430  return(0);
431  if (quantum >= 65535.0)
432  return(65535);
433  return((unsigned short) (quantum+0.5));
434 #endif
435 }
436 
437 static inline Quantum ScaleShortToQuantum(const unsigned short value)
438 {
439  return((Quantum) value);
440 }
441 #elif (MAGICKCORE_QUANTUM_DEPTH == 32)
442 static inline Quantum ScaleCharToQuantum(const unsigned char value)
443 {
444 #if !defined(MAGICKCORE_HDRI_SUPPORT)
445  return((Quantum) (16843009UL*value));
446 #else
447  return((Quantum) (16843009.0*value));
448 #endif
449 }
450 
451 static inline Quantum ScaleLongToQuantum(const unsigned int value)
452 {
453  return((Quantum) value);
454 }
455 
456 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
457 {
458  if (value <= 0.0)
459  return((Quantum) 0);
460  if (value >= (Quantum) MaxMap)
461  return(QuantumRange);
462 #if !defined(MAGICKCORE_HDRI_SUPPORT)
463  return((Quantum) (65537.0*value+0.5));
464 #else
465  return((Quantum) (65537.0*value));
466 #endif
467 }
468 
469 static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
470 {
471 #if !defined(MAGICKCORE_HDRI_SUPPORT)
472  return((unsigned int) quantum);
473 #else
474  if (quantum <= 0.0)
475  return(0);
476  if ((quantum) >= 4294967295.0)
477  return(4294967295);
478  return((unsigned int) (quantum+0.5));
479 #endif
480 }
481 
482 static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
483 {
484  if (quantum < 0.0)
485  return(0UL);
486  if ((quantum/65537) >= (Quantum) MaxMap)
487  return((unsigned int) MaxMap);
488 #if !defined(MAGICKCORE_HDRI_SUPPORT)
489  return((unsigned int) ((quantum+MagickULLConstant(32768))/
490  MagickULLConstant(65537)));
491 #else
492  return((unsigned int) (quantum/65537.0+0.5));
493 #endif
494 }
495 
496 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
497 {
498 #if !defined(MAGICKCORE_HDRI_SUPPORT)
499  return((unsigned short) ((quantum+MagickULLConstant(32768))/
500  MagickULLConstant(65537)));
501 #else
502  if (quantum <= 0.0)
503  return(0);
504  if ((quantum/65537.0) >= 65535.0)
505  return(65535);
506  return((unsigned short) (quantum/65537.0+0.5));
507 #endif
508 }
509 
510 static inline Quantum ScaleShortToQuantum(const unsigned short value)
511 {
512 #if !defined(MAGICKCORE_HDRI_SUPPORT)
513  return((Quantum) (65537UL*value));
514 #else
515  return((Quantum) (65537.0*value));
516 #endif
517 }
518 #elif (MAGICKCORE_QUANTUM_DEPTH == 64)
519 static inline Quantum ScaleCharToQuantum(const unsigned char value)
520 {
521  return((Quantum) (72340172838076673.0*value));
522 }
523 
524 static inline Quantum ScaleLongToQuantum(const unsigned int value)
525 {
526  return((Quantum) (4294967297.0*value));
527 }
528 
529 static inline Quantum ScaleMapToQuantum(const MagickRealType value)
530 {
531  if (value <= 0.0)
532  return((Quantum) 0);
533  if (value >= MaxMap)
534  return(QuantumRange);
535  return((Quantum) (281479271743489.0*value));
536 }
537 
538 static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
539 {
540  return((unsigned int) (quantum/4294967297.0+0.5));
541 }
542 
543 static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
544 {
545  if (quantum <= 0.0)
546  return(0UL);
547  if ((quantum/281479271743489.0) >= MaxMap)
548  return((unsigned int) MaxMap);
549  return((unsigned int) (quantum/281479271743489.0+0.5));
550 }
551 
552 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
553 {
554  if (quantum <= 0.0)
555  return(0);
556  if ((quantum/281479271743489.0) >= 65535.0)
557  return(65535);
558  return((unsigned short) (quantum/281479271743489.0+0.5));
559 }
560 
561 static inline Quantum ScaleShortToQuantum(const unsigned short value)
562 {
563  return((Quantum) (281479271743489.0*value));
564 }
565 #endif
566 
567 static inline unsigned short SinglePrecisionToHalf(const float value)
568 {
569  typedef union _SinglePrecision
570  {
571  unsigned int
572  fixed_point;
573 
574  float
575  single_precision;
576  } SinglePrecision;
577 
578  register int
579  exponent;
580 
581  register unsigned int
582  significand,
583  sign_bit;
584 
585  SinglePrecision
586  map;
587 
588  unsigned short
589  half;
590 
591  /*
592  The IEEE 754 standard specifies half precision as having:
593 
594  Sign bit: 1 bit
595  Exponent width: 5 bits
596  Significand precision: 11 (10 explicitly stored)
597  */
598  map.single_precision=value;
599  sign_bit=(map.fixed_point >> 16) & 0x00008000;
600  exponent=(int) ((map.fixed_point >> ExponentShift) & 0x000000ff)-ExponentBias;
601  significand=map.fixed_point & 0x007fffff;
602  if (exponent <= 0)
603  {
604  int
605  shift;
606 
607  if (exponent < -10)
608  return((unsigned short) sign_bit);
609  significand=significand | 0x00800000;
610  shift=(int) (14-exponent);
611  significand=(unsigned int) ((significand+((1 << (shift-1))-1)+
612  ((significand >> shift) & 0x01)) >> shift);
613  return((unsigned short) (sign_bit | significand));
614  }
615  else
616  if (exponent == (0xff-ExponentBias))
617  {
618  if (significand == 0)
619  return((unsigned short) (sign_bit | ExponentMask));
620  else
621  {
622  significand>>=SignificandShift;
623  half=(unsigned short) (sign_bit | significand |
624  (significand == 0) | ExponentMask);
625  return(half);
626  }
627  }
628  significand=significand+((significand >> SignificandShift) & 0x01)+0x00000fff;
629  if ((significand & 0x00800000) != 0)
630  {
631  significand=0;
632  exponent++;
633  }
634  if (exponent > 30)
635  {
636  float
637  alpha;
638 
639  register int
640  i;
641 
642  /*
643  Float overflow.
644  */
645  alpha=1.0e10;
646  for (i=0; i < 10; i++)
647  alpha*=alpha;
648  return((unsigned short) (sign_bit | ExponentMask));
649  }
650  half=(unsigned short) (sign_bit | (exponent << 10) |
651  (significand >> SignificandShift));
652  return(half);
653 }
654 
655 #if defined(__cplusplus) || defined(c_plusplus)
656 }
657 #endif
658 
659 #endif
MagickDoubleType MagickRealType
Definition: magick-type.h:123
QuantumFormatType
Definition: quantum.h:44
QuantumFormatType format
Definition: quantum-private.h:50
static MagickSizeType GetQuantumRange(const size_t depth)
Definition: quantum-private.h:92
MemoryInfo ** pixels
Definition: quantum-private.h:71
#define ExponentMask
QuantumAlphaType alpha_type
Definition: quantum-private.h:65
size_t signature
Definition: quantum-private.h:86
#define MagickULLConstant(c)
Definition: magick-type.h:39
Definition: quantum.h:33
MagickPrivate void ResetQuantumState(QuantumInfo *)
Definition: quantum.c:578
#define SignBitShift
#define SignificandMask
QuantumState state
Definition: quantum-private.h:80
Definition: memory.c:131
EndianType
Definition: quantum.h:30
size_t quantum
Definition: quantum-private.h:46
EndianType endian
Definition: quantum-private.h:77
static const unsigned char * PushShortPixel(const EndianType endian, const unsigned char *pixels, unsigned short *pixel)
Definition: quantum-private.h:253
MagickBooleanType pack
Definition: quantum-private.h:61
static const unsigned char * PushCharPixel(const unsigned char *pixels, unsigned char *pixel)
Definition: quantum-private.h:223
MagickBooleanType
Definition: magick-type.h:189
static Quantum ScaleAnyToQuantum(const QuantumAny quantum, const QuantumAny range)
Definition: quantum-private.h:272
static unsigned char * PopLongPixel(const EndianType endian, const unsigned int pixel, unsigned char *pixels)
Definition: quantum-private.h:183
unsigned int pixel
Definition: quantum-private.h:34
size_t MagickSizeType
Definition: magick-type.h:134
static const unsigned char * PushLongPixel(const EndianType endian, const unsigned char *pixels, unsigned int *pixel)
Definition: quantum-private.h:230
SemaphoreInfo * semaphore
Definition: quantum-private.h:83
#define SignificandShift
#define ExponentShift
#define MaxMap
Definition: magick-type.h:78
Definition: quantum-private.h:43
size_t pad
Definition: quantum-private.h:58
static float HalfToSinglePrecision(const unsigned short half)
Definition: quantum-private.h:101
size_t number_threads
Definition: quantum-private.h:68
double scale
Definition: quantum-private.h:53
const unsigned int * mask
Definition: quantum-private.h:40
unsigned short Quantum
Definition: magick-type.h:85
#define ExponentBias
size_t bits
Definition: quantum-private.h:37
size_t extent
Definition: quantum-private.h:74
static unsigned char * PopCharPixel(const unsigned char pixel, unsigned char *pixels)
Definition: quantum-private.h:176
static unsigned char * PopShortPixel(const EndianType endian, const unsigned short pixel, unsigned char *pixels)
Definition: quantum-private.h:205
double inverse_scale
Definition: quantum-private.h:31
static unsigned short SinglePrecisionToHalf(const float value)
Definition: quantum-private.h:567
#define MagickPrivate
Definition: method-attribute.h:99
struct _QuantumState QuantumState
static QuantumAny ScaleQuantumToAny(const Quantum quantum, const QuantumAny range)
Definition: quantum-private.h:284
Definition: quantum-private.h:28
MagickSizeType QuantumAny
Definition: magick-type.h:148
QuantumAlphaType
Definition: quantum.h:37
Definition: semaphore.c:59
#define QuantumRange
Definition: magick-type.h:86