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>
30 static inline Quantum GetPixelAlpha(const Image *image,const Quantum *pixel)
32 return(pixel[image->component_map[AlphaPixelComponent].component]);
35 static inline PixelType GetPixelAlphaType(const Image *image)
37 return(image->component_map[AlphaPixelComponent].type);
40 static inline Quantum GetPixelBlack(const Image *image,const Quantum *pixel)
42 return(pixel[image->component_map[BlackPixelComponent].component]);
45 static inline PixelType GetPixelBlackType(const Image *image)
47 return(image->component_map[BlackPixelComponent].type);
50 static inline Quantum GetPixelBlue(const Image *image,const Quantum *pixel)
52 return(pixel[image->component_map[BluePixelComponent].component]);
55 static inline PixelType GetPixelBlueType(const Image *image)
57 return(image->component_map[BluePixelComponent].type);
60 static inline Quantum GetPixelCb(const Image *image,const Quantum *pixel)
62 return(pixel[image->component_map[CbPixelComponent].component]);
65 static inline PixelType GetPixelCbType(const Image *image)
67 return(image->component_map[CbPixelComponent].type);
70 static inline size_t GetPixelChannels(const Image *image)
72 return(image->pixel_channels);
75 static inline Quantum GetPixelComponent(const Image *image,
76 const PixelComponent component)
78 return(image->component_map[component].component);
81 static inline Quantum GetPixelCr(const Image *image,const Quantum *pixel)
83 return(pixel[image->component_map[CrPixelComponent].component]);
86 static inline PixelType GetPixelCrType(const Image *image)
88 return(image->component_map[CrPixelComponent].type);
91 static inline Quantum GetPixelCyan(const Image *image,const Quantum *pixel)
93 return(pixel[image->component_map[CyanPixelComponent].component]);
96 static inline PixelType GetPixelCyanType(const Image *image)
98 return(image->component_map[CyanPixelComponent].type);
101 static inline Quantum GetPixelGray(const Image *image,const Quantum *pixel)
103 return(pixel[image->component_map[GrayPixelComponent].component]);
106 static inline PixelType GetPixelGrayType(const Image *image)
108 return(image->component_map[GrayPixelComponent].type);
111 static inline Quantum GetPixelGreen(const Image *image,const Quantum *pixel)
113 return(pixel[image->component_map[GreenPixelComponent].component]);
116 static inline PixelType GetPixelGreenType(const Image *image)
118 return(image->component_map[GreenPixelComponent].type);
121 static inline Quantum GetPixelIndex(const Image *image,const Quantum *pixel)
123 return(pixel[image->component_map[IndexPixelComponent].component]);
126 static inline PixelType GetPixelIndexType(const Image *image)
128 return(image->component_map[IndexPixelComponent].type);
131 static inline Quantum GetPixelInfoIntensity(const PixelInfo *pixel_info)
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));
136 return((Quantum) (0.299*pixel_info->red+0.587*pixel_info->green+0.114*pixel_info->blue));
140 static inline Quantum GetPixelInfoLuminance(const PixelInfo *pixel_info)
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);
149 luminance=(Quantum) (0.21267*pixel_info->red+0.71516*pixel_info->green+
150 0.07217*pixel_info->blue);
152 return((Quantum) luminance);
155 static inline Quantum GetPixelMagenta(const Image *image,
156 const Quantum *pixel)
158 return(pixel[image->component_map[MagentaPixelComponent].component]);
161 static inline PixelType GetPixelMagentaType(const Image *image)
163 return(image->component_map[MagentaPixelComponent].type);
166 static inline size_t GetPixelMetacontentExtent(const Image *image)
168 return(image->metacontent_extent);
171 static inline Quantum GetPixelRed(const Image *image,const Quantum *pixel)
173 return(pixel[image->component_map[RedPixelComponent].component]);
176 static inline PixelType GetPixelRedType(const Image *image)
178 return(image->component_map[RedPixelComponent].type);
181 static inline void GetPixelPacket(const Image *image,const Quantum *pixel,
184 packet->red=GetPixelRed(image,pixel);
185 packet->green=GetPixelGreen(image,pixel);
186 packet->blue=GetPixelBlue(image,pixel);
187 packet->alpha=GetPixelAlpha(image,pixel);
190 static inline Quantum GetPixelPacketIntensity(const PixelPacket *pixel)
192 #if !defined(MAGICKCORE_HDRI_SUPPORT)
193 if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
195 return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue+0.5));
202 alpha=pixel->red-pixel->green;
203 beta=pixel->green-pixel->blue;
204 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
206 return((Quantum) (0.299*pixel->red+0.587*pixel->green+0.114*pixel->blue));
211 static inline PixelType GetPixelType(const Image *image,
212 const PixelComponent component)
214 return(image->component_map[component].type);
217 static inline Quantum GetPixelY(const Image *image,const Quantum *pixel)
219 return(pixel[image->component_map[YPixelComponent].component]);
222 static inline PixelType GetPixelYType(const Image *image)
224 return(image->component_map[YPixelComponent].type);
227 static inline Quantum GetPixelYellow(const Image *image,
228 const Quantum *pixel)
230 return(pixel[image->component_map[YellowPixelComponent].component]);
233 static inline PixelType GetPixelYellowType(const Image *image)
235 return(image->component_map[YellowPixelComponent].type);
238 static inline MagickBooleanType IsPixelEquivalent(const Image *image,
239 const Quantum *p,const PixelPacket *q)
241 if ((GetPixelRed(image,p) == q->red) &&
242 (GetPixelGreen(image,p) == q->green) &&
243 (GetPixelBlue(image,p) == q->blue))
248 static inline MagickBooleanType IsPixelGray(const Image *image,
249 const Quantum *pixel)
251 #if !defined(MAGICKCORE_HDRI_SUPPORT)
252 if ((GetPixelRed(image,pixel) == GetPixelGreen(image,pixel)) &&
253 (GetPixelGreen(image,pixel) == GetPixelBlue(image,pixel)))
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))
270 static inline MagickBooleanType IsPixelInfoEquivalent(const PixelInfo *p,
273 #if !defined(MAGICKCORE_HDRI_SUPPORT)
274 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
275 (p->alpha != OpaqueAlpha))
277 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
278 (q->alpha != OpaqueAlpha))
280 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
282 if (p->alpha != q->alpha)
284 if (p->alpha == TransparentAlpha)
287 if (p->red != q->red)
289 if (p->green != q->green)
291 if (p->blue != q->blue)
293 if ((p->colorspace == CMYKColorspace) && (p->black != q->black))
296 if ((p->matte != MagickFalse) && (q->matte == MagickFalse) &&
297 (fabs(p->alpha-OpaqueAlpha) > 0.5))
299 if ((q->matte != MagickFalse) && (p->matte == MagickFalse) &&
300 (fabs(q->alpha-OpaqueAlpha)) > 0.5)
302 if ((p->matte != MagickFalse) && (q->matte != MagickFalse))
304 if (fabs(p->alpha-q->alpha) > 0.5)
306 if (fabs(p->alpha-TransparentAlpha) <= 0.5)
309 if (fabs(p->red-q->red) > 0.5)
311 if (fabs(p->green-q->green) > 0.5)
313 if (fabs(p->blue-q->blue) > 0.5)
315 if ((p->colorspace == CMYKColorspace) && (fabs(p->black-q->black) > 0.5))
321 static inline MagickBooleanType IsPixelMonochrome(const Image *image,
322 const Quantum *pixel)
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)))
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))
347 static inline MagickBooleanType IsPixelPacketEquivalent(const PixelPacket *p,
348 const PixelPacket *q)
350 if ((p->red == q->red) && (p->green == q->green) && (p->blue == q->blue))
355 static inline MagickBooleanType IsPixelPacketGray(const PixelPacket *pixel)
357 #if !defined(MAGICKCORE_HDRI_SUPPORT)
358 if ((pixel->red == pixel->green) && (pixel->green == pixel->blue))
366 alpha=pixel->red-(double) pixel->green;
367 beta=pixel->green-(double) pixel->blue;
368 if ((fabs(alpha) <= MagickEpsilon) && (fabs(beta) <= MagickEpsilon))
375 static inline MagickBooleanType IsPixelPacketMonochrome(
376 const PixelPacket *pixel)
378 #if !defined(MAGICKCORE_HDRI_SUPPORT)
379 if (((pixel->red == 0) || (pixel->red == (Quantum) QuantumRange)) &&
380 (pixel->red == pixel->green) && (pixel->green == pixel->blue))
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))
399 static inline void SetPacketPixelInfo(const Image *image,
400 const PixelInfo *pixel_info,PixelPacket *packet)
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);
412 static inline void SetPixelAlpha(const Image *image,const Quantum alpha,
415 pixel[image->component_map[AlphaPixelComponent].component]=alpha;
418 static inline void SetPixelAlphaType(const Image *image,const PixelType type)
420 image->component_map[AlphaPixelComponent].type=type;
423 static inline void SetPixelBlack(const Image *image,const Quantum black,
426 pixel[image->component_map[BlackPixelComponent].component]=black;
429 static inline void SetPixelBlackType(const Image *image,const PixelType type)
431 image->component_map[BlackPixelComponent].type=type;
434 static inline void SetPixelBlue(const Image *image,const Quantum blue,
437 pixel[image->component_map[BluePixelComponent].component]=blue;
440 static inline void SetPixelBlueType(const Image *image,const PixelType type)
442 image->component_map[BluePixelComponent].type=type;
445 static inline void SetPixelCb(const Image *image,const Quantum cb,
448 pixel[image->component_map[CbPixelComponent].component]=cb;
451 static inline void SetPixelCbType(const Image *image,const PixelType type)
453 image->component_map[CbPixelComponent].type=type;
456 static inline void SetPixelChannels(Image *image,const size_t channels)
458 image->pixel_channels=channels;
461 static inline void SetPixelComponent(const Image *image,
462 const PixelComponent component,const PixelComponent components)
464 image->component_map[component].component=components;
467 static inline void SetPixelCr(const Image *image,const Quantum cr,
470 pixel[image->component_map[CrPixelComponent].component]=cr;
473 static inline void SetPixelCrType(const Image *image,const PixelType type)
475 image->component_map[CrPixelComponent].type=type;
478 static inline void SetPixelCyan(const Image *image,const Quantum cyan,
481 pixel[image->component_map[CyanPixelComponent].component]=cyan;
484 static inline void SetPixelGray(const Image *image,const Quantum gray,
487 pixel[image->component_map[GrayPixelComponent].component]=gray;
490 static inline void SetPixelGrayType(const Image *image,const PixelType type)
492 image->component_map[GrayPixelComponent].type=type;
495 static inline void SetPixelGreen(const Image *image,const Quantum green,
498 pixel[image->component_map[GreenPixelComponent].component]=green;
501 static inline void SetPixelGreenType(const Image *image,const PixelType type)
503 image->component_map[GreenPixelComponent].type=type;
506 static inline void SetPixelIndex(const Image *image,const Quantum index,
509 pixel[image->component_map[IndexPixelComponent].component]=index;
512 static inline void SetPixelIndexType(const Image *image,const PixelType type)
514 image->component_map[IndexPixelComponent].type=type;
517 static inline void SetPixelInfo(const Image *image,const Quantum *pixel,
518 PixelInfo *pixel_info)
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);
530 static inline void SetPixelInfoBias(const Image *image,
531 PixelInfo *pixel_info)
534 Obsoleted by MorphologyApply().
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;
543 static inline void SetPixelInfoPacket(const Image *image,
544 const PixelPacket *pixel,PixelInfo *pixel_info)
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;
556 static inline void SetPixelMagenta(const Image *image,const Quantum magenta,
559 pixel[image->component_map[MagentaPixelComponent].component]=magenta;
562 static inline void SetPixelMagentaType(const Image *image,const PixelType type)
564 image->component_map[MagentaPixelComponent].type=type;
567 static inline void SetPixelMetacontentExtent(Image *image,const size_t extent)
569 image->metacontent_extent=extent;
572 static inline void SetPixelRed(const Image *image,const Quantum red,
575 pixel[image->component_map[RedPixelComponent].component]=red;
578 static inline void SetPixelRedType(const Image *image,const PixelType type)
580 image->component_map[RedPixelComponent].type=type;
583 static inline void SetPixelPacket(const Image *image,const PixelPacket *packet,
586 SetPixelRed(image,packet->red,pixel);
587 SetPixelGreen(image,packet->green,pixel);
588 SetPixelBlue(image,packet->blue,pixel);
589 SetPixelAlpha(image,packet->alpha,pixel);
592 static inline void SetPixelPixelInfo(const Image *image,
593 const PixelInfo *pixel_info,Quantum *packet)
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);
603 static inline void SetPixelType(const Image *image,
604 const PixelComponent component,const PixelType type)
606 image->component_map[component].type=type;
609 static inline void SetPixelYellow(const Image *image,const Quantum yellow,
612 pixel[image->component_map[YellowPixelComponent].component]=yellow;
615 static inline void SetPixelYellowType(const Image *image,const PixelType type)
617 image->component_map[YellowPixelComponent].type=type;
620 static inline void SetPixelY(const Image *image,const Quantum y,
623 pixel[image->component_map[YPixelComponent].component]=y;
626 static inline void SetPixelYType(const Image *image,const PixelType type)
628 image->component_map[YPixelComponent].type=type;
631 static inline Quantum GetPixelIntensity(const Image *image,
632 const Quantum *pixel)
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));
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)));
656 #if defined(__cplusplus) || defined(c_plusplus)