blob: 43218dc5163370c9d06fc2ddec5088341e32c568 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% IIIII N N L IIIII N N EEEEE %
7% I NN N L I NN N E %
8% I N N N L I N N N EEE %
9% I N NN L I N NN E %
10% IIIII N N LLLLL IIIII N N EEEEE %
11% %
12% %
13% Read Inline Images %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/client.h"
46#include "magick/display.h"
47#include "magick/exception.h"
48#include "magick/exception-private.h"
49#include "magick/image.h"
50#include "magick/image-private.h"
51#include "magick/list.h"
52#include "magick/magick.h"
53#include "magick/memory_.h"
54#include "magick/option.h"
55#include "magick/quantum-private.h"
56#include "magick/static.h"
57#include "magick/string_.h"
58#include "magick/module.h"
59#include "magick/utility.h"
60#include "magick/xwindow.h"
61#include "magick/xwindow-private.h"
62
63/*
64%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65% %
66% %
67% %
68% R e a d I N L I N E I m a g e %
69% %
70% %
71% %
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73%
74% ReadINLINEImage() reads base64-encoded inlines images.
75%
76% The format of the ReadINLINEImage method is:
77%
78% Image *ReadINLINEImage(const ImageInfo *image_info,
79% ExceptionInfo *exception)
80%
81% A description of each parameter follows:
82%
83% o image_info: the image info.
84%
85% o exception: return any errors or warnings in this structure.
86%
87*/
88
89static inline size_t MagickMin(const size_t x,const size_t y)
90{
91 if (x < y)
92 return(x);
93 return(y);
94}
95
96static Image *ReadINLINEImage(const ImageInfo *image_info,
97 ExceptionInfo *exception)
98{
99 Image
100 *image;
101
102 MagickBooleanType
103 status;
104
105 register size_t
106 i;
107
108 ssize_t
109 count;
110
111 size_t
112 quantum;
113
114 unsigned char
115 *inline_image;
116
117 /*
118 Open image file.
119 */
120 assert(image_info != (const ImageInfo *) NULL);
121 assert(image_info->signature == MagickSignature);
122 if (image_info->debug != MagickFalse)
123 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
124 image_info->filename);
125 assert(exception != (ExceptionInfo *) NULL);
126 assert(exception->signature == MagickSignature);
127 if (LocaleNCompare(image_info->filename,"data:",5) == 0)
128 return(ReadInlineImage(image_info,image_info->filename,exception));
129 image=AcquireImage(image_info);
130 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
131 if (status == MagickFalse)
132 {
133 image=DestroyImageList(image);
134 return((Image *) NULL);
135 }
136 quantum=MagickMin((size_t) GetBlobSize(image),MagickMaxBufferExtent);
137 inline_image=(unsigned char *) AcquireQuantumMemory(quantum,
138 sizeof(*inline_image));
139 for (i=0; inline_image != (unsigned char *) NULL; i+=count)
140 {
141 count=(ssize_t) ReadBlob(image,quantum,inline_image+i);
142 if (count <= 0)
143 {
144 count=0;
145 if (errno != EINTR)
146 break;
147 }
148 if (~(1UL*i) < (quantum+1))
149 {
150 inline_image=(unsigned char *) RelinquishMagickMemory(inline_image);
151 break;
152 }
153 inline_image=(unsigned char *) ResizeQuantumMemory(inline_image,i+quantum+1,
154 sizeof(*inline_image));
155 }
156 if (inline_image == (unsigned char *) NULL)
157 {
158 (void) ThrowMagickException(exception,GetMagickModule(),
159 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
160 return((Image *) NULL);
161 }
162 inline_image[i+count]='\0';
163 image=DestroyImageList(image);
164 image=ReadInlineImage(image_info,(char *) inline_image,exception);
165 inline_image=(unsigned char *) RelinquishMagickMemory(inline_image);
166 return(image);
167}
168
169/*
170%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
171% %
172% %
173% %
174% R e g i s t e r I N L I N E I m a g e %
175% %
176% %
177% %
178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179%
180% RegisterINLINEImage() adds attributes for the INLINE image format to
181% the list of supported formats. The attributes include the image format
182% tag, a method to read and/or write the format, whether the format
183% supports the saving of more than one frame to the same file or blob,
184% whether the format supports native in-memory I/O, and a brief
185% description of the format.
186%
187% The format of the RegisterINLINEImage method is:
188%
189% unsigned long RegisterINLINEImage(void)
190%
191*/
192ModuleExport unsigned long RegisterINLINEImage(void)
193{
194 MagickInfo
195 *entry;
196
197 entry=SetMagickInfo("INLINE");
198 entry->decoder=(DecodeImageHandler *) ReadINLINEImage;
199 entry->format_type=ExplicitFormatType;
200 entry->description=ConstantString("Base64-encoded inline images");
201 entry->module=ConstantString("INLINE");
202 (void) RegisterMagickInfo(entry);
203 return(MagickImageCoderSignature);
204}
205
206/*
207%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208% %
209% %
210% %
211% U n r e g i s t e r I N L I N E I m a g e %
212% %
213% %
214% %
215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216%
217% UnregisterINLINEImage() removes format registrations made by the
218% INLINE module from the list of supported formats.
219%
220% The format of the UnregisterINLINEImage method is:
221%
222% UnregisterINLINEImage(void)
223%
224*/
225ModuleExport void UnregisterINLINEImage(void)
226{
227 (void) UnregisterMagickInfo("INLINE");
228}