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[image->map][AlphaPixelComponent].component]);
35 static inline PixelTrait GetPixelAlphaTraits(const Image *image)
37 return(image->component_map[image->map][AlphaPixelComponent].traits);
40 static inline Quantum GetPixelBlack(const Image *image,const Quantum *pixel)
42 return(pixel[image->component_map[image->map][BlackPixelComponent].component]);
45 static inline PixelTrait GetPixelBlackTraits(const Image *image)
47 return(image->component_map[image->map][BlackPixelComponent].traits);
50 static inline Quantum GetPixelBlue(const Image *image,const Quantum *pixel)
52 return(pixel[image->component_map[image->map][BluePixelComponent].component]);
55 static inline PixelTrait GetPixelBlueTraits(const Image *image)
57 return(image->component_map[image->map][BluePixelComponent].traits);
60 static inline Quantum GetPixelCb(const Image *image,const Quantum *pixel)
62 return(pixel[image->component_map[image->map][CbPixelComponent].component]);
65 static inline PixelTrait GetPixelCbTraits(const Image *image)
67 return(image->component_map[image->map][CbPixelComponent].traits);
70 static inline Quantum GetPixelComponent(const Image *image,
71 const PixelComponent component)
73 return(image->component_map[image->map][component].component);
76 static inline size_t GetPixelComponents(const Image *image)
78 return(image->pixel_components);
81 static inline Quantum GetPixelCr(const Image *image,const Quantum *pixel)
83 return(pixel[image->component_map[image->map][CrPixelComponent].component]);
86 static inline PixelTrait GetPixelCrTraits(const Image *image)
88 return(image->component_map[image->map][CrPixelComponent].traits);
91 static inline Quantum GetPixelCyan(const Image *image,const Quantum *pixel)
93 return(pixel[image->component_map[image->map][CyanPixelComponent].component]);
96 static inline PixelTrait GetPixelCyanTraits(const Image *image)
98 return(image->component_map[image->map][CyanPixelComponent].traits);
101 static inline Quantum GetPixelGray(const Image *image,const Quantum *pixel)
103 return(pixel[image->component_map[image->map][GrayPixelComponent].component]);
106 static inline PixelTrait GetPixelGrayTraits(const Image *image)
108 return(image->component_map[image->map][GrayPixelComponent].traits);
111 static inline Quantum GetPixelGreen(const Image *image,const Quantum *pixel)
113 return(pixel[image->component_map[image->map][GreenPixelComponent].component]);
116 static inline PixelTrait GetPixelGreenTraits(const Image *image)
118 return(image->component_map[image->map][GreenPixelComponent].traits);
121 static inline Quantum GetPixelIndex(const Image *image,const Quantum *pixel)
123 return(pixel[image->component_map[image->map][IndexPixelComponent].component]);
126 static inline PixelTrait GetPixelIndexTraits(const Image *image)
128 return(image->component_map[image->map][IndexPixelComponent].traits);
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[image->map][MagentaPixelComponent].component]);
161 static inline PixelTrait GetPixelMagentaTraits(const Image *image)
163 return(image->component_map[image->map][MagentaPixelComponent].traits);
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[image->map][RedPixelComponent].component]);
176 static inline PixelTrait GetPixelRedTraits(const Image *image)
178 return(image->component_map[image->map][RedPixelComponent].traits);
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 PixelTrait GetPixelTraits(const Image *image,
212 const PixelComponent component)
214 return(image->component_map[image->map][component].traits);
217 static inline Quantum GetPixelY(const Image *image,const Quantum *pixel)
219 return(pixel[image->component_map[image->map][YPixelComponent].component]);
222 static inline PixelTrait GetPixelYTraits(const Image *image)
224 return(image->component_map[image->map][YPixelComponent].traits);
227 static inline Quantum GetPixelYellow(const Image *image,
228 const Quantum *pixel)
230 return(pixel[image->component_map[image->map][YellowPixelComponent].component]);
233 static inline PixelTrait GetPixelYellowTraits(const Image *image)
235 return(image->component_map[image->map][YellowPixelComponent].traits);
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[image->map][AlphaPixelComponent].component]=alpha;
418 static inline void SetPixelAlphaTraits(Image *image,const PixelTrait traits)
420 image->component_map[image->map][AlphaPixelComponent].traits=traits;
423 static inline void SetPixelBlack(const Image *image,const Quantum black,
426 pixel[image->component_map[image->map][BlackPixelComponent].component]=black;
429 static inline void SetPixelBlackTraits(Image *image,const PixelTrait traits)
431 image->component_map[image->map][BlackPixelComponent].traits=traits;
434 static inline void SetPixelBlue(const Image *image,const Quantum blue,
437 pixel[image->component_map[image->map][BluePixelComponent].component]=blue;
440 static inline void SetPixelBlueTraits(Image *image,const PixelTrait traits)
442 image->component_map[image->map][BluePixelComponent].traits=traits;
445 static inline void SetPixelCb(const Image *image,const Quantum cb,
448 pixel[image->component_map[image->map][CbPixelComponent].component]=cb;
451 static inline void SetPixelCbTraits(Image *image,const PixelTrait traits)
453 image->component_map[image->map][CbPixelComponent].traits=traits;
456 static inline void SetPixelComponent(const Image *image,
457 const PixelComponent component,const PixelComponent components)
459 image->component_map[image->map][component].component=components;
462 static inline void SetPixelComponents(Image *image,const size_t channels)
464 image->pixel_components=channels;
467 static inline void SetPixelComponentTraits(Image *image,
468 const PixelComponent component,const PixelTrait traits)
470 image->component_map[image->map][component].traits=traits;
473 static inline void SetPixelCr(const Image *image,const Quantum cr,
476 pixel[image->component_map[image->map][CrPixelComponent].component]=cr;
479 static inline void SetPixelCrTraits(Image *image,const PixelTrait traits)
481 image->component_map[image->map][CrPixelComponent].traits=traits;
484 static inline void SetPixelCyan(const Image *image,const Quantum cyan,
487 pixel[image->component_map[image->map][CyanPixelComponent].component]=cyan;
490 static inline void SetPixelGray(const Image *image,const Quantum gray,
493 pixel[image->component_map[image->map][GrayPixelComponent].component]=gray;
496 static inline void SetPixelGrayTraits(Image *image,const PixelTrait traits)
498 image->component_map[image->map][GrayPixelComponent].traits=traits;
501 static inline void SetPixelGreen(const Image *image,const Quantum green,
504 pixel[image->component_map[image->map][GreenPixelComponent].component]=green;
507 static inline void SetPixelGreenTraits(Image *image,const PixelTrait traits)
509 image->component_map[image->map][GreenPixelComponent].traits=traits;
512 static inline void SetPixelIndex(const Image *image,const Quantum index,
515 pixel[image->component_map[image->map][IndexPixelComponent].component]=index;
518 static inline void SetPixelIndexTraits(Image *image,const PixelTrait traits)
520 image->component_map[image->map][IndexPixelComponent].traits=traits;
523 static inline void SetPixelInfo(const Image *image,const Quantum *pixel,
524 PixelInfo *pixel_info)
526 pixel_info->red=(MagickRealType) GetPixelRed(image,pixel);
527 pixel_info->green=(MagickRealType) GetPixelGreen(image,pixel);
528 pixel_info->blue=(MagickRealType) GetPixelBlue(image,pixel);
529 pixel_info->alpha=(MagickRealType) GetPixelAlpha(image,pixel);
530 if (image->colorspace == CMYKColorspace)
531 pixel_info->black=(MagickRealType) GetPixelBlack(image,pixel);
532 if (image->storage_class == PseudoClass)
533 pixel_info->index=(MagickRealType) GetPixelIndex(image,pixel);
536 static inline void SetPixelInfoBias(const Image *image,
537 PixelInfo *pixel_info)
540 Obsoleted by MorphologyApply().
542 pixel_info->red=image->bias;
543 pixel_info->green=image->bias;
544 pixel_info->blue=image->bias;
545 pixel_info->alpha=image->bias;
546 pixel_info->black=image->bias;
549 static inline void SetPixelInfoPacket(const Image *image,
550 const PixelPacket *pixel,PixelInfo *pixel_info)
552 pixel_info->red=(MagickRealType) pixel->red;
553 pixel_info->green=(MagickRealType) pixel->green;
554 pixel_info->blue=(MagickRealType) pixel->blue;
555 pixel_info->alpha=(MagickRealType) pixel->alpha;
556 if (image->colorspace == CMYKColorspace)
557 pixel_info->black=(MagickRealType) pixel->black;
558 if (image->storage_class == PseudoClass)
559 pixel_info->index=(MagickRealType) pixel->index;
562 static inline void SetPixelMagenta(const Image *image,const Quantum magenta,
565 pixel[image->component_map[image->map][MagentaPixelComponent].component]=magenta;
568 static inline void SetPixelMagentaTraits(Image *image,const PixelTrait traits)
570 image->component_map[image->map][MagentaPixelComponent].traits=traits;
573 static inline void SetPixelMetacontentExtent(Image *image,const size_t extent)
575 image->metacontent_extent=extent;
578 static inline void SetPixelRed(const Image *image,const Quantum red,
581 pixel[image->component_map[image->map][RedPixelComponent].component]=red;
584 static inline void SetPixelRedTraits(Image *image,const PixelTrait traits)
586 image->component_map[image->map][RedPixelComponent].traits=traits;
589 static inline void SetPixelPacket(const Image *image,const PixelPacket *packet,
592 SetPixelRed(image,packet->red,pixel);
593 SetPixelGreen(image,packet->green,pixel);
594 SetPixelBlue(image,packet->blue,pixel);
595 SetPixelAlpha(image,packet->alpha,pixel);
598 static inline void SetPixelPixelInfo(const Image *image,
599 const PixelInfo *pixel_info,Quantum *packet)
601 SetPixelRed(image,ClampToQuantum(pixel_info->red),packet);
602 SetPixelGreen(image,ClampToQuantum(pixel_info->green),packet);
603 SetPixelBlue(image,ClampToQuantum(pixel_info->blue),packet);
604 SetPixelAlpha(image,ClampToQuantum(pixel_info->alpha),packet);
605 if (image->colorspace == CMYKColorspace)
606 SetPixelBlack(image,ClampToQuantum(pixel_info->black),packet);
609 static inline void SetPixelYellow(const Image *image,const Quantum yellow,
612 pixel[image->component_map[image->map][YellowPixelComponent].component]=yellow;
615 static inline void SetPixelYellowTraits(Image *image,const PixelTrait traits)
617 image->component_map[image->map][YellowPixelComponent].traits=traits;
620 static inline void SetPixelY(const Image *image,const Quantum y,
623 pixel[image->component_map[image->map][YPixelComponent].component]=y;
626 static inline void SetPixelYTraits(Image *image,const PixelTrait traits)
628 image->component_map[image->map][YPixelComponent].traits=traits;
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)