]> 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)
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 */
99
100 static inline size_t MagickMax(const size_t x,
101   const size_t y)
102 {
103   if (x > y)
104     return(x);
105   return(y);
106 }
107
108 static inline size_t MagickMin(const size_t x,
109   const size_t y)
110 {
111   if (x < y)
112     return(x);
113   return(y);
114 }
115
116 MagickExport MagickBooleanType AcquireImageColormap(Image *image,
117   const size_t colors)
118 {
119   register ssize_t
120     i;
121
122   size_t
123     length;
124
125   /*
126     Allocate image colormap.
127   */
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));
137   else
138     image->colormap=(PixelPacket *) ResizeQuantumMemory(image->colormap,length,
139       sizeof(*image->colormap));
140   if (image->colormap == (PixelPacket *) NULL)
141     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
142       image->filename);
143   for (i=0; i < (ssize_t) image->colors; i++)
144   {
145     size_t
146       pixel;
147
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].alpha=OpaqueAlpha;
153   }
154   return(SetImageStorageClass(image,PseudoClass));
155 }
156 \f
157 /*
158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159 %                                                                             %
160 %                                                                             %
161 %                                                                             %
162 %     C y c l e C o l o r m a p I m a g e                                     %
163 %                                                                             %
164 %                                                                             %
165 %                                                                             %
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 %
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.
171 %
172 %  The format of the CycleColormapImage method is:
173 %
174 %      MagickBooleanType CycleColormapImage(Image *image,const ssize_t displace)
175 %
176 %  A description of each parameter follows:
177 %
178 %    o image: the image.
179 %
180 %    o displace:  displace the colormap this amount.
181 %
182 */
183 MagickExport MagickBooleanType CycleColormapImage(Image *image,
184   const ssize_t displace)
185 {
186   CacheView
187     *image_view;
188
189   ExceptionInfo
190     *exception;
191
192   MagickBooleanType
193     status;
194
195   ssize_t
196     y;
197
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);
204   status=MagickTrue;
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)
209 #endif
210   for (y=0; y < (ssize_t) image->rows; y++)
211   {
212     register ssize_t
213       x;
214
215     register Quantum
216       *restrict q;
217
218     ssize_t
219       index;
220
221     if (status == MagickFalse)
222       continue;
223     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
224     if (q == (const Quantum *) NULL)
225       {
226         status=MagickFalse;
227         continue;
228       }
229     for (x=0; x < (ssize_t) image->columns; x++)
230     {
231       index=(ssize_t) (GetPixelIndex(image,q)+displace) % image->colors;
232       if (index < 0)
233         index+=(ssize_t) image->colors;
234       SetPixelIndex(image,(Quantum) index,q);
235       SetPixelPacket(image,image->colormap+(ssize_t) index,q);
236       q+=GetPixelChannels(image);
237     }
238     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
239       status=MagickFalse;
240   }
241   image_view=DestroyCacheView(image_view);
242   return(status);
243 }
244 \f
245 /*
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 %                                                                             %
248 %                                                                             %
249 %                                                                             %
250 +   S o r t C o l o r m a p B y I n t e n s i t y                             %
251 %                                                                             %
252 %                                                                             %
253 %                                                                             %
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 %
256 %  SortColormapByIntensity() sorts the colormap of a PseudoClass image by
257 %  decreasing color intensity.
258 %
259 %  The format of the SortColormapByIntensity method is:
260 %
261 %      MagickBooleanType SortColormapByIntensity(Image *image)
262 %
263 %  A description of each parameter follows:
264 %
265 %    o image: A pointer to an Image structure.
266 %
267 */
268
269 #if defined(__cplusplus) || defined(c_plusplus)
270 extern "C" {
271 #endif
272
273 static int IntensityCompare(const void *x,const void *y)
274 {
275   const PixelPacket
276     *color_1,
277     *color_2;
278
279   int
280     intensity;
281
282   color_1=(const PixelPacket *) x;
283   color_2=(const PixelPacket *) y;
284   intensity=(int) GetPixelPacketIntensity(color_2)-(int)
285     GetPixelPacketIntensity(color_1);
286   return(intensity);
287 }
288
289 #if defined(__cplusplus) || defined(c_plusplus)
290 }
291 #endif
292
293 MagickExport MagickBooleanType SortColormapByIntensity(Image *image)
294 {
295   CacheView
296     *image_view;
297
298   ExceptionInfo
299     *exception;
300
301   MagickBooleanType
302     status;
303
304   register ssize_t
305     i;
306
307   ssize_t
308     y;
309
310   unsigned short
311     *pixels;
312
313   assert(image != (Image *) NULL);
314   if (image->debug != MagickFalse)
315     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
316   assert(image->signature == MagickSignature);
317   if (image->storage_class != PseudoClass)
318     return(MagickTrue);
319   /*
320     Allocate memory for pixel indexes.
321   */
322   pixels=(unsigned short *) AcquireQuantumMemory((size_t) image->colors,
323     sizeof(*pixels));
324   if (pixels == (unsigned short *) NULL)
325     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
326       image->filename);
327   /*
328     Assign index values to colormap entries.
329   */
330 #if defined(MAGICKCORE_OPENMP_SUPPORT)
331   #pragma omp parallel for schedule(dynamic,4) shared(status)
332 #endif
333   for (i=0; i < (ssize_t) image->colors; i++)
334     image->colormap[i].alpha=(Quantum) i;
335   /*
336     Sort image colormap by decreasing color popularity.
337   */
338   qsort((void *) image->colormap,(size_t) image->colors,
339     sizeof(*image->colormap),IntensityCompare);
340   /*
341     Update image colormap indexes to sorted colormap order.
342   */
343 #if defined(MAGICKCORE_OPENMP_SUPPORT)
344   #pragma omp parallel for schedule(dynamic,4) shared(status)
345 #endif
346   for (i=0; i < (ssize_t) image->colors; i++)
347     pixels[(ssize_t) image->colormap[i].alpha]=(unsigned short) i;
348   status=MagickTrue;
349   exception=(&image->exception);
350   image_view=AcquireCacheView(image);
351   for (y=0; y < (ssize_t) image->rows; y++)
352   {
353     Quantum
354       index;
355
356     register ssize_t
357       x;
358
359     register Quantum
360       *restrict q;
361
362     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
363     if (q == (const Quantum *) NULL)
364       {
365         status=MagickFalse;
366         continue;
367       }
368     for (x=0; x < (ssize_t) image->columns; x++)
369     {
370       index=(Quantum) pixels[(ssize_t) GetPixelIndex(image,q)];
371       SetPixelIndex(image,index,q);
372       SetPixelPacket(image,image->colormap+(ssize_t) index,q);
373       q+=GetPixelChannels(image);
374     }
375     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
376       status=MagickFalse;
377     if (status == MagickFalse)
378       break;
379   }
380   image_view=DestroyCacheView(image_view);
381   pixels=(unsigned short *) RelinquishMagickMemory(pixels);
382   return(status);
383 }