]> granicus.if.org Git - imagemagick/blob - coders/screenshot.c
Use memset() rather than ResetMagickMemory()
[imagemagick] / coders / screenshot.c
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-2018 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://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 \f
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 ... */
48 #    include "MagickCore/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 "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"
65 #include "MagickCore/module.h"
66 #include "MagickCore/nt-feature.h"
67 #include "MagickCore/option.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/quantum-private.h"
70 #include "MagickCore/static.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/token.h"
73 #include "MagickCore/utility.h"
74 #include "MagickCore/xwindow.h"
75 #include "MagickCore/xwindow-private.h"
76 \f
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 */
101 static Image *ReadSCREENSHOTImage(const ImageInfo *image_info,
102   ExceptionInfo *exception)
103 {
104   Image
105     *image;
106
107   assert(image_info->signature == MagickCoreSignature);
108   if (image_info->debug != MagickFalse)
109     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
110       image_info->filename);
111   assert(exception != (ExceptionInfo *) NULL);
112   assert(exception->signature == MagickCoreSignature);
113   image=(Image *) NULL;
114 #if defined(MAGICKCORE_WINGDI32_DELEGATE)
115   {
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
131       *screen;
132
133     int
134       i;
135
136     MagickBooleanType
137       status;
138
139     register Quantum
140       *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
164       screen=AcquireImage(image_info,exception);
165       screen->columns=(size_t) GetDeviceCaps(hDC,HORZRES);
166       screen->rows=(size_t) GetDeviceCaps(hDC,VERTRES);
167       screen->storage_class=DirectClass;
168       if (image == (Image *) NULL)
169         image=screen;
170       else
171         AppendImageToList(&image,screen);
172       status=SetImageExtent(screen,screen->columns,screen->rows,exception);
173       if (status == MagickFalse)
174         return(DestroyImageList(image));
175
176       bitmapDC=CreateCompatibleDC(hDC);
177       if (bitmapDC == (HDC) NULL)
178         {
179           DeleteDC(hDC);
180           ThrowReaderException(CoderError,"UnableToCreateDC");
181         }
182       (void) memset(&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);
211         if (q == (Quantum *) NULL)
212           break;
213         for (x=0; x < (ssize_t) screen->columns; x++)
214         {
215           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);
219           p++;
220           q+=GetPixelChannels(image);
221         }
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   }
248 #endif
249   return(image);
250 }
251 \f
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 */
275 ModuleExport size_t RegisterSCREENSHOTImage(void)
276 {
277   MagickInfo
278     *entry;
279
280   entry=AcquireMagickInfo("SCREENSHOT","SCREENSHOT","Screen shot");
281   entry->decoder=(DecodeImageHandler *) ReadSCREENSHOTImage;
282   entry->format_type=ImplicitFormatType;
283   (void) RegisterMagickInfo(entry);
284   return(MagickImageCoderSignature);
285 }
286 \f
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 */
306 ModuleExport void UnregisterSCREENSHOTImage(void)
307 {
308   (void) UnregisterMagickInfo("SCREENSHOT");
309 }