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