blob: d154a7d580de8644c26a1611145b65adecf93ba8 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/cache.h"
46#include "MagickCore/colormap.h"
47#include "MagickCore/constitute.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/monitor.h"
56#include "MagickCore/monitor-private.h"
57#include "MagickCore/pixel-accessor.h"
58#include "MagickCore/quantum-private.h"
59#include "MagickCore/static.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/module.h"
cristy3ed852e2009-09-05 21:47:34 +000062
63/*
64%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65% %
66% %
67% %
68% R e a d S T E G A N O I m a g e %
69% %
70% %
71% %
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73%
74% ReadSTEGANOImage() reads a steganographic image hidden within another
75% image type. It allocates the memory necessary for the new Image structure
76% and returns a pointer to the new image.
77%
78% The format of the ReadSTEGANOImage method is:
79%
80% Image *ReadSTEGANOImage(const ImageInfo *image_info,
81% ExceptionInfo *exception)
82%
83% A description of each parameter follows:
84%
85% o image_info: the image info.
86%
87% o exception: return any errors or warnings in this structure.
88%
89*/
cristy3ed852e2009-09-05 21:47:34 +000090static Image *ReadSTEGANOImage(const ImageInfo *image_info,
91 ExceptionInfo *exception)
92{
cristyc8463cc2015-06-27 12:23:08 +000093#define GetBit(alpha,i) (((size_t) (alpha) >> (size_t) (i)) & 0x01)
dirk1678aec2015-08-30 12:07:29 +020094#define SetBit(i,set) SetPixelIndex(image,(Quantum) ((set) != 0 ? \
cristy4c08aed2011-07-01 19:47:50 +000095 (size_t) GetPixelIndex(image,q) | (one << (size_t) (i)) : \
96 (size_t) GetPixelIndex(image,q) & ~(one << (size_t) (i))),q)
cristy3ed852e2009-09-05 21:47:34 +000097
98 Image
99 *image,
100 *watermark;
101
102 ImageInfo
103 *read_info;
104
cristye1bf8ad2010-09-19 17:07:03 +0000105 int
106 c;
cristy3ed852e2009-09-05 21:47:34 +0000107
108 MagickBooleanType
109 status;
110
cristy101ab702011-10-13 13:06:32 +0000111 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000112 pixel;
113
cristy4c08aed2011-07-01 19:47:50 +0000114 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000115 *q;
116
cristye1bf8ad2010-09-19 17:07:03 +0000117 register ssize_t
118 x;
119
cristybb503372010-05-27 20:51:26 +0000120 size_t
cristyeaedf062010-05-29 22:36:02 +0000121 depth,
122 one;
cristy3ed852e2009-09-05 21:47:34 +0000123
cristye1bf8ad2010-09-19 17:07:03 +0000124 ssize_t
125 i,
126 j,
127 k,
128 y;
129
cristy3ed852e2009-09-05 21:47:34 +0000130 /*
131 Initialize Image structure.
132 */
133 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000134 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000135 if (image_info->debug != MagickFalse)
136 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
137 image_info->filename);
138 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000139 assert(exception->signature == MagickCoreSignature);
cristyeaedf062010-05-29 22:36:02 +0000140 one=1;
cristy9950d572011-10-01 18:22:35 +0000141 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000142 if ((image->columns == 0) || (image->rows == 0))
143 ThrowReaderException(OptionError,"MustSpecifyImageSize");
144 read_info=CloneImageInfo(image_info);
145 SetImageInfoBlob(read_info,(void *) NULL,0);
146 *read_info->magick='\0';
147 watermark=ReadImage(read_info,exception);
148 read_info=DestroyImageInfo(read_info);
149 if (watermark == (Image *) NULL)
150 return((Image *) NULL);
151 watermark->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy018f07f2011-09-04 21:15:19 +0000152 if (AcquireImageColormap(image,MaxColormapSize,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000153 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
154 if (image_info->ping != MagickFalse)
155 {
156 (void) CloseBlob(image);
157 return(GetFirstImageInList(image));
158 }
cristyacabb842014-12-14 23:36:33 +0000159 status=SetImageExtent(image,image->columns,image->rows,exception);
160 if (status == MagickFalse)
161 return(DestroyImageList(image));
cristy3ed852e2009-09-05 21:47:34 +0000162 /*
163 Get hidden watermark from low-order bits of image.
164 */
165 c=0;
166 i=0;
167 j=0;
cristy222665d2010-09-19 17:08:12 +0000168 i=(ssize_t) (watermark->depth-1);
cristye1bf8ad2010-09-19 17:07:03 +0000169 depth=watermark->depth;
cristybb503372010-05-27 20:51:26 +0000170 for (k=image->offset; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +0000171 {
cristybb503372010-05-27 20:51:26 +0000172 for (y=0; (y < (ssize_t) image->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +0000173 {
174 x=0;
cristye1bf8ad2010-09-19 17:07:03 +0000175 for ( ; (x < (ssize_t) image->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +0000176 {
cristybb503372010-05-27 20:51:26 +0000177 if ((k/(ssize_t) watermark->columns) >= (ssize_t) watermark->rows)
cristy3ed852e2009-09-05 21:47:34 +0000178 break;
cristyf05d4942012-03-17 16:26:09 +0000179 (void) GetOneVirtualPixelInfo(watermark,UndefinedVirtualPixelMethod,
180 k % (ssize_t) watermark->columns,k/(ssize_t) watermark->columns,
181 &pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000182 q=GetAuthenticPixels(image,x,y,1,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000183 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000184 break;
cristy3ed852e2009-09-05 21:47:34 +0000185 switch (c)
186 {
187 case 0:
188 {
cristy4c08aed2011-07-01 19:47:50 +0000189 SetBit(i,GetBit(pixel.red,j));
cristy3ed852e2009-09-05 21:47:34 +0000190 break;
191 }
192 case 1:
193 {
cristy4c08aed2011-07-01 19:47:50 +0000194 SetBit(i,GetBit(pixel.green,j));
cristy3ed852e2009-09-05 21:47:34 +0000195 break;
196 }
197 case 2:
198 {
cristy4c08aed2011-07-01 19:47:50 +0000199 SetBit(i,GetBit(pixel.blue,j));
cristy3ed852e2009-09-05 21:47:34 +0000200 break;
201 }
202 }
203 if (SyncAuthenticPixels(image,exception) == MagickFalse)
204 break;
205 c++;
206 if (c == 3)
207 c=0;
208 k++;
cristybb503372010-05-27 20:51:26 +0000209 if (k == (ssize_t) (watermark->columns*watermark->columns))
cristy3ed852e2009-09-05 21:47:34 +0000210 k=0;
211 if (k == image->offset)
212 j++;
213 }
214 }
cristy222665d2010-09-19 17:08:12 +0000215 status=SetImageProgress(image,LoadImagesTag,(MagickOffsetType) i,depth);
cristy3ed852e2009-09-05 21:47:34 +0000216 if (status == MagickFalse)
217 break;
218 }
219 watermark=DestroyImage(watermark);
cristyea1a8aa2011-10-20 13:24:06 +0000220 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000221 return(GetFirstImageInList(image));
222}
223
224/*
225%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226% %
227% %
228% %
229% R e g i s t e r S T E G A N O I m a g e %
230% %
231% %
232% %
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234%
235% RegisterSTEGANOImage() adds attributes for the STEGANO image format to
236% the list of supported formats. The attributes include the image format
237% tag, a method to read and/or write the format, whether the format
238% supports the saving of more than one frame to the same file or blob,
239% whether the format supports native in-memory I/O, and a brief
240% description of the format.
241%
242% The format of the RegisterSTEGANOImage method is:
243%
cristybb503372010-05-27 20:51:26 +0000244% size_t RegisterSTEGANOImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000245%
246*/
cristybb503372010-05-27 20:51:26 +0000247ModuleExport size_t RegisterSTEGANOImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000248{
249 MagickInfo
250 *entry;
251
dirk06b627a2015-04-06 18:59:17 +0000252 entry=AcquireMagickInfo("STEGANO","STEGANO","Steganographic image");
cristy3ed852e2009-09-05 21:47:34 +0000253 entry->decoder=(DecodeImageHandler *) ReadSTEGANOImage;
cristy009d7392010-07-25 22:08:41 +0000254 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000255 (void) RegisterMagickInfo(entry);
256 return(MagickImageCoderSignature);
257}
258
259/*
260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261% %
262% %
263% %
264% U n r e g i s t e r S T E G A N O I m a g e %
265% %
266% %
267% %
268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269%
270% UnregisterSTEGANOImage() removes format registrations made by the
271% STEGANO module from the list of supported formats.
272%
273% The format of the UnregisterSTEGANOImage method is:
274%
275% UnregisterSTEGANOImage(void)
276%
277*/
278ModuleExport void UnregisterSTEGANOImage(void)
279{
280 (void) UnregisterMagickInfo("STEGANO");
281}