blob: 7e25bf6cc17053f4fc602d73cd70cfb42ff5d98f [file] [log] [blame]
dirkc8e418d2014-03-30 11:27:26 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% SSSSS CCCC RRRR EEEEE EEEEE N N SSSSS H H OOO TTTTT %
7% SS C R R E E NN N SS H H O O T %
8% SSS C RRRR EEE EEE N N N SSS HHHHH O O T %
9% SS C R R E E N NN SS H H O O T %
10% SSSSS CCCC R R EEEEE EEEEE N N SSSSS H H OOO T %
11% %
12% %
13% Takes a screenshot from the monitor(s). %
14% %
15% Software Design %
16% Dirk Lemstra %
17% April 2014 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization %
dirkc8e418d2014-03-30 11:27:26 +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 "MagickCore/studio.h"
43#if defined(MAGICKCORE_WINGDI32_DELEGATE)
44# if defined(__CYGWIN__)
45# include <windows.h>
46# else
47 /* All MinGW needs ... */
dirk0b2eadc2014-04-04 20:57:30 +000048# include "MagickCore/nt-base-private.h"
dirkc8e418d2014-03-30 11:27:26 +000049# include <wingdi.h>
cristy7f563c62014-06-16 23:08:30 +000050# ifndef DISPLAY_DEVICE_ACTIVE
51# define DISPLAY_DEVICE_ACTIVE 0x00000001
52# endif
dirkc8e418d2014-03-30 11:27:26 +000053# endif
54#endif
55#include "MagickCore/blob.h"
56#include "MagickCore/blob-private.h"
57#include "MagickCore/cache.h"
58#include "MagickCore/exception.h"
59#include "MagickCore/exception-private.h"
60#include "MagickCore/image.h"
61#include "MagickCore/image-private.h"
62#include "MagickCore/list.h"
63#include "MagickCore/magick.h"
64#include "MagickCore/memory_.h"
cristyb03e4c02014-03-30 12:05:00 +000065#include "MagickCore/module.h"
dirkc8e418d2014-03-30 11:27:26 +000066#include "MagickCore/nt-feature.h"
cristyb03e4c02014-03-30 12:05:00 +000067#include "MagickCore/option.h"
dirkc8e418d2014-03-30 11:27:26 +000068#include "MagickCore/pixel-accessor.h"
69#include "MagickCore/quantum-private.h"
70#include "MagickCore/static.h"
71#include "MagickCore/string_.h"
cristyb03e4c02014-03-30 12:05:00 +000072#include "MagickCore/token.h"
73#include "MagickCore/utility.h"
74#include "MagickCore/xwindow.h"
75#include "MagickCore/xwindow-private.h"
dirkc8e418d2014-03-30 11:27:26 +000076
77/*
78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79% %
80% %
81% %
82% R e a d S C R E E N S H O T I m a g e %
83% %
84% %
85% %
86%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87%
88% ReadSCREENSHOTImage() Takes a screenshot from the monitor(s).
89%
90% The format of the ReadSCREENSHOTImage method is:
91%
92% Image *ReadXImage(const ImageInfo *image_info,ExceptionInfo *exception)
93%
94% A description of each parameter follows:
95%
96% o image_info: the image info.
97%
98% o exception: return any errors or warnings in this structure.
99%
100*/
cristyb03e4c02014-03-30 12:05:00 +0000101static Image *ReadSCREENSHOTImage(const ImageInfo *image_info,
dirkc8e418d2014-03-30 11:27:26 +0000102 ExceptionInfo *exception)
103{
dirkc8e418d2014-03-30 11:27:26 +0000104 Image
cristyb03e4c02014-03-30 12:05:00 +0000105 *image;
dirkc8e418d2014-03-30 11:27:26 +0000106
cristye1c94d92015-06-28 12:16:33 +0000107 assert(image_info->signature == MagickCoreSignature);
dirkc8e418d2014-03-30 11:27:26 +0000108 if (image_info->debug != MagickFalse)
109 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
110 image_info->filename);
111 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000112 assert(exception->signature == MagickCoreSignature);
dirkc8e418d2014-03-30 11:27:26 +0000113 image=(Image *) NULL;
dirkc8e418d2014-03-30 11:27:26 +0000114#if defined(MAGICKCORE_WINGDI32_DELEGATE)
cristyb03e4c02014-03-30 12:05:00 +0000115 {
116 BITMAPINFO
117 bmi;
118
119 DISPLAY_DEVICE
120 device;
121
122 HBITMAP
123 bitmap,
124 bitmapOld;
125
126 HDC
127 bitmapDC,
128 hDC;
129
130 Image
cristyb03e4c02014-03-30 12:05:00 +0000131 *screen;
132
133 int
134 i;
135
cristyacabb842014-12-14 23:36:33 +0000136 MagickBooleanType
137 status;
138
dirk0b2eadc2014-04-04 20:57:30 +0000139 register Quantum
cristyb03e4c02014-03-30 12:05:00 +0000140 *q;
141
142 register ssize_t
143 x;
144
145 RGBTRIPLE
146 *p;
147
148 ssize_t
149 y;
150
151 assert(image_info != (const ImageInfo *) NULL);
152 i=0;
153 device.cb = sizeof(device);
154 image=(Image *) NULL;
155 while(EnumDisplayDevices(NULL,i,&device,0) && ++i)
156 {
157 if ((device.StateFlags & DISPLAY_DEVICE_ACTIVE) != DISPLAY_DEVICE_ACTIVE)
158 continue;
159
160 hDC=CreateDC(device.DeviceName,device.DeviceName,NULL,NULL);
161 if (hDC == (HDC) NULL)
162 ThrowReaderException(CoderError,"UnableToCreateDC");
163
dirk0b2eadc2014-04-04 20:57:30 +0000164 screen=AcquireImage(image_info,exception);
cristyb03e4c02014-03-30 12:05:00 +0000165 screen->columns=(size_t) GetDeviceCaps(hDC,HORZRES);
166 screen->rows=(size_t) GetDeviceCaps(hDC,VERTRES);
167 screen->storage_class=DirectClass;
dirk2b2f6c52015-02-28 17:30:14 +0000168 status=SetImageExtent(screen,screen->columns,screen->rows,exception);
cristyacabb842014-12-14 23:36:33 +0000169 if (status == MagickFalse)
170 return(DestroyImageList(image));
cristyb03e4c02014-03-30 12:05:00 +0000171 if (image == (Image *) NULL)
172 image=screen;
173 else
174 AppendImageToList(&image,screen);
175
176 bitmapDC=CreateCompatibleDC(hDC);
177 if (bitmapDC == (HDC) NULL)
178 {
179 DeleteDC(hDC);
180 ThrowReaderException(CoderError,"UnableToCreateDC");
181 }
182 (void) ResetMagickMemory(&bmi,0,sizeof(BITMAPINFO));
183 bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
184 bmi.bmiHeader.biWidth=(LONG) screen->columns;
185 bmi.bmiHeader.biHeight=(-1)*(LONG) screen->rows;
186 bmi.bmiHeader.biPlanes=1;
187 bmi.bmiHeader.biBitCount=24;
188 bmi.bmiHeader.biCompression=BI_RGB;
189 bitmap=CreateDIBSection(hDC,&bmi,DIB_RGB_COLORS,(void **) &p,NULL,0);
190 if (bitmap == (HBITMAP) NULL)
191 {
192 DeleteDC(hDC);
193 DeleteDC(bitmapDC);
194 ThrowReaderException(CoderError,"UnableToCreateBitmap");
195 }
196 bitmapOld=(HBITMAP) SelectObject(bitmapDC,bitmap);
197 if (bitmapOld == (HBITMAP) NULL)
198 {
199 DeleteDC(hDC);
200 DeleteDC(bitmapDC);
201 DeleteObject(bitmap);
202 ThrowReaderException(CoderError,"UnableToCreateBitmap");
203 }
204 BitBlt(bitmapDC,0,0,(int) screen->columns,(int) screen->rows,hDC,0,0,
205 SRCCOPY);
206 (void) SelectObject(bitmapDC,bitmapOld);
207
208 for (y=0; y < (ssize_t) screen->rows; y++)
209 {
210 q=QueueAuthenticPixels(screen,0,y,screen->columns,1,exception);
dirk0b2eadc2014-04-04 20:57:30 +0000211 if (q == (Quantum *) NULL)
cristyb03e4c02014-03-30 12:05:00 +0000212 break;
213 for (x=0; x < (ssize_t) screen->columns; x++)
214 {
dirk0b2eadc2014-04-04 20:57:30 +0000215 SetPixelRed(image,ScaleCharToQuantum(p->rgbtRed),q);
216 SetPixelGreen(image,ScaleCharToQuantum(p->rgbtGreen),q);
217 SetPixelBlue(image,ScaleCharToQuantum(p->rgbtBlue),q);
218 SetPixelAlpha(image,OpaqueAlpha,q);
cristyb03e4c02014-03-30 12:05:00 +0000219 p++;
dirk3f4111e2014-10-12 10:13:08 +0000220 q+=GetPixelChannels(image);
cristyb03e4c02014-03-30 12:05:00 +0000221 }
222 if (SyncAuthenticPixels(screen,exception) == MagickFalse)
223 break;
224 }
225
226 DeleteDC(hDC);
227 DeleteDC(bitmapDC);
228 DeleteObject(bitmap);
229 }
230 }
231#elif defined(MAGICKCORE_X11_DELEGATE)
232 {
233 const char
234 *option;
235
236 XImportInfo
237 ximage_info;
238
239 XGetImportInfo(&ximage_info);
240 option=GetImageOption(image_info,"x:screen");
241 if (option != (const char *) NULL)
242 ximage_info.screen=IsStringTrue(option);
243 option=GetImageOption(image_info,"x:silent");
244 if (option != (const char *) NULL)
245 ximage_info.silent=IsStringTrue(option);
246 image=XImportImage(image_info,&ximage_info,exception);
247 }
dirkc8e418d2014-03-30 11:27:26 +0000248#endif
cristyb03e4c02014-03-30 12:05:00 +0000249 return(image);
dirkc8e418d2014-03-30 11:27:26 +0000250}
251
252/*
253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254% %
255% %
256% %
257% R e g i s t e r S C R E E N S H O T I m a g e %
258% %
259% %
260% %
261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262%
263% RegisterSCREENSHOTImage() adds attributes for the screen shot format to
264% the list of supported formats. The attributes include the image format
265% tag, a method to read and/or write the format, whether the format
266% supports the saving of more than one frame to the same file or blob,
267% whether the format supports native in-memory I/O, and a brief
268% description of the format.
269%
270% The format of the RegisterScreenShotImage method is:
271%
272% size_t RegisterScreenShotImage(void)
273%
274*/
275ModuleExport size_t RegisterSCREENSHOTImage(void)
276{
277 MagickInfo
278 *entry;
279
dirk06b627a2015-04-06 18:59:17 +0000280 entry=AcquireMagickInfo("SCREENSHOT","SCREENSHOT","Screen shot");
dirkc8e418d2014-03-30 11:27:26 +0000281 entry->decoder=(DecodeImageHandler *) ReadSCREENSHOTImage;
282 entry->format_type=ImplicitFormatType;
dirkc8e418d2014-03-30 11:27:26 +0000283 (void) RegisterMagickInfo(entry);
284 return(MagickImageCoderSignature);
285}
286
287/*
288%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289% %
290% %
291% %
292% U n r e g i s t e r S C R E E N S H O T I m a g e %
293% %
294% %
295% %
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297%
298% UnregisterScreenShotImage() removes format registrations made by the
299% screen shot module from the list of supported formats.
300%
301% The format of the UnregisterSCREENSHOTImage method is:
302%
303% UnregisterSCREENSHOTImage(void)
304%
305*/
306ModuleExport void UnregisterSCREENSHOTImage(void)
307{
308 (void) UnregisterMagickInfo("SCREENSHOT");
cristyb03e4c02014-03-30 12:05:00 +0000309}