]> granicus.if.org Git - imagemagick/blob - magick/colormap.c
2c879454e220e5d105b7df17149ac02534c63847
[imagemagick] / magick / 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-2010 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 "magick/studio.h"
45 #include "magick/blob.h"
46 #include "magick/cache-view.h"
47 #include "magick/cache.h"
48 #include "magick/color.h"
49 #include "magick/color-private.h"
50 #include "magick/colormap.h"
51 #include "magick/client.h"
52 #include "magick/configure.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/gem.h"
56 #include "magick/geometry.h"
57 #include "magick/image-private.h"
58 #include "magick/memory_.h"
59 #include "magick/monitor.h"
60 #include "magick/monitor-private.h"
61 #include "magick/option.h"
62 #include "magick/pixel-private.h"
63 #include "magick/quantize.h"
64 #include "magick/quantum.h"
65 #include "magick/semaphore.h"
66 #include "magick/string_.h"
67 #include "magick/token.h"
68 #include "magick/utility.h"
69 #include "magick/xml-tree.h"
70 \f
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %     C y c l e C o l o r m a p I m a g e                                     %
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
82 %  CycleColormap() displaces an image's colormap by a given number of
83 %  positions.  If you cycle the colormap a number of times you can produce
84 %  a psychodelic effect.
85 %
86 %  The format of the CycleColormapImage method is:
87 %
88 %      MagickBooleanType CycleColormapImage(Image *image,const long displace)
89 %
90 %  A description of each parameter follows:
91 %
92 %    o image: the image.
93 %
94 %    o displace:  displace the colormap this amount.
95 %
96 */
97 MagickExport MagickBooleanType CycleColormapImage(Image *image,
98   const long displace)
99 {
100   CacheView
101     *image_view;
102
103   ExceptionInfo
104     *exception;
105
106   long
107     y;
108
109   MagickBooleanType
110     status;
111
112   assert(image != (Image *) NULL);
113   assert(image->signature == MagickSignature);
114   if (image->debug != MagickFalse)
115     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
116   if (image->storage_class == DirectClass)
117     (void) SetImageType(image,PaletteType);
118   status=MagickTrue;
119   exception=(&image->exception);
120   image_view=AcquireCacheView(image);
121 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP > 202001)
122   #pragma omp parallel for schedule(dynamic,4) shared(status)
123 #endif
124   for (y=0; y < (long) image->rows; y++)
125   {
126     long
127       index;
128
129     register IndexPacket
130       *restrict indexes;
131
132     register long
133       x;
134
135     register PixelPacket
136       *restrict q;
137
138     if (status == MagickFalse)
139       continue;
140     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
141     if (q == (PixelPacket *) NULL)
142       {
143         status=MagickFalse;
144         continue;
145       }
146     indexes=GetCacheViewAuthenticIndexQueue(image_view);
147     for (x=0; x < (long) image->columns; x++)
148     {
149       index=(long) (indexes[x]+displace) % image->colors;
150       if (index < 0)
151         index+=image->colors;
152       indexes[x]=(IndexPacket) index;
153       q->red=image->colormap[index].red;
154       q->green=image->colormap[index].green;
155       q->blue=image->colormap[index].blue;
156       q++;
157     }
158     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
159       status=MagickFalse;
160   }
161   image_view=DestroyCacheView(image_view);
162   return(status);
163 }
164 \f
165 /*
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167 %                                                                             %
168 %                                                                             %
169 %                                                                             %
170 +   S o r t C o l o r m a p B y I n t e n s i t y                             %
171 %                                                                             %
172 %                                                                             %
173 %                                                                             %
174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 %
176 %  SortColormapByIntensity() sorts the colormap of a PseudoClass image by
177 %  decreasing color intensity.
178 %
179 %  The format of the SortColormapByIntensity method is:
180 %
181 %      MagickBooleanType SortColormapByIntensity(Image *image)
182 %
183 %  A description of each parameter follows:
184 %
185 %    o image: A pointer to an Image structure.
186 %
187 */
188
189 #if defined(__cplusplus) || defined(c_plusplus)
190 extern "C" {
191 #endif
192
193 static int IntensityCompare(const void *x,const void *y)
194 {
195   const PixelPacket
196     *color_1,
197     *color_2;
198
199   int
200     intensity;
201
202   color_1=(const PixelPacket *) x;
203   color_2=(const PixelPacket *) y;
204   intensity=(int) PixelIntensityToQuantum(color_2)-
205     (int) PixelIntensityToQuantum(color_1);
206   return(intensity);
207 }
208
209 #if defined(__cplusplus) || defined(c_plusplus)
210 }
211 #endif
212
213 MagickExport MagickBooleanType SortColormapByIntensity(Image *image)
214 {
215   CacheView
216     *image_view;
217
218   ExceptionInfo
219     *exception;
220
221   long
222     y;
223
224   MagickBooleanType
225     status;
226
227   register long
228     i;
229
230   unsigned short
231     *pixels;
232
233   assert(image != (Image *) NULL);
234   if (image->debug != MagickFalse)
235     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
236   assert(image->signature == MagickSignature);
237   if (image->storage_class != PseudoClass)
238     return(MagickTrue);
239   /*
240     Allocate memory for pixel indexes.
241   */
242   pixels=(unsigned short *) AcquireQuantumMemory((size_t) image->colors,
243     sizeof(*pixels));
244   if (pixels == (unsigned short *) NULL)
245     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
246       image->filename);
247   /*
248     Assign index values to colormap entries.
249   */
250 #if defined(MAGICKCORE_OPENMP_SUPPORT)
251   #pragma omp parallel for schedule(dynamic,4) shared(status)
252 #endif
253   for (i=0; i < (long) image->colors; i++)
254     image->colormap[i].opacity=(IndexPacket) i;
255   /*
256     Sort image colormap by decreasing color popularity.
257   */
258   qsort((void *) image->colormap,(size_t) image->colors,
259     sizeof(*image->colormap),IntensityCompare);
260   /*
261     Update image colormap indexes to sorted colormap order.
262   */
263 #if defined(MAGICKCORE_OPENMP_SUPPORT)
264   #pragma omp parallel for schedule(dynamic,4) shared(status)
265 #endif
266   for (i=0; i < (long) image->colors; i++)
267     pixels[(long) image->colormap[i].opacity]=(unsigned short) i;
268   status=MagickTrue;
269   exception=(&image->exception);
270   image_view=AcquireCacheView(image);
271   for (y=0; y < (long) image->rows; y++)
272   {
273     IndexPacket
274       index;
275
276     register long
277       x;
278
279     register IndexPacket
280       *restrict indexes;
281
282     register PixelPacket
283       *restrict q;
284
285     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
286     if (q == (PixelPacket *) NULL)
287       {
288         status=MagickFalse;
289         continue;
290       }
291     indexes=GetCacheViewAuthenticIndexQueue(image_view);
292     for (x=0; x < (long) image->columns; x++)
293     {
294       index=(IndexPacket) pixels[(long) indexes[x]];
295       indexes[x]=index;
296       *q++=image->colormap[(long) index];
297     }
298     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
299       status=MagickFalse;
300     if (status == MagickFalse)
301       break;
302   }
303   image_view=DestroyCacheView(image_view);
304   pixels=(unsigned short *) RelinquishMagickMemory(pixels);
305   return(status);
306 }