cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % SSSSS TTTTT EEEEE GGGG AAA N N OOO % |
| 7 | % SS T E G A A NN N O O % |
| 8 | % SSS T EEE G GG AAAAA N N N O O % |
| 9 | % SS T E G G A A N NN O O % |
| 10 | % SSSSS T EEEEE GGG A A N N OOO % |
| 11 | % % |
| 12 | % % |
| 13 | % Write A Steganographic Image. % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | 16af1cb | 2009-12-11 21:38:29 +0000 | [diff] [blame] | 20 | % Copyright 1999-2010 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 | */ |
| 42 | #include "magick/studio.h" |
| 43 | #include "magick/blob.h" |
| 44 | #include "magick/blob-private.h" |
| 45 | #include "magick/cache.h" |
cristy | e20ffe5 | 2010-04-24 23:51:41 +0000 | [diff] [blame] | 46 | #include "magick/colormap.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 47 | #include "magick/constitute.h" |
| 48 | #include "magick/exception.h" |
| 49 | #include "magick/exception-private.h" |
| 50 | #include "magick/image.h" |
| 51 | #include "magick/image-private.h" |
| 52 | #include "magick/list.h" |
| 53 | #include "magick/magick.h" |
| 54 | #include "magick/memory_.h" |
| 55 | #include "magick/monitor.h" |
| 56 | #include "magick/monitor-private.h" |
| 57 | #include "magick/quantum-private.h" |
| 58 | #include "magick/static.h" |
| 59 | #include "magick/string_.h" |
| 60 | #include "magick/module.h" |
| 61 | |
| 62 | /* |
| 63 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 64 | % % |
| 65 | % % |
| 66 | % % |
| 67 | % R e a d S T E G A N O I m a g e % |
| 68 | % % |
| 69 | % % |
| 70 | % % |
| 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 | % |
| 73 | % ReadSTEGANOImage() reads a steganographic image hidden within another |
| 74 | % image type. It allocates the memory necessary for the new Image structure |
| 75 | % and returns a pointer to the new image. |
| 76 | % |
| 77 | % The format of the ReadSTEGANOImage method is: |
| 78 | % |
| 79 | % Image *ReadSTEGANOImage(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 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 90 | static inline size_t MagickMin(const size_t x, |
| 91 | const size_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 92 | { |
| 93 | if (x < y) |
| 94 | return(x); |
| 95 | return(y); |
| 96 | } |
| 97 | |
| 98 | static Image *ReadSTEGANOImage(const ImageInfo *image_info, |
| 99 | ExceptionInfo *exception) |
| 100 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 101 | #define GetBit(alpha,i) MagickMin((((size_t) (alpha) >> (size_t) \ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 102 | (i)) & 0x01),16) |
| 103 | #define SetBit(alpha,i,set) (alpha)=(IndexPacket) ((set) != 0 ? \ |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 104 | (size_t) (alpha) | (one << (size_t) (i)) : (size_t) \ |
| 105 | (alpha) & ~(one << (size_t) (i))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 106 | |
| 107 | Image |
| 108 | *image, |
| 109 | *watermark; |
| 110 | |
| 111 | ImageInfo |
| 112 | *read_info; |
| 113 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 114 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 115 | c, |
| 116 | i, |
| 117 | j, |
| 118 | k, |
| 119 | y; |
| 120 | |
| 121 | MagickBooleanType |
| 122 | status; |
| 123 | |
| 124 | PixelPacket |
| 125 | pixel; |
| 126 | |
| 127 | register IndexPacket |
| 128 | *indexes; |
| 129 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 130 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 131 | x; |
| 132 | |
| 133 | register PixelPacket |
| 134 | *q; |
| 135 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 136 | size_t |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 137 | depth, |
| 138 | one; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | Initialize Image structure. |
| 142 | */ |
| 143 | assert(image_info != (const ImageInfo *) NULL); |
| 144 | assert(image_info->signature == MagickSignature); |
| 145 | if (image_info->debug != MagickFalse) |
| 146 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 147 | image_info->filename); |
| 148 | assert(exception != (ExceptionInfo *) NULL); |
| 149 | assert(exception->signature == MagickSignature); |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 150 | one=1; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 151 | image=AcquireImage(image_info); |
| 152 | if ((image->columns == 0) || (image->rows == 0)) |
| 153 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
| 154 | read_info=CloneImageInfo(image_info); |
| 155 | SetImageInfoBlob(read_info,(void *) NULL,0); |
| 156 | *read_info->magick='\0'; |
| 157 | watermark=ReadImage(read_info,exception); |
| 158 | read_info=DestroyImageInfo(read_info); |
| 159 | if (watermark == (Image *) NULL) |
| 160 | return((Image *) NULL); |
| 161 | watermark->depth=MAGICKCORE_QUANTUM_DEPTH; |
| 162 | if (AcquireImageColormap(image,MaxColormapSize) == MagickFalse) |
| 163 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 164 | if (image_info->ping != MagickFalse) |
| 165 | { |
| 166 | (void) CloseBlob(image); |
| 167 | return(GetFirstImageInList(image)); |
| 168 | } |
| 169 | /* |
| 170 | Get hidden watermark from low-order bits of image. |
| 171 | */ |
| 172 | c=0; |
| 173 | i=0; |
| 174 | j=0; |
| 175 | i=MAGICKCORE_QUANTUM_DEPTH-1; |
| 176 | depth=MAGICKCORE_QUANTUM_DEPTH; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 177 | for (k=image->offset; (i >= 0) && (j < (ssize_t) depth); i--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 178 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 179 | for (y=0; (y < (ssize_t) image->rows) && (j < (ssize_t) depth); y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 180 | { |
| 181 | x=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 182 | for (; (x < (ssize_t) image->columns) && (j < (ssize_t) depth); x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 183 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 184 | if ((k/(ssize_t) watermark->columns) >= (ssize_t) watermark->rows) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 185 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 186 | (void) GetOneVirtualPixel(watermark,k % (ssize_t) watermark->columns, |
| 187 | k/(ssize_t) watermark->columns,&pixel,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 188 | q=GetAuthenticPixels(image,x,y,1,1,exception); |
| 189 | if (q == (PixelPacket *) NULL) |
| 190 | break; |
| 191 | indexes=GetAuthenticIndexQueue(image); |
| 192 | switch (c) |
| 193 | { |
| 194 | case 0: |
| 195 | { |
| 196 | SetBit(*indexes,i,GetBit(pixel.red,j)); |
| 197 | break; |
| 198 | } |
| 199 | case 1: |
| 200 | { |
| 201 | SetBit(*indexes,i,GetBit(pixel.green,j)); |
| 202 | break; |
| 203 | } |
| 204 | case 2: |
| 205 | { |
| 206 | SetBit(*indexes,i,GetBit(pixel.blue,j)); |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 211 | break; |
| 212 | c++; |
| 213 | if (c == 3) |
| 214 | c=0; |
| 215 | k++; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 216 | if (k == (ssize_t) (watermark->columns*watermark->columns)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | k=0; |
| 218 | if (k == image->offset) |
| 219 | j++; |
| 220 | } |
| 221 | } |
| 222 | status=SetImageProgress(image,LoadImagesTag,i,depth); |
| 223 | if (status == MagickFalse) |
| 224 | break; |
| 225 | } |
| 226 | watermark=DestroyImage(watermark); |
| 227 | (void) SyncImage(image); |
| 228 | return(GetFirstImageInList(image)); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 233 | % % |
| 234 | % % |
| 235 | % % |
| 236 | % R e g i s t e r S T E G A N O I m a g e % |
| 237 | % % |
| 238 | % % |
| 239 | % % |
| 240 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 241 | % |
| 242 | % RegisterSTEGANOImage() adds attributes for the STEGANO image format to |
| 243 | % the list of supported formats. The attributes include the image format |
| 244 | % tag, a method to read and/or write the format, whether the format |
| 245 | % supports the saving of more than one frame to the same file or blob, |
| 246 | % whether the format supports native in-memory I/O, and a brief |
| 247 | % description of the format. |
| 248 | % |
| 249 | % The format of the RegisterSTEGANOImage method is: |
| 250 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 251 | % size_t RegisterSTEGANOImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 252 | % |
| 253 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 254 | ModuleExport size_t RegisterSTEGANOImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 255 | { |
| 256 | MagickInfo |
| 257 | *entry; |
| 258 | |
| 259 | entry=SetMagickInfo("STEGANO"); |
| 260 | entry->decoder=(DecodeImageHandler *) ReadSTEGANOImage; |
cristy | 009d739 | 2010-07-25 22:08:41 +0000 | [diff] [blame] | 261 | entry->format_type=ImplicitFormatType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 262 | entry->description=ConstantString("Steganographic image"); |
| 263 | entry->module=ConstantString("STEGANO"); |
| 264 | (void) RegisterMagickInfo(entry); |
| 265 | return(MagickImageCoderSignature); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 270 | % % |
| 271 | % % |
| 272 | % % |
| 273 | % U n r e g i s t e r S T E G A N O I m a g e % |
| 274 | % % |
| 275 | % % |
| 276 | % % |
| 277 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 278 | % |
| 279 | % UnregisterSTEGANOImage() removes format registrations made by the |
| 280 | % STEGANO module from the list of supported formats. |
| 281 | % |
| 282 | % The format of the UnregisterSTEGANOImage method is: |
| 283 | % |
| 284 | % UnregisterSTEGANOImage(void) |
| 285 | % |
| 286 | */ |
| 287 | ModuleExport void UnregisterSTEGANOImage(void) |
| 288 | { |
| 289 | (void) UnregisterMagickInfo("STEGANO"); |
| 290 | } |