1 /*
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 %                                                                             %
20 %  Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization      %
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 %    https://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 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
44 #  if defined(__CYGWIN__)
45 #    include <windows.h>
46 #  else
47      /* All MinGW needs ... */
48 #    include "magick/nt-base-private.h"
49 #    include <wingdi.h>
50 #  ifndef DISPLAY_DEVICE_ACTIVE
51 #    define DISPLAY_DEVICE_ACTIVE    0x00000001
52 #  endif
53 #  endif
54 #endif
55 #include "magick/blob.h"
56 #include "magick/blob-private.h"
57 #include "magick/cache.h"
58 #include "magick/exception.h"
59 #include "magick/exception-private.h"
60 #include "magick/image.h"
61 #include "magick/image-private.h"
62 #include "magick/list.h"
63 #include "magick/magick.h"
64 #include "magick/memory_.h"
65 #include "magick/module.h"
66 #include "magick/nt-feature.h"
67 #include "magick/option.h"
68 #include "magick/pixel-accessor.h"
69 #include "magick/quantum-private.h"
70 #include "magick/static.h"
71 #include "magick/string_.h"
72 #include "magick/token.h"
73 #include "magick/transform.h"
74 #include "magick/utility.h"
75 #include "magick/xwindow.h"
76 #include "magick/xwindow-private.h"
77 
78 /*
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %                                                                             %
81 %                                                                             %
82 %                                                                             %
83 %   R e a d S C R E E N S H O T I m a g e                                     %
84 %                                                                             %
85 %                                                                             %
86 %                                                                             %
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %
89 %  ReadSCREENSHOTImage() Takes a screenshot from the monitor(s).
90 %
91 %  The format of the ReadSCREENSHOTImage method is:
92 %
93 %      Image *ReadXImage(const ImageInfo *image_info,ExceptionInfo *exception)
94 %
95 %  A description of each parameter follows:
96 %
97 %    o image_info: the image info.
98 %
99 %    o exception: return any errors or warnings in this structure.
100 %
101 */
ReadSCREENSHOTImage(const ImageInfo * image_info,ExceptionInfo * exception)102 static Image *ReadSCREENSHOTImage(const ImageInfo *image_info,
103   ExceptionInfo *exception)
104 {
105   Image
106     *image;
107 
108   assert(image_info->signature == MagickCoreSignature);
109   if (image_info->debug != MagickFalse)
110     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
111       image_info->filename);
112   assert(exception != (ExceptionInfo *) NULL);
113   assert(exception->signature == MagickCoreSignature);
114   image=(Image *) NULL;
115 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
116   {
117     BITMAPINFO
118       bmi;
119 
120     DISPLAY_DEVICE
121       device;
122 
123     HBITMAP
124       bitmap,
125       bitmapOld;
126 
127     HDC
128       bitmapDC,
129       hDC;
130 
131     Image
132       *screen;
133 
134     int
135       i;
136 
137     MagickBooleanType
138       status;
139 
140     PixelPacket
141       *q;
142 
143     ssize_t
144       x;
145 
146     RGBQUAD
147       *p;
148 
149     ssize_t
150       y;
151 
152     assert(image_info != (const ImageInfo *) NULL);
153     i=0;
154     device.cb = sizeof(device);
155     image=(Image *) NULL;
156     while(EnumDisplayDevices(NULL,i,&device,0) && ++i)
157     {
158       if ((device.StateFlags & DISPLAY_DEVICE_ACTIVE) != DISPLAY_DEVICE_ACTIVE)
159         continue;
160 
161       hDC=CreateDC(device.DeviceName,device.DeviceName,NULL,NULL);
162       if (hDC == (HDC) NULL)
163         ThrowReaderException(CoderError,"UnableToCreateDC");
164 
165       screen=AcquireImage(image_info);
166       screen->columns=(size_t) GetDeviceCaps(hDC,HORZRES);
167       screen->rows=(size_t) GetDeviceCaps(hDC,VERTRES);
168       screen->storage_class=DirectClass;
169       if (image == (Image *) NULL)
170         image=screen;
171       else
172         AppendImageToList(&image,screen);
173       status=SetImageExtent(screen,screen->columns,screen->rows);
174       if (status == MagickFalse)
175         {
176           InheritException(exception,&image->exception);
177           return(DestroyImageList(image));
178         }
179 
180       bitmapDC=CreateCompatibleDC(hDC);
181       if (bitmapDC == (HDC) NULL)
182         {
183           DeleteDC(hDC);
184           ThrowReaderException(CoderError,"UnableToCreateDC");
185         }
186       (void) memset(&bmi,0,sizeof(BITMAPINFO));
187       bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
188       bmi.bmiHeader.biWidth=(LONG) screen->columns;
189       bmi.bmiHeader.biHeight=(-1)*(LONG) screen->rows;
190       bmi.bmiHeader.biPlanes=1;
191       bmi.bmiHeader.biBitCount=32;
192       bmi.bmiHeader.biCompression=BI_RGB;
193       bitmap=CreateDIBSection(hDC,&bmi,DIB_RGB_COLORS,(void **) &p,NULL,0);
194       if (bitmap == (HBITMAP) NULL)
195         {
196           DeleteDC(hDC);
197           DeleteDC(bitmapDC);
198           ThrowReaderException(CoderError,"UnableToCreateBitmap");
199         }
200       bitmapOld=(HBITMAP) SelectObject(bitmapDC,bitmap);
201       if (bitmapOld == (HBITMAP) NULL)
202         {
203           DeleteDC(hDC);
204           DeleteDC(bitmapDC);
205           DeleteObject(bitmap);
206           ThrowReaderException(CoderError,"UnableToCreateBitmap");
207         }
208       BitBlt(bitmapDC,0,0,(int) screen->columns,(int) screen->rows,hDC,0,0,
209         SRCCOPY);
210       (void) SelectObject(bitmapDC,bitmapOld);
211 
212       for (y=0; y < (ssize_t) screen->rows; y++)
213       {
214         q=QueueAuthenticPixels(screen,0,y,screen->columns,1,exception);
215         if (q == (PixelPacket *) NULL)
216           break;
217         for (x=0; x < (ssize_t) screen->columns; x++)
218         {
219           SetPixelRed(q,ScaleCharToQuantum(p->rgbRed));
220           SetPixelGreen(q,ScaleCharToQuantum(p->rgbGreen));
221           SetPixelBlue(q,ScaleCharToQuantum(p->rgbBlue));
222           SetPixelOpacity(q,OpaqueOpacity);
223           p++;
224           q++;
225         }
226         if (SyncAuthenticPixels(screen,exception) == MagickFalse)
227           break;
228       }
229 
230       DeleteDC(hDC);
231       DeleteDC(bitmapDC);
232       DeleteObject(bitmap);
233     }
234   }
235 #elif defined(MAGICKCORE_X11_DELEGATE)
236   {
237     const char
238       *option;
239 
240     XImportInfo
241       ximage_info;
242 
243     (void) exception;
244     XGetImportInfo(&ximage_info);
245     option=GetImageOption(image_info,"x:screen");
246     if (option != (const char *) NULL)
247       ximage_info.screen=IsMagickTrue(option);
248     option=GetImageOption(image_info,"x:silent");
249     if (option != (const char *) NULL)
250       ximage_info.silent=IsMagickTrue(option);
251     image=XImportImage(image_info,&ximage_info);
252     if ((image != (Image *) NULL) && (image_info->extract != (char *) NULL))
253       {
254         Image
255           *crop_image;
256 
257         RectangleInfo
258           crop_info;
259 
260         /*
261           Crop image as defined by the extract rectangle.
262         */
263         (void) ParsePageGeometry(image,image_info->extract,&crop_info,
264           exception);
265         crop_image=CropImage(image,&crop_info,exception);
266         if (crop_image != (Image *) NULL)
267           {
268             image=DestroyImage(image);
269             image=crop_image;
270           }
271       }
272   }
273 #endif
274   return(image);
275 }
276 
277 /*
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 %                                                                             %
280 %                                                                             %
281 %                                                                             %
282 %   R e g i s t e r S C R E E N S H O T I m a g e                             %
283 %                                                                             %
284 %                                                                             %
285 %                                                                             %
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 %
288 %  RegisterSCREENSHOTImage() adds attributes for the screen shot format to
289 %  the list of supported formats.  The attributes include the image format
290 %  tag, a method to read and/or write the format, whether the format
291 %  supports the saving of more than one frame to the same file or blob,
292 %  whether the format supports native in-memory I/O, and a brief
293 %  description of the format.
294 %
295 %  The format of the RegisterScreenShotImage method is:
296 %
297 %      size_t RegisterScreenShotImage(void)
298 %
299 */
RegisterSCREENSHOTImage(void)300 ModuleExport size_t RegisterSCREENSHOTImage(void)
301 {
302   MagickInfo
303     *entry;
304 
305   entry=SetMagickInfo("SCREENSHOT");
306   entry->decoder=(DecodeImageHandler *) ReadSCREENSHOTImage;
307   entry->format_type=ImplicitFormatType;
308   entry->description=ConstantString("Screen shot");
309   entry->magick_module=ConstantString("SCREENSHOT");
310   (void) RegisterMagickInfo(entry);
311   return(MagickImageCoderSignature);
312 }
313 
314 /*
315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 %                                                                             %
317 %                                                                             %
318 %                                                                             %
319 %   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                         %
320 %                                                                             %
321 %                                                                             %
322 %                                                                             %
323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 %
325 %  UnregisterScreenShotImage() removes format registrations made by the
326 %  screen shot module from the list of supported formats.
327 %
328 %  The format of the UnregisterSCREENSHOTImage method is:
329 %
330 %      UnregisterSCREENSHOTImage(void)
331 %
332 */
UnregisterSCREENSHOTImage(void)333 ModuleExport void UnregisterSCREENSHOTImage(void)
334 {
335   (void) UnregisterMagickInfo("SCREENSHOT");
336 }
337