]> granicus.if.org Git - imagemagick/blob - coders/thumbnail.c
38d093643a9d050968971dd25ef004f377e14d48
[imagemagick] / coders / thumbnail.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %       TTTTT  H   H  U   U  M   M  BBBB   N   N   AAA   IIIII  L             %
7 %         T    H   H  U   U  MM MM  B   B  NN  N  A   A    I    L             %
8 %         T    HHHHH  U   U  M M M  BBBB   N N N  AAAAA    I    L             %
9 %         T    H   H  U   U  M   M  B   B  N  NN  A   A    I    L             %
10 %         T    H   H   UUU   M   M  BBBB   N   N  A   A  IIIII  LLLLL         %
11 %                                                                             %
12 %                                                                             %
13 %                        Write EXIF Thumbnail To File.                        %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 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 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/constitute.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/magick.h"
50 #include "MagickCore/memory_.h"
51 #include "MagickCore/module.h"
52 #include "MagickCore/monitor.h"
53 #include "MagickCore/monitor-private.h"
54 #include "MagickCore/profile.h"
55 #include "MagickCore/property.h"
56 #include "MagickCore/quantum-private.h"
57 #include "MagickCore/static.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/string-private.h"
60 \f
61 /*
62   Forward declarations.
63 */
64 static MagickBooleanType
65   WriteTHUMBNAILImage(const ImageInfo *,Image *,ExceptionInfo *);
66 \f
67 /*
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69 %                                                                             %
70 %                                                                             %
71 %                                                                             %
72 %   R e g i s t e r T H U M B N A I L I m a g e                               %
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %
78 %  RegisterTHUMBNAILImage() adds attributes for the THUMBNAIL image format to
79 %  the list of supported formats.  The attributes include the image format
80 %  tag, a method to read and/or write the format, whether the format
81 %  supports the saving of more than one frame to the same file or blob,
82 %  whether the format supports native in-memory I/O, and a brief
83 %  description of the format.
84 %
85 %  The format of the RegisterTHUMBNAILImage method is:
86 %
87 %      size_t RegisterTHUMBNAILImage(void)
88 %
89 */
90 ModuleExport size_t RegisterTHUMBNAILImage(void)
91 {
92   MagickInfo
93     *entry;
94
95   entry=SetMagickInfo("THUMBNAIL");
96   entry->encoder=(EncodeImageHandler *) WriteTHUMBNAILImage;
97   entry->description=ConstantString("EXIF Profile Thumbnail");
98   entry->module=ConstantString("THUMBNAIL");
99   (void) RegisterMagickInfo(entry);
100   return(MagickImageCoderSignature);
101 }
102 \f
103 /*
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %                                                                             %
106 %                                                                             %
107 %                                                                             %
108 %   U n r e g i s t e r T H U M B N A I L I m a g e                           %
109 %                                                                             %
110 %                                                                             %
111 %                                                                             %
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 %
114 %  UnregisterTHUMBNAILImage() removes format registrations made by the
115 %  THUMBNAIL module from the list of supported formats.
116 %
117 %  The format of the UnregisterTHUMBNAILImage method is:
118 %
119 %      UnregisterTHUMBNAILImage(void)
120 %
121 */
122 ModuleExport void UnregisterTHUMBNAILImage(void)
123 {
124   (void) UnregisterMagickInfo("THUMBNAIL");
125 }
126 \f
127 /*
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 %                                                                             %
130 %                                                                             %
131 %                                                                             %
132 %   W r i t e T H U M B N A I L I m a g e                                     %
133 %                                                                             %
134 %                                                                             %
135 %                                                                             %
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 %
138 %  WriteTHUMBNAILImage() extracts the EXIF thumbnail image and writes it.
139 %
140 %  The format of the WriteTHUMBNAILImage method is:
141 %
142 %      MagickBooleanType WriteTHUMBNAILImage(const ImageInfo *image_info,
143 %        Image *image,ExceptionInfo *exception)
144 %
145 %  A description of each parameter follows.
146 %
147 %    o image_info: the image info.
148 %
149 %    o image:  The image.
150 %
151 %    o exception: return any errors or warnings in this structure.
152 %
153 */
154 static MagickBooleanType WriteTHUMBNAILImage(const ImageInfo *image_info,
155   Image *image,ExceptionInfo *exception)
156 {
157   const char
158     *property;
159
160   const StringInfo
161     *profile;
162
163   Image
164     *thumbnail_image;
165
166   ImageInfo
167     *write_info;
168
169   MagickBooleanType
170     status;
171
172   register ssize_t
173     i;
174
175   size_t
176     length;
177
178   ssize_t
179     offset;
180
181   unsigned char
182     magick[MaxTextExtent];
183
184   profile=GetImageProfile(image,"exif");
185   if (profile == (const StringInfo *) NULL)
186     ThrowWriterException(CoderError,"ImageDoesNotHaveAThumbnail");
187   property=GetImageProperty(image,"exif:JPEGInterchangeFormat",exception);
188   if (property == (const char *) NULL)
189     ThrowWriterException(CoderError,"ImageDoesNotHaveAThumbnail");
190   offset=(ssize_t) StringToLong(property);
191   property=GetImageProperty(image,"exif:JPEGInterchangeFormatLength",exception);
192   if (property == (const char *) NULL)
193     ThrowWriterException(CoderError,"ImageDoesNotHaveAThumbnail");
194   length=(size_t) StringToLong(property);
195   (void) ResetMagickMemory(magick,0,sizeof(magick));
196   for (i=0; i < (ssize_t) length; i++)
197   {
198     magick[0]=magick[1];
199     magick[1]=magick[2];
200     magick[2]=GetStringInfoDatum(profile)[offset+i];
201     if (memcmp(magick,"\377\330\377",3) == 0)
202       break;
203   }
204   thumbnail_image=BlobToImage(image_info,GetStringInfoDatum(profile)+offset+i-2,
205     length,exception);
206   if (thumbnail_image == (Image *) NULL)
207     return(MagickFalse);
208   (void) SetImageType(thumbnail_image,thumbnail_image->alpha_trait != BlendPixelTrait ?
209     TrueColorType : TrueColorMatteType,exception);
210   (void) CopyMagickString(thumbnail_image->filename,image->filename,
211     MaxTextExtent);
212   write_info=CloneImageInfo(image_info);
213   (void) SetImageInfo(write_info,1,exception);
214   if (LocaleCompare(write_info->magick,"THUMBNAIL") == 0)
215     (void) FormatLocaleString(thumbnail_image->filename,MaxTextExtent,
216       "miff:%s",write_info->filename);
217   status=WriteImage(write_info,thumbnail_image,exception);
218   thumbnail_image=DestroyImage(thumbnail_image);
219   write_info=DestroyImageInfo(write_info);
220   return(status);
221 }