colormap.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %            CCCC   OOO   L       OOO   RRRR   M   M   AAA   PPPP             %
00007 %           C      O   O  L      O   O  R   R  MM MM  A   A  P   P            %
00008 %           C      O   O  L      O   O  RRRR   M M M  AAAAA  PPPP             %
00009 %           C      O   O  L      O   O  R R    M   M  A   A  P                %
00010 %            CCCC   OOO   LLLLL   OOO   R  R   M   M  A   A  P                %
00011 %                                                                             %
00012 %                                                                             %
00013 %                        MagickCore Colormap Methods                          %
00014 %                                                                             %
00015 %                              Software Design                                %
00016 %                                John Cristy                                  %
00017 %                                 July 1992                                   %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.imagemagick.org/script/license.php                            %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %  We use linked-lists because splay-trees do not currently support duplicate
00037 %  key / value pairs (.e.g X11 green compliance and SVG green compliance).
00038 %
00039 */
00040 
00041 /*
00042   Include declarations.
00043 */
00044 #include "magick/studio.h"
00045 #include "magick/blob.h"
00046 #include "magick/cache-view.h"
00047 #include "magick/cache.h"
00048 #include "magick/color.h"
00049 #include "magick/color-private.h"
00050 #include "magick/colormap.h"
00051 #include "magick/client.h"
00052 #include "magick/configure.h"
00053 #include "magick/exception.h"
00054 #include "magick/exception-private.h"
00055 #include "magick/gem.h"
00056 #include "magick/geometry.h"
00057 #include "magick/image-private.h"
00058 #include "magick/memory_.h"
00059 #include "magick/monitor.h"
00060 #include "magick/monitor-private.h"
00061 #include "magick/option.h"
00062 #include "magick/pixel-private.h"
00063 #include "magick/quantize.h"
00064 #include "magick/quantum.h"
00065 #include "magick/semaphore.h"
00066 #include "magick/string_.h"
00067 #include "magick/token.h"
00068 #include "magick/utility.h"
00069 #include "magick/xml-tree.h"
00070 
00071 /*
00072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00073 %                                                                             %
00074 %                                                                             %
00075 %                                                                             %
00076 %     C y c l e C o l o r m a p I m a g e                                     %
00077 %                                                                             %
00078 %                                                                             %
00079 %                                                                             %
00080 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00081 %
00082 %  CycleColormap() displaces an image's colormap by a given number of
00083 %  positions.  If you cycle the colormap a number of times you can produce
00084 %  a psychodelic effect.
00085 %
00086 %  The format of the CycleColormapImage method is:
00087 %
00088 %      MagickBooleanType CycleColormapImage(Image *image,const long displace)
00089 %
00090 %  A description of each parameter follows:
00091 %
00092 %    o image: the image.
00093 %
00094 %    o displace:  displace the colormap this amount.
00095 %
00096 */
00097 MagickExport MagickBooleanType CycleColormapImage(Image *image,
00098   const long displace)
00099 {
00100   CacheView
00101     *image_view;
00102 
00103   ExceptionInfo
00104     *exception;
00105 
00106   long
00107     y;
00108 
00109   MagickBooleanType
00110     status;
00111 
00112   assert(image != (Image *) NULL);
00113   assert(image->signature == MagickSignature);
00114   if (image->debug != MagickFalse)
00115     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
00116   if (image->storage_class == DirectClass)
00117     (void) SetImageType(image,PaletteType);
00118   status=MagickTrue;
00119   exception=(&image->exception);
00120   image_view=AcquireCacheView(image);
00121 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00122   #pragma omp parallel for schedule(dynamic,4) shared(status)
00123 #endif
00124   for (y=0; y < (long) image->rows; y++)
00125   {
00126     long
00127       index;
00128 
00129     register IndexPacket
00130       *__restrict indexes;
00131 
00132     register long
00133       x;
00134 
00135     register PixelPacket
00136       *__restrict q;
00137 
00138     if (status == MagickFalse)
00139       continue;
00140     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
00141     if (q == (PixelPacket *) NULL)
00142       {
00143         status=MagickFalse;
00144         continue;
00145       }
00146     indexes=GetCacheViewAuthenticIndexQueue(image_view);
00147     for (x=0; x < (long) image->columns; x++)
00148     {
00149       index=(long) (indexes[x]+displace) % image->colors;
00150       if (index < 0)
00151         index+=image->colors;
00152       indexes[x]=(IndexPacket) index;
00153       q->red=image->colormap[index].red;
00154       q->green=image->colormap[index].green;
00155       q->blue=image->colormap[index].blue;
00156       q++;
00157     }
00158     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
00159       status=MagickFalse;
00160   }
00161   image_view=DestroyCacheView(image_view);
00162   return(status);
00163 }
00164 
00165 /*
00166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00167 %                                                                             %
00168 %                                                                             %
00169 %                                                                             %
00170 +   S o r t C o l o r m a p B y I n t e n s i t y                             %
00171 %                                                                             %
00172 %                                                                             %
00173 %                                                                             %
00174 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00175 %
00176 %  SortColormapByIntensity() sorts the colormap of a PseudoClass image by
00177 %  decreasing color intensity.
00178 %
00179 %  The format of the SortColormapByIntensity method is:
00180 %
00181 %      MagickBooleanType SortColormapByIntensity(Image *image)
00182 %
00183 %  A description of each parameter follows:
00184 %
00185 %    o image: A pointer to an Image structure.
00186 %
00187 */
00188 
00189 #if defined(__cplusplus) || defined(c_plusplus)
00190 extern "C" {
00191 #endif
00192 
00193 static int IntensityCompare(const void *x,const void *y)
00194 {
00195   const PixelPacket
00196     *color_1,
00197     *color_2;
00198 
00199   int
00200     intensity;
00201 
00202   color_1=(const PixelPacket *) x;
00203   color_2=(const PixelPacket *) y;
00204   intensity=(int) PixelIntensityToQuantum(color_2)-
00205     (int) PixelIntensityToQuantum(color_1);
00206   return(intensity);
00207 }
00208 
00209 #if defined(__cplusplus) || defined(c_plusplus)
00210 }
00211 #endif
00212 
00213 MagickExport MagickBooleanType SortColormapByIntensity(Image *image)
00214 {
00215   CacheView
00216     *image_view;
00217 
00218   ExceptionInfo
00219     *exception;
00220 
00221   long
00222     y;
00223 
00224   MagickBooleanType
00225     status;
00226 
00227   register long
00228     i;
00229 
00230   unsigned short
00231     *pixels;
00232 
00233   assert(image != (Image *) NULL);
00234   if (image->debug != MagickFalse)
00235     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00236   assert(image->signature == MagickSignature);
00237   if (image->storage_class != PseudoClass)
00238     return(MagickTrue);
00239   /*
00240     Allocate memory for pixel indexes.
00241   */
00242   pixels=(unsigned short *) AcquireQuantumMemory((size_t) image->colors,
00243     sizeof(*pixels));
00244   if (pixels == (unsigned short *) NULL)
00245     ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
00246       image->filename);
00247   /*
00248     Assign index values to colormap entries.
00249   */
00250 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00251   #pragma omp parallel for schedule(dynamic,4) shared(status)
00252 #endif
00253   for (i=0; i < (long) image->colors; i++)
00254     image->colormap[i].opacity=(IndexPacket) i;
00255   /*
00256     Sort image colormap by decreasing color popularity.
00257   */
00258   qsort((void *) image->colormap,(size_t) image->colors,
00259     sizeof(*image->colormap),IntensityCompare);
00260   /*
00261     Update image colormap indexes to sorted colormap order.
00262   */
00263 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00264   #pragma omp parallel for schedule(dynamic,4) shared(status)
00265 #endif
00266   for (i=0; i < (long) image->colors; i++)
00267     pixels[(long) image->colormap[i].opacity]=(unsigned short) i;
00268   status=MagickTrue;
00269   exception=(&image->exception);
00270   image_view=AcquireCacheView(image);
00271   for (y=0; y < (long) image->rows; y++)
00272   {
00273     IndexPacket
00274       index;
00275 
00276     register long
00277       x;
00278 
00279     register IndexPacket
00280       *__restrict indexes;
00281 
00282     register PixelPacket
00283       *__restrict q;
00284 
00285     q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
00286     if (q == (PixelPacket *) NULL)
00287       {
00288         status=MagickFalse;
00289         continue;
00290       }
00291     indexes=GetCacheViewAuthenticIndexQueue(image_view);
00292     for (x=0; x < (long) image->columns; x++)
00293     {
00294       index=(IndexPacket) pixels[(long) indexes[x]];
00295       indexes[x]=index;
00296       *q++=image->colormap[(long) index];
00297     }
00298     if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
00299       status=MagickFalse;
00300     if (status == MagickFalse)
00301       break;
00302   }
00303   image_view=DestroyCacheView(image_view);
00304   pixels=(unsigned short *) RelinquishMagickMemory(pixels);
00305   return(status);
00306 }

Generated on 19 Nov 2009 for MagickCore by  doxygen 1.6.1