2 Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
8 http://www.imagemagick.org/script/license.php
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.
16 MagickCore pixel accessor methods.
18 #ifndef _MAGICKCORE_PIXEL_ACCESSOR_H
19 #define _MAGICKCORE_PIXEL_ACCESSOR_H
21 #if defined(__cplusplus) || defined(c_plusplus)
26 #include <MagickCore/cache-view.h>
27 #include <MagickCore/color.h>
28 #include <MagickCore/image.h>
32 static inline Quantum GetPixelAlpha(const Image *restrict image,
33 const Quantum *restrict pixel)
35 if (image->channel_map[AlphaPixelChannel].traits == UndefinedPixelTrait)
37 return(pixel[image->channel_map[AlphaPixelChannel].channel]);
40 static inline PixelTrait GetPixelAlphaTraits(const Image *restrict image)
42 return(image->channel_map[AlphaPixelChannel].traits);
45 static inline Quantum GetPixelBlack(const Image *restrict image,
46 const Quantum *restrict pixel)
48 if (image->channel_map[BlackPixelChannel].traits == UndefinedPixelTrait)
50 return(pixel[image->channel_map[BlackPixelChannel].channel]);
53 static inline PixelTrait GetPixelBlackTraits(const Image *restrict image)
55 return(image->channel_map[BlackPixelChannel].traits);
58 static inline Quantum GetPixelBlue(const Image *restrict image,
59 const Quantum *restrict pixel)
61 return(pixel[image->channel_map[BluePixelChannel].channel]);
64 static inline PixelTrait GetPixelBlueTraits(const Image *restrict image)
66 return(image->channel_map[BluePixelChannel].traits);
69 static inline Quantum GetPixelCb(const Image *restrict image,
70 const Quantum *restrict pixel)
72 return(pixel[image->channel_map[CbPixelChannel].channel]);
75 static inline PixelTrait GetPixelCbTraits(const Image *restrict image)
77 return(image->channel_map[CbPixelChannel].traits);
80 static inline Quantum GetPixelChannel(const Image *restrict image,
81 const PixelChannel channel,const Quantum *restrict pixel)
83 if (image->channel_map[channel].traits == UndefinedPixelTrait)
85 return(pixel[image->channel_map[channel].channel]);
88 static inline PixelChannel GetPixelChannelMapChannel(
89 const Image *restrict image,const PixelChannel channel)
91 return(image->channel_map[channel].channel);
94 static inline PixelTrait GetPixelChannelMapTraits(const Image *restrict image,
95 const PixelChannel channel)
97 return(image->channel_map[channel].traits);
100 static inline size_t GetPixelChannels(const Image *restrict image)
102 return(image->number_channels);
105 static inline Quantum GetPixelCr(const Image *restrict image,
106 const Quantum *restrict pixel)
108 return(pixel[image->channel_map[CrPixelChannel].channel]);
111 static inline PixelTrait GetPixelCrTraits(const Image *restrict image)
113 return(image->channel_map[CrPixelChannel].traits);
116 static inline Quantum GetPixelCyan(const Image *restrict image,
117 const Quantum *restrict pixel)
119 return(pixel[image->channel_map[CyanPixelChannel].channel]);
122 static inline PixelTrait GetPixelCyanTraits(const Image *restrict image)
124 return(image->channel_map[CyanPixelChannel].traits);
127 static inline Quantum GetPixelGray(const Image *restrict image,
128 const Quantum *restrict pixel)
130 return(pixel[image->channel_map[GrayPixelChannel].channel]);
133 static inline PixelTrait GetPixelGrayTraits(const Image *restrict image)
135 return(image->channel_map[GrayPixelChannel].traits);
138 static inline Quantum GetPixelGreen(const Image *restrict image,
139 const Quantum *restrict pixel)
141 return(pixel[image->channel_map[GreenPixelChannel].channel]);
144 static inline PixelTrait GetPixelGreenTraits(const Image *restrict image)
146 return(image->channel_map[GreenPixelChannel].traits);
149 static inline Quantum GetPixelIndex(const Image *restrict image,
150 const Quantum *restrict pixel)
152 if (image->channel_map[IndexPixelChannel].traits == UndefinedPixelTrait)
154 return(pixel[image->channel_map[IndexPixelChannel].channel]);
157 static inline PixelTrait GetPixelIndexTraits(const Image *restrict image)
159 return(image->channel_map[IndexPixelChannel].traits);
162 static inline Quantum GetPixelInfoIntensity(
163 const PixelInfo *restrict pixel_info)
165 #if !defined(MAGICKCORE_HDRI_SUPPORT)
166 return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
167 pixel_info->blue+0.5));
169 return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
174 static inline Quantum GetPixelInfoLuminance(
175 const PixelInfo *restrict pixel_info)
180 #if !defined(MAGICKCORE_HDRI_SUPPORT)
181 luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
182 0.07217*pixel_info->blue+0.5);
184 luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
185 0.07217*pixel_info->blue);
187 return((Quantum) luminance);
190 static inline Quantum GetPixelMagenta(const Image *restrict image,
191 const Quantum *restrict pixel)
193 return(pixel[image->channel_map[MagentaPixelChannel].channel]);
196 static inline PixelTrait GetPixelMagentaTraits(const Image *restrict image)
198 return(image->channel_map[MagentaPixelChannel].traits);
201 static inline size_t GetPixelMetaChannels(const Image *restrict image)
203 return(image->number_meta_channels);
206 static inline size_t GetPixelMetacontentExtent(const Image *restrict image)
208 return(image->metacontent_extent);
211 static inline Quantum GetPixelRed(const Image *restrict image,
212 const Quantum *restrict pixel)
214 return(pixel[image->channel_map[RedPixelChannel].channel]);
217 static inline PixelTrait GetPixelRedTraits(const Image *restrict image)
219 return(image->channel_map[RedPixelChannel].traits);
222 static inline void GetPixelInfoPixel(const Image *restrict image,
223 const Quantum *restrict pixel,PixelInfo *restrict pixel_info)
225 pixel_info->red=(MagickRealType)
226 pixel[image->channel_map[RedPixelChannel].channel];
227 pixel_info->green=(MagickRealType)
228 pixel[image->channel_map[GreenPixelChannel].channel];
229 pixel_info->blue=(MagickRealType)
230 pixel[image->channel_map[BluePixelChannel].channel];
232 if (image->channel_map[BlackPixelChannel].traits != UndefinedPixelTrait)
233 pixel_info->black=(MagickRealType)
234 pixel[image->channel_map[BlackPixelChannel].channel];
235 pixel_info->alpha=OpaqueAlpha;
236 if (image->channel_map[AlphaPixelChannel].traits != UndefinedPixelTrait)
237 pixel_info->alpha=(MagickRealType)
238 pixel[image->channel_map[AlphaPixelChannel].channel];
240 if (image->channel_map[IndexPixelChannel].traits != UndefinedPixelTrait)
241 pixel_info->index=(MagickRealType)
242 pixel[image->channel_map[IndexPixelChannel].channel];
245 static inline PixelTrait GetPixelTraits(const Image *restrict image,
246 const PixelChannel channel)
248 return(image->channel_map[channel].traits);
251 static inline Quantum GetPixelY(const Image *restrict image,
252 const Quantum *restrict pixel)
254 return(pixel[image->channel_map[YPixelChannel].channel]);
257 static inline PixelTrait GetPixelYTraits(const Image *restrict image)
259 return(image->channel_map[YPixelChannel].traits);
262 static inline Quantum GetPixelYellow(const Image *restrict image,
263 const Quantum *restrict pixel)
265 return(pixel[image->channel_map[YellowPixelChannel].channel]);
268 static inline PixelTrait GetPixelYellowTraits(const Image *restrict image)
270 return(image->channel_map[YellowPixelChannel].traits);
273 static inline MagickBooleanType IsPixelEquivalent(const Image *restrict image,
274 const Quantum *restrict p,const PixelInfo *restrict q)
276 if (((double) p[image->channel_map[RedPixelChannel].channel] == q->red) &&
277 ((double) p[image->channel_map[GreenPixelChannel].channel] == q->green) &&
278 ((double) p[image->channel_map[BluePixelChannel].channel] == q->blue))
283 static inline MagickBooleanType IsPixelGray(const Image *restrict image,
284 const Quantum *restrict pixel)
286 #if !defined(MAGICKCORE_HDRI_SUPPORT)
287 if ((pixel[image->channel_map[RedPixelChannel].channel] ==
288 pixel[image->channel_map[GreenPixelChannel].channel]) &&
289 (pixel[image->channel_map[GreenPixelChannel].channel] ==
290 pixel[image->channel_map[BluePixelChannel].channel]))
298 alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
299 pixel[image->channel_map[GreenPixelChannel].channel];
300 beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
301 pixel[image->channel_map[BluePixelChannel].channel];
302 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
309 static inline MagickBooleanType IsPixelInfoEquivalent(
310 const PixelInfo *restrict p,const PixelInfo *restrict q)
312 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
313 (fabs(p->alpha-OpaqueAlpha) > 0.5))
315 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
316 (fabs(q->alpha-OpaqueAlpha)) > 0.5)
318 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
320 if (fabs(p->alpha-q->alpha) > 0.5)
322 if (fabs(p->alpha-TransparentAlpha) <= 0.5)
325 if (fabs(p->red-q->red) > 0.5)
327 if (fabs(p->green-q->green) > 0.5)
329 if (fabs(p->blue-q->blue) > 0.5)
331 if ((p->colorspace == CMYKColorspace) && (fabs(p->black-q->black) > 0.5))
336 static inline MagickBooleanType IsPixelMonochrome(const Image *restrict image,
337 const Quantum *restrict pixel)
339 #if !defined(MAGICKCORE_HDRI_SUPPORT)
340 if (((pixel[image->channel_map[RedPixelChannel].channel] == 0) ||
341 (pixel[image->channel_map[RedPixelChannel].channel] == (Quantum) QuantumRange)) &&
342 (pixel[image->channel_map[RedPixelChannel].channel] ==
343 pixel[image->channel_map[GreenPixelChannel].channel]) &&
344 (pixel[image->channel_map[GreenPixelChannel].channel] ==
345 pixel[image->channel_map[BluePixelChannel].channel]))
353 alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
354 pixel[image->channel_map[GreenPixelChannel].channel];
355 beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
356 pixel[image->channel_map[BluePixelChannel].channel];
357 if (((fabs(pixel[image->channel_map[RedPixelChannel].channel]) <=
359 (fabs(pixel[image->channel_map[RedPixelChannel].channel]-
360 QuantumRange) <= MagickEpsilon)) &&
361 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
368 static inline MagickBooleanType IsPixelInfoGray(
369 const PixelInfo *restrict pixel_info)
371 #if !defined(MAGICKCORE_HDRI_SUPPORT)
372 if ((pixel_info->red == pixel_info->green) &&
373 (pixel_info->green == pixel_info->blue))
381 alpha=pixel_info->red-(double) pixel_info->green;
382 beta=pixel_info->green-(double) pixel_info->blue;
383 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
390 static inline MagickBooleanType IsPixelInfoMonochrome(
391 const PixelInfo *restrict pixel_info)
393 #if !defined(MAGICKCORE_HDRI_SUPPORT)
394 if (((pixel_info->red == 0) || (pixel_info->red == (Quantum) QuantumRange)) &&
395 (pixel_info->red == pixel_info->green) &&
396 (pixel_info->green == pixel_info->blue))
404 alpha=pixel_info->red-(double) pixel_info->green;
405 beta=pixel_info->green-(double) pixel_info->blue;
406 if (((fabs(pixel_info->red) <= MagickEpsilon) ||
407 (fabs(pixel_info->red-QuantumRange) <= MagickEpsilon)) &&
408 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
415 static inline void SetPixelAlpha(const Image *restrict image,
416 const Quantum alpha,Quantum *restrict pixel)
418 if (image->channel_map[AlphaPixelChannel].traits != UndefinedPixelTrait)
419 pixel[image->channel_map[AlphaPixelChannel].channel]=alpha;
422 static inline void SetPixelAlphaTraits(Image *image,const PixelTrait traits)
424 image->channel_map[AlphaPixelChannel].traits=traits;
427 static inline void SetPixelBlack(const Image *restrict image,
428 const Quantum black,Quantum *restrict pixel)
430 if (image->channel_map[BlackPixelChannel].traits != UndefinedPixelTrait)
431 pixel[image->channel_map[BlackPixelChannel].channel]=black;
434 static inline void SetPixelBlackTraits(Image *image,const PixelTrait traits)
436 image->channel_map[BlackPixelChannel].traits=traits;
439 static inline void SetPixelBlue(const Image *restrict image,const Quantum blue,
440 Quantum *restrict pixel)
442 pixel[image->channel_map[BluePixelChannel].channel]=blue;
445 static inline void SetPixelBlueTraits(Image *image,const PixelTrait traits)
447 image->channel_map[BluePixelChannel].traits=traits;
450 static inline void SetPixelCb(const Image *restrict image,const Quantum cb,
451 Quantum *restrict pixel)
453 pixel[image->channel_map[CbPixelChannel].channel]=cb;
456 static inline void SetPixelCbTraits(Image *image,const PixelTrait traits)
458 image->channel_map[CbPixelChannel].traits=traits;
461 static inline void SetPixelChannel(const Image *restrict image,
462 const PixelChannel channel,const Quantum quantum,Quantum *restrict pixel)
464 if (image->channel_map[channel].traits != UndefinedPixelTrait)
465 pixel[image->channel_map[channel].channel]=quantum;
468 static inline void SetPixelChannelMapChannel(const Image *restrict image,
469 const PixelChannel channel,const PixelChannel channels)
471 image->channel_map[channel].channel=channels;
474 static inline void SetPixelChannels(Image *image,const size_t number_channels)
476 image->number_channels=number_channels;
479 static inline void SetPixelChannelTraits(Image *image,
480 const PixelChannel channel,const PixelTrait traits)
482 image->channel_map[channel].traits=traits;
485 static inline void SetPixelChannelMapTraits(Image *image,
486 const PixelChannel channel,const PixelTrait traits)
488 image->channel_map[channel].traits=traits;
491 static inline void SetPixelCr(const Image *restrict image,const Quantum cr,
492 Quantum *restrict pixel)
494 pixel[image->channel_map[CrPixelChannel].channel]=cr;
497 static inline void SetPixelCrTraits(Image *image,const PixelTrait traits)
499 image->channel_map[CrPixelChannel].traits=traits;
502 static inline void SetPixelCyan(const Image *restrict image,const Quantum cyan,
503 Quantum *restrict pixel)
505 pixel[image->channel_map[CyanPixelChannel].channel]=cyan;
508 static inline void SetPixelGray(const Image *restrict image,const Quantum gray,
509 Quantum *restrict pixel)
511 pixel[image->channel_map[GrayPixelChannel].channel]=gray;
514 static inline void SetPixelGrayTraits(Image *image,const PixelTrait traits)
516 image->channel_map[GrayPixelChannel].traits=traits;
519 static inline void SetPixelGreen(const Image *restrict image,
520 const Quantum green,Quantum *restrict pixel)
522 pixel[image->channel_map[GreenPixelChannel].channel]=green;
525 static inline void SetPixelGreenTraits(Image *image,const PixelTrait traits)
527 image->channel_map[GreenPixelChannel].traits=traits;
530 static inline void SetPixelIndex(const Image *restrict image,
531 const Quantum index,Quantum *restrict pixel)
533 if (image->channel_map[IndexPixelChannel].traits != UndefinedPixelTrait)
534 pixel[image->channel_map[IndexPixelChannel].channel]=index;
537 static inline void SetPixelIndexTraits(Image *image,const PixelTrait traits)
539 image->channel_map[IndexPixelChannel].traits=traits;
542 static inline void SetPixelInfo(const Image *restrict image,
543 const PixelInfo *restrict p,PixelInfo *restrict q)
549 if (image->channel_map[BlackPixelChannel].traits != UndefinedPixelTrait)
551 q->alpha=OpaqueAlpha;
552 if (image->channel_map[AlphaPixelChannel].traits != UndefinedPixelTrait)
555 if (image->channel_map[IndexPixelChannel].traits != UndefinedPixelTrait)
559 static inline void SetPixelInfoPixel(const Image *restrict image,
560 const PixelInfo *restrict pixel_info,Quantum *restrict pixel)
562 pixel[image->channel_map[RedPixelChannel].channel]=
563 ClampToQuantum(pixel_info->red);
564 pixel[image->channel_map[GreenPixelChannel].channel]=
565 ClampToQuantum(pixel_info->green);
566 pixel[image->channel_map[BluePixelChannel].channel]=
567 ClampToQuantum(pixel_info->blue);
568 if ((image->channel_map[BlackPixelChannel].traits != UndefinedPixelTrait) &&
569 (image->colorspace == CMYKColorspace) &&
570 (pixel_info->colorspace == CMYKColorspace))
571 pixel[image->channel_map[BlackPixelChannel].channel]=
572 ClampToQuantum(pixel_info->black);
573 if (image->channel_map[AlphaPixelChannel].traits != UndefinedPixelTrait)
574 pixel[image->channel_map[AlphaPixelChannel].channel]=
575 pixel_info->matte == MagickFalse ? OpaqueAlpha :
576 ClampToQuantum(pixel_info->alpha);
579 static inline void SetPixelMagenta(const Image *restrict image,
580 const Quantum magenta,Quantum *restrict pixel)
582 pixel[image->channel_map[MagentaPixelChannel].channel]=magenta;
585 static inline void SetPixelMagentaTraits(Image *image,const PixelTrait traits)
587 image->channel_map[MagentaPixelChannel].traits=traits;
590 static inline void SetPixelMetaChannels(Image *image,
591 const size_t number_meta_channels)
593 image->number_meta_channels=number_meta_channels;
596 static inline void SetPixelMetacontentExtent(Image *image,const size_t extent)
598 image->metacontent_extent=extent;
601 static inline void SetPixelRed(const Image *restrict image,const Quantum red,
602 Quantum *restrict pixel)
604 pixel[image->channel_map[RedPixelChannel].channel]=red;
607 static inline void SetPixelRedTraits(Image *image,const PixelTrait traits)
609 image->channel_map[RedPixelChannel].traits=traits;
612 static inline void SetPixelYellow(const Image *restrict image,
613 const Quantum yellow,Quantum *restrict pixel)
615 pixel[image->channel_map[YellowPixelChannel].channel]=yellow;
618 static inline void SetPixelYellowTraits(Image *image,const PixelTrait traits)
620 image->channel_map[YellowPixelChannel].traits=traits;
623 static inline void SetPixelY(const Image *restrict image,const Quantum y,
624 Quantum *restrict pixel)
626 pixel[image->channel_map[YPixelChannel].channel]=y;
629 static inline void SetPixelYTraits(Image *image,const PixelTrait traits)
631 image->channel_map[YPixelChannel].traits=traits;
634 static inline Quantum GetPixelIntensity(const Image *restrict image,
635 const Quantum *restrict pixel)
637 #if !defined(MAGICKCORE_HDRI_SUPPORT)
638 if ((pixel[image->channel_map[RedPixelChannel].channel] ==
639 pixel[image->channel_map[GreenPixelChannel].channel]) &&
640 (pixel[image->channel_map[GreenPixelChannel].channel] ==
641 pixel[image->channel_map[BluePixelChannel].channel]))
642 return(pixel[image->channel_map[RedPixelChannel].channel]);
643 return((Quantum) (0.299*pixel[image->channel_map[RedPixelChannel].channel]+
644 0.587*pixel[image->channel_map[GreenPixelChannel].channel]+0.114*
645 pixel[image->channel_map[BluePixelChannel].channel]+0.5));
652 alpha=pixel[image->channel_map[RedPixelChannel].channel]-(double)
653 pixel[image->channel_map[GreenPixelChannel].channel];
654 beta=pixel[image->channel_map[GreenPixelChannel].channel]-(double)
655 pixel[image->channel_map[BluePixelChannel].channel];
656 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
657 return(pixel[image->channel_map[RedPixelChannel].channel]);
658 return((Quantum) (0.299*pixel[image->channel_map[RedPixelChannel].channel]+
659 0.587*pixel[image->channel_map[GreenPixelChannel].channel]+0.114*
660 pixel[image->channel_map[BluePixelChannel].channel]));
665 static inline Quantum GetPixelLuminance(const Image *restrict image,
666 const Quantum *restrict pixel)
668 #if !defined(MAGICKCORE_HDRI_SUPPORT)
669 return((Quantum) (0.21267*pixel[image->channel_map[RedPixelChannel].channel]+
670 0.71516*pixel[image->channel_map[GreenPixelChannel].channel]+0.07217*
671 pixel[image->channel_map[BluePixelChannel].channel]+0.5));
673 return((Quantum) (0.21267*pixel[image->channel_map[RedPixelChannel].channel]+
674 0.71516*pixel[image->channel_map[GreenPixelChannel].channel]+0.07217*
675 pixel[image->channel_map[BluePixelChannel].channel]));
679 #if defined(__cplusplus) || defined(c_plusplus)