2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO L OOO RRRR M M AAA PPPP %
7 % C O O L O O R R MM MM A A P P %
8 % C O O L O O RRRR M M M AAAAA PPPP %
9 % C O O L O O R R M M A A P %
10 % CCCC OOO LLLLL OOO R R M M A A P %
13 % MagickCore Colormap Methods %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % We use linked-lists because splay-trees do not currently support duplicate
37 % key / value pairs (.e.g X11 green compliance and SVG green compliance).
44 #include "magick/studio.h"
45 #include "magick/blob.h"
46 #include "magick/cache-view.h"
47 #include "magick/cache.h"
48 #include "magick/color.h"
49 #include "magick/color-private.h"
50 #include "magick/colormap.h"
51 #include "magick/client.h"
52 #include "magick/configure.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/gem.h"
56 #include "magick/geometry.h"
57 #include "magick/image-private.h"
58 #include "magick/memory_.h"
59 #include "magick/monitor.h"
60 #include "magick/monitor-private.h"
61 #include "magick/option.h"
62 #include "magick/pixel-private.h"
63 #include "magick/quantize.h"
64 #include "magick/quantum.h"
65 #include "magick/semaphore.h"
66 #include "magick/string_.h"
67 #include "magick/token.h"
68 #include "magick/utility.h"
69 #include "magick/xml-tree.h"
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % A c q u i r e I m a g e C o l o r m a p %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % AcquireImageColormap() allocates an image colormap and initializes
83 % it to a linear gray colorspace. If the image already has a colormap,
84 % it is replaced. AcquireImageColormap() returns MagickTrue if successful,
85 % otherwise MagickFalse if there is not enough memory.
87 % The format of the AcquireImageColormap method is:
89 % MagickBooleanType AcquireImageColormap(Image *image,
90 % const size_t colors)
92 % A description of each parameter follows:
96 % o colors: the number of colors in the image colormap.
100 static inline size_t MagickMax(const size_t x,
108 static inline size_t MagickMin(const size_t x,
116 MagickExport MagickBooleanType AcquireImageColormap(Image *image,
126 Allocate image colormap.
128 assert(image != (Image *) NULL);
129 assert(image->signature == MagickSignature);
130 if (image->debug != MagickFalse)
131 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
132 image->colors=colors;
133 length=(size_t) colors;
134 if (image->colormap == (PixelPacket *) NULL)
135 image->colormap=(PixelPacket *) AcquireQuantumMemory(length,
136 sizeof(*image->colormap));
138 image->colormap=(PixelPacket *) ResizeQuantumMemory(image->colormap,length,
139 sizeof(*image->colormap));
140 if (image->colormap == (PixelPacket *) NULL)
141 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
143 for (i=0; i < (ssize_t) image->colors; i++)
148 pixel=(size_t) (i*(QuantumRange/MagickMax(colors-1,1)));
149 image->colormap[i].red=(Quantum) pixel;
150 image->colormap[i].green=(Quantum) pixel;
151 image->colormap[i].blue=(Quantum) pixel;
152 image->colormap[i].opacity=OpaqueOpacity;
154 return(SetImageStorageClass(image,PseudoClass));
158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 % C y c l e C o l o r m a p I m a g e %
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 % CycleColormap() displaces an image's colormap by a given number of
169 % positions. If you cycle the colormap a number of times you can produce
170 % a psychodelic effect.
172 % The format of the CycleColormapImage method is:
174 % MagickBooleanType CycleColormapImage(Image *image,const ssize_t displace)
176 % A description of each parameter follows:
178 % o image: the image.
180 % o displace: displace the colormap this amount.
183 MagickExport MagickBooleanType CycleColormapImage(Image *image,
184 const ssize_t displace)
198 assert(image != (Image *) NULL);
199 assert(image->signature == MagickSignature);
200 if (image->debug != MagickFalse)
201 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
202 if (image->storage_class == DirectClass)
203 (void) SetImageType(image,PaletteType);
205 exception=(&image->exception);
206 image_view=AcquireCacheView(image);
207 #if defined(MAGICKCORE_OPENMP_SUPPORT)
208 #pragma omp parallel for schedule(dynamic,4) shared(status)
210 for (y=0; y < (ssize_t) image->rows; y++)
224 if (status == MagickFalse)
226 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
227 if (q == (PixelPacket *) NULL)
232 indexes=GetCacheViewAuthenticIndexQueue(image_view);
233 for (x=0; x < (ssize_t) image->columns; x++)
235 index=(ssize_t) (GetIndexPixelComponent(indexes+x)+displace) %
238 index+=(ssize_t) image->colors;
239 SetIndexPixelComponent(indexes+x,index);
240 SetRGBOPixelComponents(q,image->colormap+(ssize_t) index);
243 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
246 image_view=DestroyCacheView(image_view);
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 + S o r t C o l o r m a p B y I n t e n s i t y %
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 % SortColormapByIntensity() sorts the colormap of a PseudoClass image by
262 % decreasing color intensity.
264 % The format of the SortColormapByIntensity method is:
266 % MagickBooleanType SortColormapByIntensity(Image *image)
268 % A description of each parameter follows:
270 % o image: A pointer to an Image structure.
274 #if defined(__cplusplus) || defined(c_plusplus)
278 static int IntensityCompare(const void *x,const void *y)
287 color_1=(const PixelPacket *) x;
288 color_2=(const PixelPacket *) y;
289 intensity=(int) PixelIntensityToQuantum(color_2)-
290 (int) PixelIntensityToQuantum(color_1);
294 #if defined(__cplusplus) || defined(c_plusplus)
298 MagickExport MagickBooleanType SortColormapByIntensity(Image *image)
318 assert(image != (Image *) NULL);
319 if (image->debug != MagickFalse)
320 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
321 assert(image->signature == MagickSignature);
322 if (image->storage_class != PseudoClass)
325 Allocate memory for pixel indexes.
327 pixels=(unsigned short *) AcquireQuantumMemory((size_t) image->colors,
329 if (pixels == (unsigned short *) NULL)
330 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
333 Assign index values to colormap entries.
335 #if defined(MAGICKCORE_OPENMP_SUPPORT)
336 #pragma omp parallel for schedule(dynamic,4) shared(status)
338 for (i=0; i < (ssize_t) image->colors; i++)
339 image->colormap[i].opacity=(IndexPacket) i;
341 Sort image colormap by decreasing color popularity.
343 qsort((void *) image->colormap,(size_t) image->colors,
344 sizeof(*image->colormap),IntensityCompare);
346 Update image colormap indexes to sorted colormap order.
348 #if defined(MAGICKCORE_OPENMP_SUPPORT)
349 #pragma omp parallel for schedule(dynamic,4) shared(status)
351 for (i=0; i < (ssize_t) image->colors; i++)
352 pixels[(ssize_t) image->colormap[i].opacity]=(unsigned short) i;
354 exception=(&image->exception);
355 image_view=AcquireCacheView(image);
356 for (y=0; y < (ssize_t) image->rows; y++)
370 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
371 if (q == (PixelPacket *) NULL)
376 indexes=GetCacheViewAuthenticIndexQueue(image_view);
377 for (x=0; x < (ssize_t) image->columns; x++)
379 index=(IndexPacket) pixels[(ssize_t) GetIndexPixelComponent(indexes+x)];
380 SetIndexPixelComponent(indexes+x,index);
381 SetRGBOPixelComponents(q,image->colormap+(ssize_t) index);
384 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
386 if (status == MagickFalse)
389 image_view=DestroyCacheView(image_view);
390 pixels=(unsigned short *) RelinquishMagickMemory(pixels);