]> granicus.if.org Git - imagemagick/blob - MagickCore/pixel-accessor.h
(no commit message)
[imagemagick] / MagickCore / pixel-accessor.h
1 /*
2   Copyright 1999-2011 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     http://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 pixel accessor methods.
17 */
18 #ifndef _MAGICKCORE_PIXEL_ACCESSOR_H
19 #define _MAGICKCORE_PIXEL_ACCESSOR_H
20
21 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24
25 #include <math.h>
26 #include <MagickCore/cache-view.h>
27 #include <MagickCore/color.h>
28 #include <MagickCore/image.h>
29
30 #undef index
31
32 static inline Quantum GetPixelAlpha(const Image *image,const Quantum *pixel)
33 {
34   return(pixel[image->channel_map[AlphaPixelChannel].channel]);
35 }
36
37 static inline PixelTrait GetPixelAlphaTraits(const Image *image)
38 {
39   return(image->channel_map[AlphaPixelChannel].traits);
40 }
41
42 static inline Quantum GetPixelBlack(const Image *image,const Quantum *pixel)
43 {
44   return(pixel[image->channel_map[BlackPixelChannel].channel]);
45 }
46
47 static inline PixelTrait GetPixelBlackTraits(const Image *image)
48 {
49   return(image->channel_map[BlackPixelChannel].traits);
50 }
51
52 static inline Quantum GetPixelBlue(const Image *image,const Quantum *pixel)
53 {
54   return(pixel[image->channel_map[BluePixelChannel].channel]);
55 }
56
57 static inline PixelTrait GetPixelBlueTraits(const Image *image)
58 {
59   return(image->channel_map[BluePixelChannel].traits);
60 }
61
62 static inline Quantum GetPixelCb(const Image *image,const Quantum *pixel)
63 {
64   return(pixel[image->channel_map[CbPixelChannel].channel]);
65 }
66
67 static inline PixelTrait GetPixelCbTraits(const Image *image)
68 {
69   return(image->channel_map[CbPixelChannel].traits);
70 }
71
72 static inline Quantum GetPixelChannel(const Image *image,
73   const PixelChannel channel,const Quantum *pixel)
74 {
75   return(pixel[image->channel_map[channel].channel]);
76 }
77
78 static inline PixelChannel GetPixelChannelMapChannel(const Image *image,
79   const PixelChannel channel)
80 {
81   return(image->channel_map[channel].channel);
82 }
83
84 static inline PixelTrait GetPixelChannelMapTraits(const Image *image,
85   const PixelChannel channel)
86 {
87   return(image->channel_map[channel].traits);
88 }
89
90 static inline size_t GetPixelChannels(const Image *image)
91 {
92   return(image->number_channels);
93 }
94
95 static inline Quantum GetPixelCr(const Image *image,const Quantum *pixel)
96 {
97   return(pixel[image->channel_map[CrPixelChannel].channel]);
98 }
99
100 static inline PixelTrait GetPixelCrTraits(const Image *image)
101 {
102   return(image->channel_map[CrPixelChannel].traits);
103 }
104
105 static inline Quantum GetPixelCyan(const Image *image,const Quantum *pixel)
106 {
107   return(pixel[image->channel_map[CyanPixelChannel].channel]);
108 }
109
110 static inline PixelTrait GetPixelCyanTraits(const Image *image)
111 {
112   return(image->channel_map[CyanPixelChannel].traits);
113 }
114
115 static inline Quantum GetPixelGray(const Image *image,const Quantum *pixel)
116 {
117   return(pixel[image->channel_map[GrayPixelChannel].channel]);
118 }
119
120 static inline PixelTrait GetPixelGrayTraits(const Image *image)
121 {
122   return(image->channel_map[GrayPixelChannel].traits);
123 }
124
125 static inline Quantum GetPixelGreen(const Image *image,const Quantum *pixel)
126 {
127   return(pixel[image->channel_map[GreenPixelChannel].channel]);
128 }
129
130 static inline PixelTrait GetPixelGreenTraits(const Image *image)
131 {
132   return(image->channel_map[GreenPixelChannel].traits);
133 }
134
135 static inline Quantum GetPixelIndex(const Image *image,const Quantum *pixel)
136 {
137   return(pixel[image->channel_map[IndexPixelChannel].channel]);
138 }
139
140 static inline PixelTrait GetPixelIndexTraits(const Image *image)
141 {
142   return(image->channel_map[IndexPixelChannel].traits);
143 }
144
145 static inline Quantum GetPixelInfoIntensity(const PixelInfo *pixel_info)
146 {
147 #if !defined(MAGICKCORE_HDRI_SUPPORT)
148   return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
149     pixel_info->blue+0.5));
150 #else
151   return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
152     pixel_info->blue));
153 #endif
154 }
155
156 static inline Quantum GetPixelInfoLuminance(const PixelInfo *pixel_info)
157 {
158   Quantum
159     luminance;
160
161 #if !defined(MAGICKCORE_HDRI_SUPPORT)
162   luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
163     0.07217*pixel_info->blue+0.5);
164 #else
165   luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
166     0.07217*pixel_info->blue);
167 #endif
168   return((Quantum) luminance);
169 }
170
171 static inline Quantum GetPixelMagenta(const Image *image,const Quantum *pixel)
172 {
173   return(pixel[image->channel_map[MagentaPixelChannel].channel]);
174 }
175
176 static inline PixelTrait GetPixelMagentaTraits(const Image *image)
177 {
178   return(image->channel_map[MagentaPixelChannel].traits);
179 }
180
181 static inline size_t GetPixelMetaChannels(const Image *image)
182 {
183   return(image->number_meta_channels);
184 }
185
186 static inline size_t GetPixelMetacontentExtent(const Image *image)
187 {
188   return(image->metacontent_extent);
189 }
190
191 static inline Quantum GetPixelRed(const Image *image,const Quantum *pixel)
192 {
193   return(pixel[image->channel_map[RedPixelChannel].channel]);
194 }
195
196 static inline PixelTrait GetPixelRedTraits(const Image *image)
197 {
198   return(image->channel_map[RedPixelChannel].traits);
199 }
200
201 static inline void GetPixelInfoPixel(const Image *image,const Quantum *pixel,
202   PixelInfo *packet)
203 {
204   packet->red=(double) pixel[image->channel_map[RedPixelChannel].channel];
205   packet->green=(double) pixel[image->channel_map[GreenPixelChannel].channel];
206   packet->blue=(double) pixel[image->channel_map[BluePixelChannel].channel];
207   packet->alpha=(double) pixel[image->channel_map[AlphaPixelChannel].channel];
208 }
209
210 static inline PixelTrait GetPixelTraits(const Image *image,
211   const PixelChannel channel)
212 {
213   return(image->channel_map[channel].traits);
214 }
215
216 static inline Quantum GetPixelY(const Image *image,const Quantum *pixel)
217 {
218   return(pixel[image->channel_map[YPixelChannel].channel]);
219 }
220
221 static inline PixelTrait GetPixelYTraits(const Image *image)
222 {
223   return(image->channel_map[YPixelChannel].traits);
224 }
225
226 static inline Quantum GetPixelYellow(const Image *image,const Quantum *pixel)
227 {
228   return(pixel[image->channel_map[YellowPixelChannel].channel]);
229 }
230
231 static inline PixelTrait GetPixelYellowTraits(const Image *image)
232 {
233   return(image->channel_map[YellowPixelChannel].traits);
234 }
235
236 static inline MagickBooleanType IsPixelEquivalent(const Image *image,
237   const Quantum *p,const PixelInfo *q)
238 {
239   if (((double) p[image->channel_map[RedPixelChannel].channel] == q->red) &&
240       ((double) p[image->channel_map[GreenPixelChannel].channel] == q->green) &&
241       ((double) p[image->channel_map[BluePixelChannel].channel] == q->blue))
242     return(MagickTrue);
243   return(MagickFalse);
244 }
245
246 static inline MagickBooleanType IsPixelGray(const Image *image,
247   const Quantum *pixel)
248 {
249 #if !defined(MAGICKCORE_HDRI_SUPPORT)
250   if ((pixel[image->channel_map[RedPixelChannel].channel] ==
251        pixel[image->channel_map[GreenPixelChannel].channel]) &&
252       (pixel[image->channel_map[GreenPixelChannel].channel] ==
253        pixel[image->channel_map[BluePixelChannel].channel]))
254     return(MagickTrue);
255 #else
256   {
257     double
258       alpha,
259       beta;
260
261     alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
262       pixel[image->channel_map[GreenPixelChannel].channel];
263     beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
264       pixel[image->channel_map[BluePixelChannel].channel];
265     if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
266       return(MagickTrue);
267   }
268 #endif
269   return(MagickFalse);
270 }
271
272 static inline MagickBooleanType IsPixelInfoEquivalent(const PixelInfo *p,
273   const PixelInfo *q)
274 {
275   if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
276       (fabs(p->alpha-OpaqueAlpha) > 0.5))
277     return(MagickFalse);
278   if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
279       (fabs(q->alpha-OpaqueAlpha)) > 0.5)
280     return(MagickFalse);
281   if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
282     {
283       if (fabs(p->alpha-q->alpha) > 0.5)
284         return(MagickFalse);
285       if (fabs(p->alpha-TransparentAlpha) <= 0.5)
286         return(MagickTrue);
287     }
288   if (fabs(p->red-q->red) > 0.5)
289     return(MagickFalse);
290   if (fabs(p->green-q->green) > 0.5)
291     return(MagickFalse);
292   if (fabs(p->blue-q->blue) > 0.5)
293     return(MagickFalse);
294   if ((p->colorspace == CMYKColorspace) && (fabs(p->black-q->black) > 0.5))
295     return(MagickFalse);
296   return(MagickTrue);
297 }
298
299 static inline MagickBooleanType IsPixelMonochrome(const Image *image,
300   const Quantum *pixel)
301 {
302 #if !defined(MAGICKCORE_HDRI_SUPPORT)
303   if (((pixel[image->channel_map[RedPixelChannel].channel] == 0) ||
304       (pixel[image->channel_map[RedPixelChannel].channel] == (Quantum) QuantumRange)) &&
305       (pixel[image->channel_map[RedPixelChannel].channel] ==
306        pixel[image->channel_map[GreenPixelChannel].channel]) &&
307       (pixel[image->channel_map[GreenPixelChannel].channel] ==
308        pixel[image->channel_map[BluePixelChannel].channel]))
309     return(MagickTrue);
310 #else
311   {
312     double
313       alpha,
314       beta;
315
316     alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
317       pixel[image->channel_map[GreenPixelChannel].channel];
318     beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
319       pixel[image->channel_map[BluePixelChannel].channel];
320     if (((fabs(pixel[image->channel_map[RedPixelChannel].channel]) <= MagickEpsilon) ||
321          (fabs(pixel[image->channel_map[RedPixelChannel].channel]-QuantumRange) <= MagickEpsilon)) &&
322         (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
323       return(MagickTrue);
324     }
325 #endif
326   return(MagickFalse);
327 }
328
329 static inline MagickBooleanType IsPixelInfoGray(const PixelInfo *pixel)
330 {
331 #if !defined(MAGICKCORE_HDRI_SUPPORT)
332   if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
333     return(MagickTrue);
334 #else
335   {
336     double
337       alpha,
338       beta;
339
340     alpha=pixel->red-(double) pixel->green;
341     beta=pixel->green-(double) pixel->blue;
342     if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
343       return(MagickTrue);
344   }
345 #endif
346   return(MagickFalse);
347 }
348
349 static inline MagickBooleanType IsPixelInfoMonochrome(
350   const PixelInfo *pixel)
351 {
352 #if !defined(MAGICKCORE_HDRI_SUPPORT)
353   if (((pixel->red == 0) || (pixel->red == (Quantum) QuantumRange)) &&
354       (pixel->red == pixel->green) && (pixel->green == pixel->blue))
355     return(MagickTrue);
356 #else
357   {
358     double
359       alpha,
360       beta;
361
362     alpha=pixel->red-(double) pixel->green;
363     beta=pixel->green-(double) pixel->blue;
364     if (((fabs(pixel->red) <= MagickEpsilon) ||
365          (fabs(pixel->red-QuantumRange) <= MagickEpsilon)) &&
366         (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
367       return(MagickTrue);
368     }
369 #endif
370   return(MagickFalse);
371 }
372
373 static inline void SetPacketPixelInfo(const Image *image,
374   const PixelInfo *pixel_info,PixelInfo *packet)
375 {
376   packet->red=pixel_info->red;
377   packet->green=pixel_info->green;
378   packet->blue=pixel_info->blue;
379   packet->alpha=pixel_info->alpha;
380   if (image->colorspace == CMYKColorspace)
381     packet->black=pixel_info->black;
382   if (image->storage_class == PseudoClass)
383     packet->index=pixel_info->index;
384 }
385
386 static inline void SetPixelAlpha(const Image *image,const Quantum alpha,
387   Quantum *pixel)
388 {
389   pixel[image->channel_map[AlphaPixelChannel].channel]=alpha;
390 }
391
392 static inline void SetPixelAlphaTraits(Image *image,const PixelTrait traits)
393 {
394   image->channel_map[AlphaPixelChannel].traits=traits;
395 }
396
397 static inline void SetPixelBlack(const Image *image,const Quantum black,
398   Quantum *pixel)
399 {
400   pixel[image->channel_map[BlackPixelChannel].channel]=black;
401 }
402
403 static inline void SetPixelBlackTraits(Image *image,const PixelTrait traits)
404 {
405   image->channel_map[BlackPixelChannel].traits=traits;
406 }
407
408 static inline void SetPixelBlue(const Image *image,const Quantum blue,
409   Quantum *pixel)
410 {
411   pixel[image->channel_map[BluePixelChannel].channel]=blue;
412 }
413
414 static inline void SetPixelBlueTraits(Image *image,const PixelTrait traits)
415 {
416   image->channel_map[BluePixelChannel].traits=traits;
417 }
418
419 static inline void SetPixelCb(const Image *image,const Quantum cb,
420   Quantum *pixel)
421 {
422   pixel[image->channel_map[CbPixelChannel].channel]=cb;
423 }
424
425 static inline void SetPixelCbTraits(Image *image,const PixelTrait traits)
426 {
427   image->channel_map[CbPixelChannel].traits=traits;
428 }
429
430 static inline void SetPixelChannel(const Image *image,
431   const PixelChannel channel,const Quantum quantum,Quantum *pixel)
432 {
433   pixel[image->channel_map[channel].channel]=quantum;
434 }
435
436 static inline void SetPixelChannelMapChannel(const Image *image,
437   const PixelChannel channel,const PixelChannel channels)
438 {
439   image->channel_map[channel].channel=channels;
440 }
441
442 static inline void SetPixelChannels(Image *image,const size_t number_channels)
443 {
444   image->number_channels=number_channels;
445 }
446
447 static inline void SetPixelChannelTraits(Image *image,
448   const PixelChannel channel,const PixelTrait traits)
449 {
450   image->channel_map[channel].traits=traits;
451 }
452
453 static inline void SetPixelChannelMapTraits(Image *image,
454   const PixelChannel channel,const PixelTrait traits)
455 {
456   image->channel_map[channel].traits=traits;
457 }
458
459 static inline void SetPixelCr(const Image *image,const Quantum cr,
460   Quantum *pixel)
461 {
462   pixel[image->channel_map[CrPixelChannel].channel]=cr;
463 }
464
465 static inline void SetPixelCrTraits(Image *image,const PixelTrait traits)
466 {
467   image->channel_map[CrPixelChannel].traits=traits;
468 }
469
470 static inline void SetPixelCyan(const Image *image,const Quantum cyan,
471   Quantum *pixel)
472 {
473   pixel[image->channel_map[CyanPixelChannel].channel]=cyan;
474 }
475
476 static inline void SetPixelGray(const Image *image,const Quantum gray,
477   Quantum *pixel)
478 {
479   pixel[image->channel_map[GrayPixelChannel].channel]=gray;
480 }
481
482 static inline void SetPixelGrayTraits(Image *image,const PixelTrait traits)
483 {
484   image->channel_map[GrayPixelChannel].traits=traits;
485 }
486
487 static inline void SetPixelGreen(const Image *image,const Quantum green,
488   Quantum *pixel)
489 {
490   pixel[image->channel_map[GreenPixelChannel].channel]=green;
491 }
492
493 static inline void SetPixelGreenTraits(Image *image,const PixelTrait traits)
494 {
495   image->channel_map[GreenPixelChannel].traits=traits;
496 }
497
498 static inline void SetPixelIndex(const Image *image,const Quantum index,
499   Quantum *pixel)
500 {
501   pixel[image->channel_map[IndexPixelChannel].channel]=index;
502 }
503
504 static inline void SetPixelIndexTraits(Image *image,const PixelTrait traits)
505 {
506   image->channel_map[IndexPixelChannel].traits=traits;
507 }
508
509 static inline void SetPixelInfo(const Image *image,const Quantum *pixel,
510   PixelInfo *pixel_info)
511 {
512   pixel_info->red=(MagickRealType)
513     pixel[image->channel_map[RedPixelChannel].channel];
514   pixel_info->green=(MagickRealType)
515     pixel[image->channel_map[GreenPixelChannel].channel];
516   pixel_info->blue=(MagickRealType)
517     pixel[image->channel_map[BluePixelChannel].channel];
518   if (image->colorspace == CMYKColorspace)
519     pixel_info->black=(MagickRealType)
520       pixel[image->channel_map[BlackPixelChannel].channel];
521   pixel_info->alpha=(MagickRealType)
522     pixel[image->channel_map[AlphaPixelChannel].channel];
523   if (image->storage_class == PseudoClass)
524     pixel_info->index=(MagickRealType)
525       pixel[image->channel_map[IndexPixelChannel].channel];
526 }
527
528 static inline void SetPixelMagenta(const Image *image,const Quantum magenta,
529   Quantum *pixel)
530 {
531   pixel[image->channel_map[MagentaPixelChannel].channel]=magenta;
532 }
533
534 static inline void SetPixelMagentaTraits(Image *image,const PixelTrait traits)
535 {
536   image->channel_map[MagentaPixelChannel].traits=traits;
537 }
538
539 static inline void SetPixelMetaChannels(Image *image,
540   const size_t number_meta_channels)
541 {
542   image->number_meta_channels=number_meta_channels;
543 }
544
545 static inline void SetPixelMetacontentExtent(Image *image,const size_t extent)
546 {
547   image->metacontent_extent=extent;
548 }
549
550 static inline void SetPixelRed(const Image *image,const Quantum red,
551   Quantum *pixel)
552 {
553   pixel[image->channel_map[RedPixelChannel].channel]=red;
554 }
555
556 static inline void SetPixelRedTraits(Image *image,const PixelTrait traits)
557 {
558   image->channel_map[RedPixelChannel].traits=traits;
559 }
560
561 static inline void SetPixelPixelInfo(const Image *image,
562   const PixelInfo *pixel_info,Quantum *pixel)
563 {
564   pixel[image->channel_map[RedPixelChannel].channel]=
565     ClampToQuantum(pixel_info->red);
566   pixel[image->channel_map[GreenPixelChannel].channel]=
567     ClampToQuantum(pixel_info->green);
568   pixel[image->channel_map[BluePixelChannel].channel]=
569     ClampToQuantum(pixel_info->blue);
570   pixel[image->channel_map[AlphaPixelChannel].channel]=
571     ClampToQuantum(pixel_info->alpha);
572   if (image->colorspace == CMYKColorspace)
573     pixel[image->channel_map[BlackPixelChannel].channel]=
574       ClampToQuantum(pixel_info->black);
575 }
576
577 static inline void SetPixelYellow(const Image *image,const Quantum yellow,
578   Quantum *pixel)
579 {
580   pixel[image->channel_map[YellowPixelChannel].channel]=yellow;
581 }
582
583 static inline void SetPixelYellowTraits(Image *image,const PixelTrait traits)
584 {
585   image->channel_map[YellowPixelChannel].traits=traits;
586 }
587
588 static inline void SetPixelY(const Image *image,const Quantum y,Quantum *pixel)
589 {
590   pixel[image->channel_map[YPixelChannel].channel]=y;
591 }
592
593 static inline void SetPixelYTraits(Image *image,const PixelTrait traits)
594 {
595   image->channel_map[YPixelChannel].traits=traits;
596 }
597
598 static inline Quantum GetPixelIntensity(const Image *image,const Quantum *pixel)
599 {
600 #if !defined(MAGICKCORE_HDRI_SUPPORT)
601   if ((pixel[image->channel_map[RedPixelChannel].channel] ==
602        pixel[image->channel_map[GreenPixelChannel].channel]) &&
603       (pixel[image->channel_map[GreenPixelChannel].channel] ==
604        pixel[image->channel_map[BluePixelChannel].channel]))
605     return(pixel[image->channel_map[RedPixelChannel].channel]);
606   return((Quantum) (0.299*pixel[image->channel_map[RedPixelChannel].channel]+
607     0.587*pixel[image->channel_map[GreenPixelChannel].channel]+0.114*
608     pixel[image->channel_map[BluePixelChannel].channel]+0.5));
609 #else
610   {
611     double
612       alpha,
613       beta;
614
615     alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
616       pixel[image->channel_map[GreenPixelChannel].channel];
617     beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
618       pixel[image->channel_map[BluePixelChannel].channel];
619     if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
620       return(pixel[image->channel_map[RedPixelChannel].channel]);
621     return((Quantum) (0.299*pixel[image->channel_map[RedPixelChannel].channel]+
622       0.587*pixel[image->channel_map[GreenPixelChannel].channel]+0.114*
623       pixel[image->channel_map[BluePixelChannel].channel]));
624   }
625 #endif
626 }
627
628 static inline Quantum GetPixelLuminance(const Image *image,const Quantum *pixel)
629 {
630 #if !defined(MAGICKCORE_HDRI_SUPPORT)
631   return((Quantum) (0.21267*pixel[image->channel_map[RedPixelChannel].channel]+
632     0.71516*pixel[image->channel_map[GreenPixelChannel].channel]+0.07217*
633     pixel[image->channel_map[BluePixelChannel].channel]+0.5));
634 #else
635   return((Quantum) (0.21267*pixel[image->channel_map[RedPixelChannel].channel]+
636     0.71516*pixel[image->channel_map[GreenPixelChannel].channel]+0.07217*
637     pixel[image->channel_map[BluePixelChannel].channel]));
638 #endif
639 }
640
641 #if defined(__cplusplus) || defined(c_plusplus)
642 }
643 #endif
644
645 #endif