]> granicus.if.org Git - imagemagick/blob - MagickCore/colormap.c
(no commit message)
[imagemagick] / MagickCore / colormap.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
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                %
11 %                                                                             %
12 %                                                                             %
13 %                        MagickCore Colormap Methods                          %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
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).
38 %
39 */
40 \f
41 /*
42   Include declarations.
43 */
44 #include "MagickCore/studio.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/cache-view.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color.h"
49 #include "MagickCore/color-private.h"
50 #include "MagickCore/colormap.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/configure.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/gem.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image-private.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/option.h"
62 #include "MagickCore/pixel-accessor.h"
63 #include "MagickCore/quantize.h"
64 #include "MagickCore/quantum.h"
65 #include "MagickCore/semaphore.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/token.h"
68 #include "MagickCore/utility.h"
69 #include "MagickCore/xml-tree.h"
70 \f
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %   A c q u i r e I m a g e C o l o r m a p                                   %
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
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.
86 %
87 %  The format of the AcquireImageColormap method is:
88 %
89 %      MagickBooleanType AcquireImageColormap(Image *image,
90 %        const size_t colors,ExceptionInfo *exception)
91 %
92 %  A description of each parameter follows:
93 %
94 %    o image: the image.
95 %
96 %    o colors: the number of colors in the image colormap.
97 %
98 %    o exception: return any errors or warnings in this structure.
99 %
100 */
101
102 static inline size_t MagickMax(const size_t x,
103   const size_t y)
104 {
105   if (x > y)
106     return(x);
107   return(y);
108 }
109
110 static inline size_t MagickMin(const size_t x,
111   const size_t y)
112 {
113   if (x < y)
114     return(x);
115   return(y);
116 }
117
118 MagickExport MagickBooleanType AcquireImageColormap(Image *image,
119   const size_t colors,ExceptionInfo *exception)
120 {
121   register ssize_t
122     i;
123
124   size_t
125     length;
126
127   /*
128     Allocate image colormap.
129   */
130   assert(image != (Image *) NULL);
131   assert(image->signature == MagickSignature);
132   if (image->debug != MagickFalse)
133     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
134   image->colors=colors;
135   length=(size_t) colors;
136   if (image->colormap == (PixelPacket *) NULL)
137     image->colormap=(PixelPacket *) AcquireQuantumMemory(length,
138       sizeof(*image->colormap));
139   else
140     image->colormap=(PixelPacket *) ResizeQuantumMemory(image->colormap,length,
141       sizeof(*image->colormap));
142   if (image->colormap == (PixelPacket *) NULL)
143     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
144       image->filename);
145   for (i=0; i < (ssize_t) image->colors; i++)
146   {
147     size_t
148       pixel;
149
150     pixel=(size_t) (i*(QuantumRange/MagickMax(colors-1,1)));
151     image->colormap[i].red=(Quantum) pixel;
152     image->colormap[i].green=(Quantum) pixel;
153     image->colormap[i].blue=(Quantum) pixel;
154     image->colormap[i].alpha=OpaqueAlpha;
155   }
156   return(SetImageStorageClass(image,PseudoClass,&image->exception));
157 }
158 \f
159 /*
160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 %                                                                             %
162 %                                                                             %
163 %                                                                             %
164 %     C y c l e C o l o r m a p I m a g e                                     %
165 %                                                                             %
166 %                                                                             %
167 %                                                                             %
168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 %
170 %  CycleColormap() displaces an image's colormap by a given number of
171 %  positions.  If you cycle the colormap a number of times you can produce
172 %  a psychodelic effect.
173 %
174 %  The format of the CycleColormapImage method is:
175 %
176 %      MagickBooleanType CycleColormapImage(Image *image,const ssize_t displace,
177 %        ExceptionInfo *exception)
178 %
179 %  A description of each parameter follows:
180 %
181 %    o image: the image.
182 %
183 %    o displace:  displace the colormap this amount.
184 %
185 %    o exception: return any errors or warnings in this structure.
186 %
187 */
188 MagickExport MagickBooleanType CycleColormapImage(Image *image,
189   const ssize_t displace,ExceptionInfo *exception)
190 {
191   CacheView
192     *image_view;
193
194   MagickBooleanType
195     status;
196
197   ssize_t
198     y;
199
200   assert(image != (Image *) NULL);
201   assert(image->signature == MagickSignature);
202   if (image->debug != MagickFalse)
203     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
204   if (image->storage_class == DirectClass)
205     (void) SetImageType(image,PaletteType,exception);
206   status=MagickTrue;
207   image_view=AcquireCacheView(image);
208 #if defined(MAGICKCORE_OPENMP_SUPPORT) 
209   #pragma omp parallel for schedule(dynamic,4) shared(status)
210 #endif
211   for (y=0; y < (ssize_t) image->rows; y++)
212   {
213     register ssize_t
214       x;
215
216     register Quantum
217       *restrict q;
218
219     ssize_t
220       index;
221
222     if (status == MagickFalse)
223       continue;
224     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
225     if (q == (Quantum *) NULL)
226       {
227         status=MagickFalse;
228         continue;
229       }
230     for (x=0; x < (ssize_t) image->columns; x++)
231     {
232       index=(ssize_t) (GetPixelIndex(image,q)+displace) % image->colors;
233       if (index < 0)
234         index+=(ssize_t) image->colors;
235       SetPixelIndex(image,(Quantum) index,q);
236       SetPixelPacket(image,image->colormap+(ssize_t) index,q);
237       q+=GetPixelChannels(image);
238     }
239     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
240       status=MagickFalse;
241   }
242   image_view=DestroyCacheView(image_view);
243   return(status);
244 }
245 \f
246 /*
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 %                                                                             %
249 %                                                                             %
250 %                                                                             %
251 +   S o r t C o l o r m a p B y I n t e n s i t y                             %
252 %                                                                             %
253 %                                                                             %
254 %                                                                             %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 %
257 %  SortColormapByIntensity() sorts the colormap of a PseudoClass image by
258 %  decreasing color intensity.
259 %
260 %  The format of the SortColormapByIntensity method is:
261 %
262 %      MagickBooleanType SortColormapByIntensity(Image *image,
263 %        ExceptionInfo *exception)
264 %
265 %  A description of each parameter follows:
266 %
267 %    o image: A pointer to an Image structure.
268 %
269 %    o exception: return any errors or warnings in this structure.
270 %
271 */
272
273 #if defined(__cplusplus) || defined(c_plusplus)
274 extern "C" {
275 #endif
276
277 static int IntensityCompare(const void *x,const void *y)
278 {
279   const PixelPacket
280     *color_1,
281     *color_2;
282
283   int
284     intensity;
285
286   color_1=(const PixelPacket *) x;
287   color_2=(const PixelPacket *) y;
288   intensity=(int) GetPixelPacketIntensity(color_2)-(int)
289     GetPixelPacketIntensity(color_1);
290   return(intensity);
291 }
292
293 #if defined(__cplusplus) || defined(c_plusplus)
294 }
295 #endif
296
297 MagickExport MagickBooleanType SortColormapByIntensity(Image *image,
298   ExceptionInfo *exception)
299 {
300   CacheView
301     *image_view;
302
303   MagickBooleanType
304     status;
305
306   register ssize_t
307     i;
308
309   ssize_t
310     y;
311
312   unsigned short
313     *pixels;
314
315   assert(image != (Image *) NULL);
316   if (image->debug != MagickFalse)
317     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
318   assert(image->signature == MagickSignature);
319   if (image->storage_class != PseudoClass)
320     return(MagickTrue);
321   /*
322     Allocate memory for pixel indexes.
323   */
324   pixels=(unsigned short *) AcquireQuantumMemory((size_t) image->colors,
325     sizeof(*pixels));
326   if (pixels == (unsigned short *) NULL)
327     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
328       image->filename);
329   /*
330     Assign index values to colormap entries.
331   */
332 #if defined(MAGICKCORE_OPENMP_SUPPORT)
333   #pragma omp parallel for schedule(dynamic,4) shared(status)
334 #endif
335   for (i=0; i < (ssize_t) image->colors; i++)
336     image->colormap[i].alpha=(Quantum) i;
337   /*
338     Sort image colormap by decreasing color popularity.
339   */
340   qsort((void *) image->colormap,(size_t) image->colors,
341     sizeof(*image->colormap),IntensityCompare);
342   /*
343     Update image colormap indexes to sorted colormap order.
344   */
345 #if defined(MAGICKCORE_OPENMP_SUPPORT)
346   #pragma omp parallel for schedule(dynamic,4) shared(status)
347 #endif
348   for (i=0; i < (ssize_t) image->colors; i++)
349     pixels[(ssize_t) image->colormap[i].alpha]=(unsigned short) i;
350   status=MagickTrue;
351   image_view=AcquireCacheView(image);
352   for (y=0; y < (ssize_t) image->rows; y++)
353   {
354     Quantum
355       index;
356
357     register ssize_t
358       x;
359
360     register Quantum
361       *restrict q;
362
363     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
364     if (q == (Quantum *) NULL)
365       {
366         status=MagickFalse;
367         continue;
368       }
369     for (x=0; x < (ssize_t) image->columns; x++)
370     {
371       index=(Quantum) pixels[(ssize_t) GetPixelIndex(image,q)];
372       SetPixelIndex(image,index,q);
373       SetPixelPacket(image,image->colormap+(ssize_t) index,q);
374       q+=GetPixelChannels(image);
375     }
376     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
377       status=MagickFalse;
378     if (status == MagickFalse)
379       break;
380   }
381   image_view=DestroyCacheView(image_view);
382   pixels=(unsigned short *) RelinquishMagickMemory(pixels);
383   return(status);
384 }