]> 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/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/client.h"
53 #include "MagickCore/configure.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/gem.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/quantize.h"
65 #include "MagickCore/quantum.h"
66 #include "MagickCore/resource_.h"
67 #include "MagickCore/semaphore.h"
68 #include "MagickCore/string_.h"
69 #include "MagickCore/thread-private.h"
70 #include "MagickCore/token.h"
71 #include "MagickCore/utility.h"
72 #include "MagickCore/xml-tree.h"
73 \f
74 /*
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %   A c q u i r e I m a g e C o l o r m a p                                   %
80 %                                                                             %
81 %                                                                             %
82 %                                                                             %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %
85 %  AcquireImageColormap() allocates an image colormap and initializes
86 %  it to a linear gray colorspace.  If the image already has a colormap,
87 %  it is replaced.  AcquireImageColormap() returns MagickTrue if successful,
88 %  otherwise MagickFalse if there is not enough memory.
89 %
90 %  The format of the AcquireImageColormap method is:
91 %
92 %      MagickBooleanType AcquireImageColormap(Image *image,
93 %        const size_t colors,ExceptionInfo *exception)
94 %
95 %  A description of each parameter follows:
96 %
97 %    o image: the image.
98 %
99 %    o colors: the number of colors in the image colormap.
100 %
101 %    o exception: return any errors or warnings in this structure.
102 %
103 */
104
105 static inline size_t MagickMax(const size_t x,
106   const size_t y)
107 {
108   if (x > y)
109     return(x);
110   return(y);
111 }
112
113 MagickExport MagickBooleanType AcquireImageColormap(Image *image,
114   const size_t colors,ExceptionInfo *exception)
115 {
116   register ssize_t
117     i;
118
119   size_t
120     length;
121
122   /*
123     Allocate image colormap.
124   */
125   assert(image != (Image *) NULL);
126   assert(image->signature == MagickSignature);
127   if (image->debug != MagickFalse)
128     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
129   image->colors=colors;
130   length=(size_t) colors;
131   if (image->colormap == (PixelInfo *) NULL)
132     image->colormap=(PixelInfo *) AcquireQuantumMemory(length,
133       sizeof(*image->colormap));
134   else
135     image->colormap=(PixelInfo *) ResizeQuantumMemory(image->colormap,length,
136       sizeof(*image->colormap));
137   if (image->colormap == (PixelInfo *) NULL)
138     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
139       image->filename);
140   for (i=0; i < (ssize_t) image->colors; i++)
141   {
142     double
143       pixel;
144
145     pixel=(double) (i*(QuantumRange/MagickMax(colors-1,1)));
146     GetPixelInfo(image,image->colormap+i);
147     image->colormap[i].matte=MagickTrue;
148     image->colormap[i].red=pixel;
149     image->colormap[i].green=pixel;
150     image->colormap[i].blue=pixel;
151     image->colormap[i].alpha=OpaqueAlpha;
152   }
153   return(SetImageStorageClass(image,PseudoClass,exception));
154 }
155 \f
156 /*
157 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 %                                                                             %
159 %                                                                             %
160 %                                                                             %
161 %     C y c l e C o l o r m a p I m a g e                                     %
162 %                                                                             %
163 %                                                                             %
164 %                                                                             %
165 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 %
167 %  CycleColormap() displaces an image's colormap by a given number of
168 %  positions.  If you cycle the colormap a number of times you can produce
169 %  a psychodelic effect.
170 %
171 %  WARNING: this assumes an images colormap is in a well know and defined
172 %  order. Currently Imagemagick has no way of setting that order.
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=AcquireAuthenticCacheView(image,exception);
208 #if defined(MAGICKCORE_OPENMP_SUPPORT)
209   #pragma omp parallel for schedule(static,4) shared(status) \
210     dynamic_number_threads(image,image->columns,image->rows,1)
211 #endif
212   for (y=0; y < (ssize_t) image->rows; y++)
213   {
214     register ssize_t
215       x;
216
217     register Quantum
218       *restrict q;
219
220     ssize_t
221       index;
222
223     if (status == MagickFalse)
224       continue;
225     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
226     if (q == (Quantum *) NULL)
227       {
228         status=MagickFalse;
229         continue;
230       }
231     for (x=0; x < (ssize_t) image->columns; x++)
232     {
233       index=(ssize_t) (GetPixelIndex(image,q)+displace) % image->colors;
234       if (index < 0)
235         index+=(ssize_t) image->colors;
236       SetPixelIndex(image,(Quantum) index,q);
237       SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
238       q+=GetPixelChannels(image);
239     }
240     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
241       status=MagickFalse;
242   }
243   image_view=DestroyCacheView(image_view);
244   return(status);
245 }
246 \f
247 /*
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 %                                                                             %
250 %                                                                             %
251 %                                                                             %
252 +   S o r t C o l o r m a p B y I n t e n s i t y                             %
253 %                                                                             %
254 %                                                                             %
255 %                                                                             %
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 %
258 %  SortColormapByIntensity() sorts the colormap of a PseudoClass image by
259 %  decreasing color intensity.
260 %
261 %  The format of the SortColormapByIntensity method is:
262 %
263 %      MagickBooleanType SortColormapByIntensity(Image *image,
264 %        ExceptionInfo *exception)
265 %
266 %  A description of each parameter follows:
267 %
268 %    o image: A pointer to an Image structure.
269 %
270 %    o exception: return any errors or warnings in this structure.
271 %
272 */
273
274 #if defined(__cplusplus) || defined(c_plusplus)
275 extern "C" {
276 #endif
277
278 static int IntensityCompare(const void *x,const void *y)
279 {
280   const PixelInfo
281     *color_1,
282     *color_2;
283
284   int
285     intensity;
286
287   color_1=(const PixelInfo *) x;
288   color_2=(const PixelInfo *) y;
289   intensity=(int) GetPixelInfoIntensity(color_2)-(int)
290     GetPixelInfoIntensity(color_1);
291   return(intensity);
292 }
293
294 #if defined(__cplusplus) || defined(c_plusplus)
295 }
296 #endif
297
298 MagickExport MagickBooleanType SortColormapByIntensity(Image *image,
299   ExceptionInfo *exception)
300 {
301   CacheView
302     *image_view;
303
304   MagickBooleanType
305     status;
306
307   register ssize_t
308     i;
309
310   ssize_t
311     y;
312
313   unsigned short
314     *pixels;
315
316   assert(image != (Image *) NULL);
317   if (image->debug != MagickFalse)
318     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
319   assert(image->signature == MagickSignature);
320   if (image->storage_class != PseudoClass)
321     return(MagickTrue);
322   /*
323     Allocate memory for pixel indexes.
324   */
325   pixels=(unsigned short *) AcquireQuantumMemory((size_t) image->colors,
326     sizeof(*pixels));
327   if (pixels == (unsigned short *) NULL)
328     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
329       image->filename);
330   /*
331     Assign index values to colormap entries.
332   */
333 #if defined(MAGICKCORE_OPENMP_SUPPORT)
334   #pragma omp parallel for schedule(static,4) shared(status) \
335     dynamic_number_threads(image,image->columns,1,1)
336 #endif
337   for (i=0; i < (ssize_t) image->colors; i++)
338     image->colormap[i].alpha=(double) i;
339   /*
340     Sort image colormap by decreasing color popularity.
341   */
342   qsort((void *) image->colormap,(size_t) image->colors,
343     sizeof(*image->colormap),IntensityCompare);
344   /*
345     Update image colormap indexes to sorted colormap order.
346   */
347 #if defined(MAGICKCORE_OPENMP_SUPPORT)
348   #pragma omp parallel for schedule(static,4) shared(status)
349 #endif
350   for (i=0; i < (ssize_t) image->colors; i++)
351     pixels[(ssize_t) image->colormap[i].alpha]=(unsigned short) i;
352   status=MagickTrue;
353   image_view=AcquireAuthenticCacheView(image,exception);
354   for (y=0; y < (ssize_t) image->rows; y++)
355   {
356     Quantum
357       index;
358
359     register ssize_t
360       x;
361
362     register Quantum
363       *restrict q;
364
365     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
366     if (q == (Quantum *) NULL)
367       {
368         status=MagickFalse;
369         break;
370       }
371     for (x=0; x < (ssize_t) image->columns; x++)
372     {
373       index=(Quantum) pixels[(ssize_t) GetPixelIndex(image,q)];
374       SetPixelIndex(image,index,q);
375       SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
376       q+=GetPixelChannels(image);
377     }
378     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
379       status=MagickFalse;
380     if (status == MagickFalse)
381       break;
382   }
383   image_view=DestroyCacheView(image_view);
384   pixels=(unsigned short *) RelinquishMagickMemory(pixels);
385   return(status);
386 }