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