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