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"
103MagickExport 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,
"UnableToCreateColormap",
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 image->storage_class=PseudoClass;
178MagickExport MagickBooleanType CycleColormapImage(
Image *image,
179 const ssize_t displace)
193 assert(image != (
Image *) NULL);
194 assert(image->signature == MagickCoreSignature);
195 if (IsEventLogging() != MagickFalse)
196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
197 if (image->storage_class == DirectClass)
198 (void) SetImageType(image,PaletteType);
200 exception=(&image->exception);
201 image_view=AcquireAuthenticCacheView(image,exception);
202#if defined(MAGICKCORE_OPENMP_SUPPORT)
203 #pragma omp parallel for schedule(static) shared(status) \
204 magick_number_threads(image,image,image->rows,2)
206 for (y=0; y < (ssize_t) image->rows; y++)
209 *magick_restrict indexes;
220 if (status == MagickFalse)
222 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
228 indexes=GetCacheViewAuthenticIndexQueue(image_view);
229 for (x=0; x < (ssize_t) image->columns; x++)
231 index=(ssize_t) (GetPixelIndex(indexes+x)+displace) %
234 index+=(ssize_t) image->colors;
235 SetPixelIndex(indexes+x,index);
236 SetPixelRGBO(q,image->colormap+(ssize_t) index);
239 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
242 image_view=DestroyCacheView(image_view);
270#if defined(__cplusplus) || defined(c_plusplus)
274static int IntensityCompare(
const void *x,
const void *y)
285 intensity=PixelPacketIntensity(color_2)-(int) PixelPacketIntensity(color_1);
289#if defined(__cplusplus) || defined(c_plusplus)
293MagickExport MagickBooleanType SortColormapByIntensity(
Image *image)
311 assert(image != (
Image *) NULL);
312 assert(image->signature == MagickCoreSignature);
313 if (IsEventLogging() != MagickFalse)
314 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
315 if (image->storage_class != PseudoClass)
317 exception=(&image->exception);
321 pixels=(
unsigned short *) AcquireQuantumMemory((
size_t) image->colors,
323 if (pixels == (
unsigned short *) NULL)
324 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
329 for (i=0; i < (ssize_t) image->colors; i++)
330 image->colormap[i].opacity=(IndexPacket) i;
334 qsort((
void *) image->colormap,(
size_t) image->colors,
335 sizeof(*image->colormap),IntensityCompare);
339 for (i=0; i < (ssize_t) image->colors; i++)
340 pixels[(ssize_t) image->colormap[i].opacity]=(
unsigned short) i;
342 image_view=AcquireAuthenticCacheView(image,exception);
343#if defined(MAGICKCORE_OPENMP_SUPPORT)
344 #pragma omp parallel for schedule(static) shared(status) \
345 magick_number_threads(image,image,image->rows,2)
347 for (y=0; y < (ssize_t) image->rows; y++)
350 *magick_restrict indexes;
358 if (status == MagickFalse)
360 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
366 indexes=GetCacheViewAuthenticIndexQueue(image_view);
367 for (x=0; x < (ssize_t) image->columns; x++)
375 i=ConstrainColormapIndex(image,GetPixelIndex(indexes+x));
376 index=(IndexPacket) pixels[i];
377 SetPixelIndex(indexes+x,index);
378 SetPixelRGBO(q,image->colormap+(ssize_t) index);
381 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
384 image_view=DestroyCacheView(image_view);
385 pixels=(
unsigned short *) RelinquishMagickMemory(pixels);