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 *image,const Quantum *pixel)
34 return(pixel[image->channel_map[AlphaPixelChannel].channel]);
37 static inline PixelTrait GetPixelAlphaTraits(const Image *image)
39 return(image->channel_map[AlphaPixelChannel].traits);
42 static inline Quantum GetPixelBlack(const Image *image,const Quantum *pixel)
44 return(pixel[image->channel_map[BlackPixelChannel].channel]);
47 static inline PixelTrait GetPixelBlackTraits(const Image *image)
49 return(image->channel_map[BlackPixelChannel].traits);
52 static inline Quantum GetPixelBlue(const Image *image,const Quantum *pixel)
54 return(pixel[image->channel_map[BluePixelChannel].channel]);
57 static inline PixelTrait GetPixelBlueTraits(const Image *image)
59 return(image->channel_map[BluePixelChannel].traits);
62 static inline Quantum GetPixelCb(const Image *image,const Quantum *pixel)
64 return(pixel[image->channel_map[CbPixelChannel].channel]);
67 static inline PixelTrait GetPixelCbTraits(const Image *image)
69 return(image->channel_map[CbPixelChannel].traits);
72 static inline Quantum GetPixelChannel(const Image *image,
73 const PixelChannel channel,const Quantum *pixel)
75 return(pixel[image->channel_map[channel].channel]);
78 static inline PixelChannel GetPixelChannelMapChannel(const Image *image,
79 const PixelChannel channel)
81 return(image->channel_map[channel].channel);
84 static inline PixelTrait GetPixelChannelMapTraits(const Image *image,
85 const PixelChannel channel)
87 return(image->channel_map[channel].traits);
90 static inline size_t GetPixelChannels(const Image *image)
92 return(image->number_channels);
95 static inline Quantum GetPixelCr(const Image *image,const Quantum *pixel)
97 return(pixel[image->channel_map[CrPixelChannel].channel]);
100 static inline PixelTrait GetPixelCrTraits(const Image *image)
102 return(image->channel_map[CrPixelChannel].traits);
105 static inline Quantum GetPixelCyan(const Image *image,const Quantum *pixel)
107 return(pixel[image->channel_map[CyanPixelChannel].channel]);
110 static inline PixelTrait GetPixelCyanTraits(const Image *image)
112 return(image->channel_map[CyanPixelChannel].traits);
115 static inline Quantum GetPixelGray(const Image *image,const Quantum *pixel)
117 return(pixel[image->channel_map[GrayPixelChannel].channel]);
120 static inline PixelTrait GetPixelGrayTraits(const Image *image)
122 return(image->channel_map[GrayPixelChannel].traits);
125 static inline Quantum GetPixelGreen(const Image *image,const Quantum *pixel)
127 return(pixel[image->channel_map[GreenPixelChannel].channel]);
130 static inline PixelTrait GetPixelGreenTraits(const Image *image)
132 return(image->channel_map[GreenPixelChannel].traits);
135 static inline Quantum GetPixelIndex(const Image *image,const Quantum *pixel)
137 return(pixel[image->channel_map[IndexPixelChannel].channel]);
140 static inline PixelTrait GetPixelIndexTraits(const Image *image)
142 return(image->channel_map[IndexPixelChannel].traits);
145 static inline Quantum GetPixelInfoIntensity(const PixelInfo *pixel_info)
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));
151 return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*
156 static inline Quantum GetPixelInfoLuminance(const PixelInfo *pixel_info)
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);
165 luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
166 0.07217*pixel_info->blue);
168 return((Quantum) luminance);
171 static inline Quantum GetPixelMagenta(const Image *image,const Quantum *pixel)
173 return(pixel[image->channel_map[MagentaPixelChannel].channel]);
176 static inline PixelTrait GetPixelMagentaTraits(const Image *image)
178 return(image->channel_map[MagentaPixelChannel].traits);
181 static inline size_t GetPixelMetaChannels(const Image *image)
183 return(image->number_meta_channels);
186 static inline size_t GetPixelMetacontentExtent(const Image *image)
188 return(image->metacontent_extent);
191 static inline Quantum GetPixelRed(const Image *image,const Quantum *pixel)
193 return(pixel[image->channel_map[RedPixelChannel].channel]);
196 static inline PixelTrait GetPixelRedTraits(const Image *image)
198 return(image->channel_map[RedPixelChannel].traits);
201 static inline void GetPixelPacket(const Image *image,const Quantum *pixel,
204 packet->red=GetPixelRed(image,pixel);
205 packet->green=GetPixelGreen(image,pixel);
206 packet->blue=GetPixelBlue(image,pixel);
207 packet->alpha=GetPixelAlpha(image,pixel);
210 static inline Quantum GetPixelPacketIntensity(const PixelPacket *pixel)
212 #if !defined(MAGICKCORE_HDRI_SUPPORT)
213 if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
215 return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue+0.5));
222 alpha=pixel->red-pixel->green;
223 beta=pixel->green-pixel->blue;
224 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
226 return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue));
231 static inline PixelTrait GetPixelTraits(const Image *image,
232 const PixelChannel channel)
234 return(image->channel_map[channel].traits);
237 static inline Quantum GetPixelY(const Image *image,const Quantum *pixel)
239 return(pixel[image->channel_map[YPixelChannel].channel]);
242 static inline PixelTrait GetPixelYTraits(const Image *image)
244 return(image->channel_map[YPixelChannel].traits);
247 static inline Quantum GetPixelYellow(const Image *image,const Quantum *pixel)
249 return(pixel[image->channel_map[YellowPixelChannel].channel]);
252 static inline PixelTrait GetPixelYellowTraits(const Image *image)
254 return(image->channel_map[YellowPixelChannel].traits);
257 static inline MagickBooleanType IsPixelEquivalent(const Image *image,
258 const Quantum *p,const PixelPacket *q)
260 if ((GetPixelRed(image,p) == q->red) &&
261 (GetPixelGreen(image,p) == q->green) &&
262 (GetPixelBlue(image,p) == q->blue))
267 static inline MagickBooleanType IsPixelGray(const Image *image,
268 const Quantum *pixel)
270 #if !defined(MAGICKCORE_HDRI_SUPPORT)
271 if ((GetPixelRed(image,pixel) == GetPixelGreen(image,pixel)) &&
272 (GetPixelGreen(image,pixel) == GetPixelBlue(image,pixel)))
280 alpha=GetPixelRed(image,pixel)-(double) GetPixelGreen(image,pixel);
281 beta=GetPixelGreen(image,pixel)-(double) GetPixelBlue(image,pixel);
282 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
289 static inline MagickBooleanType IsPixelInfoEquivalent(const PixelInfo *p,
292 #if !defined(MAGICKCORE_HDRI_SUPPORT)
293 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
294 (p->alpha != OpaqueAlpha))
296 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
297 (q->alpha != OpaqueAlpha))
299 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
301 if (p->alpha != q->alpha)
303 if (p->alpha == TransparentAlpha)
306 if (p->red != q->red)
308 if (p->green != q->green)
310 if (p->blue != q->blue)
312 if ((p->colorspace == CMYKColorspace) && (p->black != q->black))
315 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
316 (fabs(p->alpha-OpaqueAlpha) > 0.5))
318 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
319 (fabs(q->alpha-OpaqueAlpha)) > 0.5)
321 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
323 if (fabs(p->alpha-q->alpha) > 0.5)
325 if (fabs(p->alpha-TransparentAlpha) <= 0.5)
328 if (fabs(p->red-q->red) > 0.5)
330 if (fabs(p->green-q->green) > 0.5)
332 if (fabs(p->blue-q->blue) > 0.5)
334 if ((p->colorspace == CMYKColorspace) && (fabs(p->black-q->black) > 0.5))
340 static inline MagickBooleanType IsPixelMonochrome(const Image *image,
341 const Quantum *pixel)
343 #if !defined(MAGICKCORE_HDRI_SUPPORT)
344 if (((GetPixelRed(image,pixel) == 0) ||
345 (GetPixelRed(image,pixel) == (Quantum) QuantumRange)) &&
346 (GetPixelRed(image,pixel) == GetPixelGreen(image,pixel)) &&
347 (GetPixelGreen(image,pixel) == GetPixelBlue(image,pixel)))
355 alpha=GetPixelRed(image,pixel)-(double) GetPixelGreen(image,pixel);
356 beta=GetPixelGreen(image,pixel)-(double) GetPixelBlue(image,pixel);
357 if (((fabs(GetPixelRed(image,pixel)) <= MagickEpsilon) ||
358 (fabs(GetPixelRed(image,pixel)-QuantumRange) <= MagickEpsilon)) &&
359 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
366 static inline MagickBooleanType IsPixelPacketEquivalent(const PixelPacket *p,
367 const PixelPacket *q)
369 if ((p->red == q->red) && (p->green == q->green) && (p->blue == q->blue))
374 static inline MagickBooleanType IsPixelPacketGray(const PixelPacket *pixel)
376 #if !defined(MAGICKCORE_HDRI_SUPPORT)
377 if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
385 alpha=pixel->red-(double) pixel->green;
386 beta=pixel->green-(double) pixel->blue;
387 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
394 static inline MagickBooleanType IsPixelPacketMonochrome(
395 const PixelPacket *pixel)
397 #if !defined(MAGICKCORE_HDRI_SUPPORT)
398 if (((pixel->red == 0) || (pixel->red == (Quantum) QuantumRange)) &&
399 (pixel->red == pixel->green) && (pixel->green == pixel->blue))
407 alpha=pixel->red-(double) pixel->green;
408 beta=pixel->green-(double) pixel->blue;
409 if (((fabs(pixel->red) <= MagickEpsilon) ||
410 (fabs(pixel->red-QuantumRange) <= MagickEpsilon)) &&
411 (fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
418 static inline void SetPacketPixelInfo(const Image *image,
419 const PixelInfo *pixel_info,PixelPacket *packet)
421 if ((image->channel_map[RedPixelChannel].traits & UpdatePixelTrait) != 0)
422 packet->red=ClampToQuantum(pixel_info->red);
423 if ((image->channel_map[GreenPixelChannel].traits & UpdatePixelTrait) != 0)
424 packet->green=ClampToQuantum(pixel_info->green);
425 if ((image->channel_map[BluePixelChannel].traits & UpdatePixelTrait) != 0)
426 packet->blue=ClampToQuantum(pixel_info->blue);
427 if ((image->channel_map[BlackPixelChannel].traits & UpdatePixelTrait) != 0)
428 packet->black=ClampToQuantum(pixel_info->black);
429 if ((image->channel_map[AlphaPixelChannel].traits & CopyPixelTrait) != 0)
430 packet->alpha=ClampToQuantum(pixel_info->alpha);
431 if ((image->channel_map[IndexPixelChannel].traits & CopyPixelTrait) != 0)
432 packet->index=ClampToQuantum(pixel_info->index);
435 static inline void SetPixelAlpha(const Image *image,const Quantum alpha,
438 pixel[image->channel_map[AlphaPixelChannel].channel]=alpha;
441 static inline void SetPixelAlphaTraits(Image *image,const PixelTrait traits)
443 image->channel_map[AlphaPixelChannel].traits=traits;
446 static inline void SetPixelBlack(const Image *image,const Quantum black,
449 pixel[image->channel_map[BlackPixelChannel].channel]=black;
452 static inline void SetPixelBlackTraits(Image *image,const PixelTrait traits)
454 image->channel_map[BlackPixelChannel].traits=traits;
457 static inline void SetPixelBlue(const Image *image,const Quantum blue,
460 pixel[image->channel_map[BluePixelChannel].channel]=blue;
463 static inline void SetPixelBlueTraits(Image *image,const PixelTrait traits)
465 image->channel_map[BluePixelChannel].traits=traits;
468 static inline void SetPixelCb(const Image *image,const Quantum cb,
471 pixel[image->channel_map[CbPixelChannel].channel]=cb;
474 static inline void SetPixelCbTraits(Image *image,const PixelTrait traits)
476 image->channel_map[CbPixelChannel].traits=traits;
479 static inline void SetPixelChannel(const Image *image,
480 const PixelChannel channel,const Quantum quantum,Quantum *pixel)
482 pixel[image->channel_map[channel].channel]=quantum;
485 static inline void SetPixelChannelMapChannel(const Image *image,
486 const PixelChannel channel,const PixelChannel channels)
488 image->channel_map[channel].channel=channels;
491 static inline void SetPixelChannels(Image *image,const size_t number_channels)
493 image->number_channels=number_channels;
496 static inline void SetPixelChannelTraits(Image *image,
497 const PixelChannel channel,const PixelTrait traits)
499 image->channel_map[channel].traits=traits;
502 static inline void SetPixelChannelMapTraits(Image *image,
503 const PixelChannel channel,const PixelTrait traits)
505 image->channel_map[channel].traits=traits;
508 static inline void SetPixelCr(const Image *image,const Quantum cr,
511 pixel[image->channel_map[CrPixelChannel].channel]=cr;
514 static inline void SetPixelCrTraits(Image *image,const PixelTrait traits)
516 image->channel_map[CrPixelChannel].traits=traits;
519 static inline void SetPixelCyan(const Image *image,const Quantum cyan,
522 pixel[image->channel_map[CyanPixelChannel].channel]=cyan;
525 static inline void SetPixelGray(const Image *image,const Quantum gray,
528 pixel[image->channel_map[GrayPixelChannel].channel]=gray;
531 static inline void SetPixelGrayTraits(Image *image,const PixelTrait traits)
533 image->channel_map[GrayPixelChannel].traits=traits;
536 static inline void SetPixelGreen(const Image *image,const Quantum green,
539 pixel[image->channel_map[GreenPixelChannel].channel]=green;
542 static inline void SetPixelGreenTraits(Image *image,const PixelTrait traits)
544 image->channel_map[GreenPixelChannel].traits=traits;
547 static inline void SetPixelIndex(const Image *image,const Quantum index,
550 pixel[image->channel_map[IndexPixelChannel].channel]=index;
553 static inline void SetPixelIndexTraits(Image *image,const PixelTrait traits)
555 image->channel_map[IndexPixelChannel].traits=traits;
558 static inline void SetPixelInfo(const Image *image,const Quantum *pixel,
559 PixelInfo *pixel_info)
561 if ((image->channel_map[RedPixelChannel].traits & UpdatePixelTrait) != 0)
562 pixel_info->red=(MagickRealType) GetPixelRed(image,pixel);
563 if ((image->channel_map[GreenPixelChannel].traits & UpdatePixelTrait) != 0)
564 pixel_info->green=(MagickRealType) GetPixelGreen(image,pixel);
565 if ((image->channel_map[BluePixelChannel].traits & UpdatePixelTrait) != 0)
566 pixel_info->blue=(MagickRealType) GetPixelBlue(image,pixel);
567 if ((image->channel_map[BlackPixelChannel].traits & UpdatePixelTrait) != 0)
568 pixel_info->black=(MagickRealType) GetPixelBlack(image,pixel);
569 if ((image->channel_map[AlphaPixelChannel].traits & CopyPixelTrait) != 0)
570 pixel_info->alpha=(MagickRealType) GetPixelAlpha(image,pixel);
571 if ((image->channel_map[IndexPixelChannel].traits & CopyPixelTrait) != 0)
572 pixel_info->index=(MagickRealType) GetPixelIndex(image,pixel);
575 static inline void SetPixelInfoBias(const Image *image,PixelInfo *pixel_info)
578 Obsoleted by MorphologyApply().
580 pixel_info->red=image->bias;
581 pixel_info->green=image->bias;
582 pixel_info->blue=image->bias;
583 pixel_info->alpha=image->bias;
584 pixel_info->black=image->bias;
587 static inline void SetPixelInfoPacket(const Image *image,
588 const PixelPacket *pixel,PixelInfo *pixel_info)
590 pixel_info->red=(MagickRealType) pixel->red;
591 pixel_info->green=(MagickRealType) pixel->green;
592 pixel_info->blue=(MagickRealType) pixel->blue;
593 pixel_info->alpha=(MagickRealType) pixel->alpha;
594 if (image->colorspace == CMYKColorspace)
595 pixel_info->black=(MagickRealType) pixel->black;
596 if (image->storage_class == PseudoClass)
597 pixel_info->index=(MagickRealType) pixel->index;
600 static inline void SetPixelMagenta(const Image *image,const Quantum magenta,
603 pixel[image->channel_map[MagentaPixelChannel].channel]=magenta;
606 static inline void SetPixelMagentaTraits(Image *image,const PixelTrait traits)
608 image->channel_map[MagentaPixelChannel].traits=traits;
611 static inline void SetPixelMetaChannels(Image *image,
612 const size_t number_meta_channels)
614 image->number_meta_channels=number_meta_channels;
617 static inline void SetPixelMetacontentExtent(Image *image,const size_t extent)
619 image->metacontent_extent=extent;
622 static inline void SetPixelRed(const Image *image,const Quantum red,
625 pixel[image->channel_map[RedPixelChannel].channel]=red;
628 static inline void SetPixelRedTraits(Image *image,const PixelTrait traits)
630 image->channel_map[RedPixelChannel].traits=traits;
633 static inline void SetPixelPacket(const Image *image,const PixelPacket *packet,
636 SetPixelRed(image,packet->red,pixel);
637 SetPixelGreen(image,packet->green,pixel);
638 SetPixelBlue(image,packet->blue,pixel);
639 SetPixelAlpha(image,packet->alpha,pixel);
642 static inline void SetPixelPixelInfo(const Image *image,
643 const PixelInfo *pixel_info,Quantum *packet)
645 SetPixelRed(image,ClampToQuantum(pixel_info->red),packet);
646 SetPixelGreen(image,ClampToQuantum(pixel_info->green),packet);
647 SetPixelBlue(image,ClampToQuantum(pixel_info->blue),packet);
648 if (image->matte != MagickFalse)
649 SetPixelAlpha(image,ClampToQuantum(pixel_info->alpha),packet);
650 if (image->colorspace == CMYKColorspace)
651 SetPixelBlack(image,ClampToQuantum(pixel_info->black),packet);
654 static inline void SetPixelYellow(const Image *image,const Quantum yellow,
657 pixel[image->channel_map[YellowPixelChannel].channel]=yellow;
660 static inline void SetPixelYellowTraits(Image *image,const PixelTrait traits)
662 image->channel_map[YellowPixelChannel].traits=traits;
665 static inline void SetPixelY(const Image *image,const Quantum y,Quantum *pixel)
667 pixel[image->channel_map[YPixelChannel].channel]=y;
670 static inline void SetPixelYTraits(Image *image,const PixelTrait traits)
672 image->channel_map[YPixelChannel].traits=traits;
675 static inline Quantum GetPixelIntensity(const Image *image,const Quantum *pixel)
677 #if !defined(MAGICKCORE_HDRI_SUPPORT)
678 if ((GetPixelRed(image,pixel) == GetPixelGreen(image,pixel)) &&
679 (GetPixelGreen(image,pixel) == GetPixelBlue(image,pixel)))
680 return(GetPixelRed(image,pixel));
681 return((Quantum) (0.299*GetPixelRed(image,pixel)+0.587*
682 GetPixelGreen(image,pixel)+0.114*GetPixelBlue(image,pixel)+0.5));
689 alpha=GetPixelRed(image,pixel)-(double) GetPixelGreen(image,pixel);
690 beta=GetPixelGreen(image,pixel)-(double) GetPixelBlue(image,pixel);
691 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
692 return(GetPixelRed(image,pixel));
693 return((Quantum) (0.299*GetPixelRed(image,pixel)+0.587*
694 GetPixelGreen(image,pixel)+0.114*GetPixelBlue(image,pixel)));
699 static inline Quantum GetPixelLuminance(const Image *image,const Quantum *pixel)
701 #if !defined(MAGICKCORE_HDRI_SUPPORT)
702 return((Quantum) (0.21267*GetPixelRed(image,pixel)+0.71516*
703 GetPixelGreen(image,pixel)+0.07217*GetPixelBlue(image,pixel)+0.5));
705 return((Quantum) (0.21267*GetPixelRed(image,pixel)+0.71516*
706 GetPixelGreen(image,pixel)+0.07217*GetPixelBlue(image,pixel)));
710 #if defined(__cplusplus) || defined(c_plusplus)