| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 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 | % % |
| Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 20 | % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization % |
| dirk | c8e418d | 2014-03-30 11:27:26 +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 "MagickCore/studio.h" |
| 43 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| 44 | # if defined(__CYGWIN__) |
| 45 | # include <windows.h> |
| 46 | # else |
| 47 | /* All MinGW needs ... */ |
| dirk | 0b2eadc | 2014-04-04 20:57:30 +0000 | [diff] [blame] | 48 | # include "MagickCore/nt-base-private.h" |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 49 | # include <wingdi.h> |
| cristy | 7f563c6 | 2014-06-16 23:08:30 +0000 | [diff] [blame] | 50 | # ifndef DISPLAY_DEVICE_ACTIVE |
| 51 | # define DISPLAY_DEVICE_ACTIVE 0x00000001 |
| 52 | # endif |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 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" |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 65 | #include "MagickCore/module.h" |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 66 | #include "MagickCore/nt-feature.h" |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 67 | #include "MagickCore/option.h" |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 68 | #include "MagickCore/pixel-accessor.h" |
| 69 | #include "MagickCore/quantum-private.h" |
| 70 | #include "MagickCore/static.h" |
| 71 | #include "MagickCore/string_.h" |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 72 | #include "MagickCore/token.h" |
| 73 | #include "MagickCore/utility.h" |
| 74 | #include "MagickCore/xwindow.h" |
| 75 | #include "MagickCore/xwindow-private.h" |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 76 | |
| 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 | */ |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 101 | static Image *ReadSCREENSHOTImage(const ImageInfo *image_info, |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 102 | ExceptionInfo *exception) |
| 103 | { |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 104 | Image |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 105 | *image; |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 106 | |
| cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 107 | assert(image_info->signature == MagickCoreSignature); |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 108 | if (image_info->debug != MagickFalse) |
| 109 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 110 | image_info->filename); |
| 111 | assert(exception != (ExceptionInfo *) NULL); |
| cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 112 | assert(exception->signature == MagickCoreSignature); |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 113 | image=(Image *) NULL; |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 114 | #if defined(MAGICKCORE_WINGDI32_DELEGATE) |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 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 |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 131 | *screen; |
| 132 | |
| 133 | int |
| 134 | i; |
| 135 | |
| cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 136 | MagickBooleanType |
| 137 | status; |
| 138 | |
| dirk | 0b2eadc | 2014-04-04 20:57:30 +0000 | [diff] [blame] | 139 | register Quantum |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 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 | |
| dirk | 0b2eadc | 2014-04-04 20:57:30 +0000 | [diff] [blame] | 164 | screen=AcquireImage(image_info,exception); |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 165 | screen->columns=(size_t) GetDeviceCaps(hDC,HORZRES); |
| 166 | screen->rows=(size_t) GetDeviceCaps(hDC,VERTRES); |
| 167 | screen->storage_class=DirectClass; |
| dirk | 2b2f6c5 | 2015-02-28 17:30:14 +0000 | [diff] [blame] | 168 | status=SetImageExtent(screen,screen->columns,screen->rows,exception); |
| cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 169 | if (status == MagickFalse) |
| 170 | return(DestroyImageList(image)); |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 171 | 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); |
| dirk | 0b2eadc | 2014-04-04 20:57:30 +0000 | [diff] [blame] | 211 | if (q == (Quantum *) NULL) |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 212 | break; |
| 213 | for (x=0; x < (ssize_t) screen->columns; x++) |
| 214 | { |
| dirk | 0b2eadc | 2014-04-04 20:57:30 +0000 | [diff] [blame] | 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); |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 219 | p++; |
| dirk | 3f4111e | 2014-10-12 10:13:08 +0000 | [diff] [blame] | 220 | q+=GetPixelChannels(image); |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 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 | } |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 248 | #endif |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 249 | return(image); |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 250 | } |
| 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 | */ |
| 275 | ModuleExport size_t RegisterSCREENSHOTImage(void) |
| 276 | { |
| 277 | MagickInfo |
| 278 | *entry; |
| 279 | |
| dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 280 | entry=AcquireMagickInfo("SCREENSHOT","SCREENSHOT","Screen shot"); |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 281 | entry->decoder=(DecodeImageHandler *) ReadSCREENSHOTImage; |
| 282 | entry->format_type=ImplicitFormatType; |
| dirk | c8e418d | 2014-03-30 11:27:26 +0000 | [diff] [blame] | 283 | (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 | */ |
| 306 | ModuleExport void UnregisterSCREENSHOTImage(void) |
| 307 | { |
| 308 | (void) UnregisterMagickInfo("SCREENSHOT"); |
| cristy | b03e4c0 | 2014-03-30 12:05:00 +0000 | [diff] [blame] | 309 | } |