44 #include "magick/studio.h"
45 #include "magick/attribute.h"
46 #include "magick/blob.h"
47 #include "magick/cache-view.h"
48 #include "magick/cache.h"
49 #include "magick/color.h"
50 #include "magick/color-private.h"
51 #include "magick/colormap.h"
52 #include "magick/colormap-private.h"
53 #include "magick/client.h"
54 #include "magick/configure.h"
55 #include "magick/exception.h"
56 #include "magick/exception-private.h"
57 #include "magick/gem.h"
58 #include "magick/geometry.h"
59 #include "magick/image-private.h"
60 #include "magick/memory_.h"
61 #include "magick/monitor.h"
62 #include "magick/monitor-private.h"
63 #include "magick/option.h"
64 #include "magick/pixel-accessor.h"
65 #include "magick/pixel-private.h"
66 #include "magick/quantize.h"
67 #include "magick/quantum.h"
68 #include "magick/semaphore.h"
69 #include "magick/resource_.h"
70 #include "magick/string_.h"
71 #include "magick/thread-private.h"
72 #include "magick/token.h"
73 #include "magick/utility.h"
74 #include "magick/xml-tree.h"
103 MagickExport MagickBooleanType AcquireImageColormap(
Image *image,
112 assert(image != (
Image *) NULL);
113 assert(image->signature == MagickCoreSignature);
114 if (IsEventLogging() != MagickFalse)
115 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
116 if (colors > MaxColormapSize)
119 image->storage_class=DirectClass;
120 ThrowBinaryImageException(ResourceLimitError,
"MemoryAllocationFailed",
123 image->colors=MagickMax(colors,1);
125 image->colormap=(
PixelPacket *) AcquireQuantumMemory(image->colors+256,
126 sizeof(*image->colormap));
128 image->colormap=(
PixelPacket *) ResizeQuantumMemory(image->colormap,
129 image->colors+256,
sizeof(*image->colormap));
133 image->storage_class=DirectClass;
134 ThrowBinaryImageException(ResourceLimitError,
"MemoryAllocationFailed",
137 for (i=0; i < (ssize_t) image->colors; i++)
142 pixel=(size_t) (i*(QuantumRange/MagickMax(colors-1,1)));
143 image->colormap[i].red=(Quantum) pixel;
144 image->colormap[i].green=(Quantum) pixel;
145 image->colormap[i].blue=(Quantum) pixel;
146 image->colormap[i].opacity=OpaqueOpacity;
148 return(SetImageStorageClass(image,PseudoClass));
177 MagickExport MagickBooleanType CycleColormapImage(
Image *image,
178 const ssize_t displace)
192 assert(image != (
Image *) NULL);
193 assert(image->signature == MagickCoreSignature);
194 if (IsEventLogging() != MagickFalse)
195 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
196 if (image->storage_class == DirectClass)
197 (void) SetImageType(image,PaletteType);
199 exception=(&image->exception);
200 image_view=AcquireAuthenticCacheView(image,exception);
201 #if defined(MAGICKCORE_OPENMP_SUPPORT)
202 #pragma omp parallel for schedule(static) shared(status) \
203 magick_number_threads(image,image,image->rows,1)
205 for (y=0; y < (ssize_t) image->rows; y++)
208 *magick_restrict indexes;
219 if (status == MagickFalse)
221 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
227 indexes=GetCacheViewAuthenticIndexQueue(image_view);
228 for (x=0; x < (ssize_t) image->columns; x++)
230 index=(ssize_t) (GetPixelIndex(indexes+x)+displace) %
233 index+=(ssize_t) image->colors;
234 SetPixelIndex(indexes+x,index);
235 SetPixelRGBO(q,image->colormap+(ssize_t) index);
238 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
241 image_view=DestroyCacheView(image_view);
269 #if defined(__cplusplus) || defined(c_plusplus)
273 static int IntensityCompare(
const void *x,
const void *y)
284 intensity=PixelPacketIntensity(color_2)-(int) PixelPacketIntensity(color_1);
288 #if defined(__cplusplus) || defined(c_plusplus)
292 MagickExport MagickBooleanType SortColormapByIntensity(
Image *image)
312 assert(image != (
Image *) NULL);
313 assert(image->signature == MagickCoreSignature);
314 if (IsEventLogging() != MagickFalse)
315 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
316 if (image->storage_class != PseudoClass)
318 exception=(&image->exception);
322 pixels=(
unsigned short *) AcquireQuantumMemory((
size_t) image->colors,
324 if (pixels == (
unsigned short *) NULL)
325 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
330 for (i=0; i < (ssize_t) image->colors; i++)
331 image->colormap[i].opacity=(IndexPacket) i;
335 qsort((
void *) image->colormap,(
size_t) image->colors,
336 sizeof(*image->colormap),IntensityCompare);
340 for (i=0; i < (ssize_t) image->colors; i++)
341 pixels[(ssize_t) image->colormap[i].opacity]=(
unsigned short) i;
343 image_view=AcquireAuthenticCacheView(image,exception);
344 for (y=0; y < (ssize_t) image->rows; y++)
353 *magick_restrict indexes;
358 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
364 indexes=GetCacheViewAuthenticIndexQueue(image_view);
365 for (x=0; x < (ssize_t) image->columns; x++)
367 i=ConstrainColormapIndex(image,GetPixelIndex(indexes+x));
368 index=(IndexPacket) pixels[i];
369 SetPixelIndex(indexes+x,index);
370 SetPixelRGBO(q,image->colormap+(ssize_t) index);
373 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
375 if (status == MagickFalse)
378 image_view=DestroyCacheView(image_view);
379 pixels=(
unsigned short *) RelinquishMagickMemory(pixels);