44#include "MagickCore/studio.h"
45#include "MagickCore/attribute.h"
46#include "MagickCore/blob.h"
47#include "MagickCore/cache-view.h"
48#include "MagickCore/cache.h"
49#include "MagickCore/color.h"
50#include "MagickCore/color-private.h"
51#include "MagickCore/colormap.h"
52#include "MagickCore/colormap-private.h"
53#include "MagickCore/client.h"
54#include "MagickCore/configure.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/gem.h"
58#include "MagickCore/geometry.h"
59#include "MagickCore/image-private.h"
60#include "MagickCore/memory_.h"
61#include "MagickCore/monitor.h"
62#include "MagickCore/monitor-private.h"
63#include "MagickCore/option.h"
64#include "MagickCore/pixel-accessor.h"
65#include "MagickCore/quantize.h"
66#include "MagickCore/quantum.h"
67#include "MagickCore/resource_.h"
68#include "MagickCore/semaphore.h"
69#include "MagickCore/string_.h"
70#include "MagickCore/thread-private.h"
71#include "MagickCore/token.h"
72#include "MagickCore/utility.h"
73#include "MagickCore/xml-tree.h"
105MagickExport MagickBooleanType AcquireImageColormap(
Image *image,
114 assert(image != (
Image *) NULL);
115 assert(image->signature == MagickCoreSignature);
116 if (IsEventLogging() != MagickFalse)
117 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
118 if (colors > MaxColormapSize)
121 image->storage_class=DirectClass;
122 ThrowBinaryException(ResourceLimitError,
"UnableToCreateColormap",
125 image->colors=MagickMax(colors,1);
126 if (image->colormap == (
PixelInfo *) NULL)
127 image->colormap=(
PixelInfo *) AcquireQuantumMemory(image->colors+1,
128 sizeof(*image->colormap));
130 image->colormap=(
PixelInfo *) ResizeQuantumMemory(image->colormap,
131 image->colors+1,
sizeof(*image->colormap));
132 if (image->colormap == (
PixelInfo *) NULL)
135 image->storage_class=DirectClass;
136 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
139 for (i=0; i < (ssize_t) image->colors; i++)
144 GetPixelInfo(image,image->colormap+i);
145 pixel=((double) i*(QuantumRange/MagickMax(colors-1,1)));
146 image->colormap[i].red=pixel;
147 image->colormap[i].green=pixel;
148 image->colormap[i].blue=pixel;
149 image->colormap[i].alpha=(MagickRealType) OpaqueAlpha;
150 image->colormap[i].alpha_trait=BlendPixelTrait;
152 image->storage_class=PseudoClass;
188MagickExport MagickBooleanType CycleColormapImage(
Image *image,
200 assert(image != (
Image *) NULL);
201 assert(image->signature == MagickCoreSignature);
202 if (IsEventLogging() != MagickFalse)
203 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
204 if (image->storage_class == DirectClass)
205 (void) SetImageType(image,PaletteType,exception);
207 image_view=AcquireAuthenticCacheView(image,exception);
208#if defined(MAGICKCORE_OPENMP_SUPPORT)
209 #pragma omp parallel for schedule(static) \
210 magick_number_threads(image,image,image->rows,2)
212 for (y=0; y < (ssize_t) image->rows; y++)
223 if (status == MagickFalse)
225 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
226 if (q == (Quantum *) NULL)
231 for (x=0; x < (ssize_t) image->columns; x++)
233 index=(ssize_t) (GetPixelIndex(image,q)+displace) % (ssize_t)
236 index+=(ssize_t) image->colors;
237 SetPixelIndex(image,(Quantum) index,q);
238 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
239 q+=(ptrdiff_t) GetPixelChannels(image);
241 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
244 image_view=DestroyCacheView(image_view);
275#if defined(__cplusplus) || defined(c_plusplus)
279static int IntensityCompare(
const void *x,
const void *y)
290 intensity=(int) GetPixelInfoIntensity((
const Image *) NULL,color_2)-(int)
291 GetPixelInfoIntensity((
const Image *) NULL,color_1);
295#if defined(__cplusplus) || defined(c_plusplus)
299MagickExport MagickBooleanType SortColormapByIntensity(
Image *image,
317 assert(image != (
Image *) NULL);
318 assert(image->signature == MagickCoreSignature);
319 if (IsEventLogging() != MagickFalse)
320 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
321 if (image->storage_class != PseudoClass)
326 pixels=(
unsigned short *) AcquireQuantumMemory((
size_t) image->colors,
328 if (pixels == (
unsigned short *) NULL)
329 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
334 for (j=0; j < (ssize_t) image->colors; j++)
335 image->colormap[j].alpha=(
double) j;
339 qsort((
void *) image->colormap,(
size_t) image->colors,
340 sizeof(*image->colormap),IntensityCompare);
344 for (j=0; j < (ssize_t) image->colors; j++)
345 pixels[(ssize_t) image->colormap[j].alpha]=(
unsigned short) j;
347 image_view=AcquireAuthenticCacheView(image,exception);
348#if defined(MAGICKCORE_OPENMP_SUPPORT)
349 #pragma omp parallel for schedule(static) \
350 magick_number_threads(image,image,image->rows,2)
352 for (y=0; y < (ssize_t) image->rows; y++)
360 if (status == MagickFalse)
362 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
363 if (q == (Quantum *) NULL)
368 for (x=0; x < (ssize_t) image->columns; x++)
376 i=ConstrainColormapIndex(image,GetPixelIndex(image,q),exception);
377 index=(Quantum) pixels[i];
378 SetPixelIndex(image,index,q);
379 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
380 q+=(ptrdiff_t) GetPixelChannels(image);
382 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
385 image_view=DestroyCacheView(image_view);
386 pixels=(
unsigned short *) RelinquishMagickMemory(pixels);