blob: f1f977e74c74818b1949d7115c1647c5ea3a1a96 [file] [log] [blame]
cristyf7836bf2012-02-20 16:32:47 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% %
7% N N TTTTT %
8% NN N T %
9% N N N T %
10% N NN T %
11% N N T %
12% %
13% %
14% Windows NT Feature Methods for MagickCore %
15% %
16% Software Design %
17% John Cristy %
18% December 1996 %
19% %
20% %
21% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
22% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% http://www.imagemagick.org/script/license.php %
28% %
29% Unless required by applicable law or agreed to in writing, software %
30% distributed under the License is distributed on an "AS IS" BASIS, %
31% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32% See the License for the specific language governing permissions and %
33% limitations under the License. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
cristy309042f2012-02-20 18:59:35 +000043#include "MagickCore/studio.h"
cristyf7836bf2012-02-20 16:32:47 +000044#if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
45#define WIN32_LEAN_AND_MEAN
46#define VC_EXTRALEAN
47#include <windows.h>
cristy309042f2012-02-20 18:59:35 +000048#include "MagickCore/cache.h"
49#include "MagickCore/colorspace.h"
50#include "MagickCore/colorspace-private.h"
51#include "MagickCore/draw.h"
52#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/image-private.h"
55#include "MagickCore/memory_.h"
56#include "MagickCore/monitor.h"
57#include "MagickCore/monitor-private.h"
cristyd2d11ec2012-03-28 13:53:49 +000058#include "MagickCore/nt-base.h"
59#include "MagickCore/nt-base-private.h"
cristy309042f2012-02-20 18:59:35 +000060#include "MagickCore/pixel-accessor.h"
61#include "MagickCore/quantum.h"
62#include "MagickCore/string_.h"
63#include "MagickCore/token.h"
64#include "MagickCore/splay-tree.h"
65#include "MagickCore/utility.h"
cristy2c5fc272012-02-22 01:27:46 +000066#include "MagickCore/nt-base-private.h"
cristyf7836bf2012-02-20 16:32:47 +000067
68/*
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70% %
71% %
72% %
73% C r o p I m a g e T o H B i t m a p %
74% %
75% %
76% %
77%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78%
79% CropImageToHBITMAP() extracts a specified region of the image and returns
80% it as a Windows HBITMAP. While the same functionality can be accomplished by
81% invoking CropImage() followed by ImageToHBITMAP(), this method is more
82% efficient since it copies pixels directly to the HBITMAP.
83%
84% The format of the CropImageToHBITMAP method is:
85%
86% HBITMAP CropImageToHBITMAP(Image* image,const RectangleInfo *geometry,
87% ExceptionInfo *exception)
88%
89% A description of each parameter follows:
90%
91% o image: the image.
92%
93% o geometry: Define the region of the image to crop with members
94% x, y, width, and height.
95%
96% o exception: return any errors or warnings in this structure.
97%
98*/
99MagickExport void *CropImageToHBITMAP(Image *image,
100 const RectangleInfo *geometry,ExceptionInfo *exception)
101{
102#define CropImageTag "Crop/Image"
103
104 BITMAP
105 bitmap;
106
107 HBITMAP
108 bitmapH;
109
110 HANDLE
111 bitmap_bitsH;
112
113 MagickBooleanType
114 proceed;
115
116 RectangleInfo
117 page;
118
cristy309042f2012-02-20 18:59:35 +0000119 register const Quantum
cristyf7836bf2012-02-20 16:32:47 +0000120 *p;
121
122 register RGBQUAD
123 *q;
124
125 RGBQUAD
126 *bitmap_bits;
127
128 ssize_t
129 y;
130
131 /*
132 Check crop geometry.
133 */
134 assert(image != (const Image *) NULL);
135 assert(image->signature == MagickSignature);
136 if (image->debug != MagickFalse)
137 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
138 assert(geometry != (const RectangleInfo *) NULL);
139 assert(exception != (ExceptionInfo *) NULL);
140 assert(exception->signature == MagickSignature);
141 if (((geometry->x+(ssize_t) geometry->width) < 0) ||
142 ((geometry->y+(ssize_t) geometry->height) < 0) ||
143 (geometry->x >= (ssize_t) image->columns) ||
144 (geometry->y >= (ssize_t) image->rows))
145 ThrowImageException(OptionError,"GeometryDoesNotContainImage");
146 page=(*geometry);
147 if ((page.x+(ssize_t) page.width) > (ssize_t) image->columns)
148 page.width=image->columns-page.x;
149 if ((page.y+(ssize_t) page.height) > (ssize_t) image->rows)
150 page.height=image->rows-page.y;
151 if (page.x < 0)
152 {
153 page.width+=page.x;
154 page.x=0;
155 }
156 if (page.y < 0)
157 {
158 page.height+=page.y;
159 page.y=0;
160 }
161
162 if ((page.width == 0) || (page.height == 0))
163 ThrowImageException(OptionError,"GeometryDimensionsAreZero");
164 /*
165 Initialize crop image attributes.
166 */
167 bitmap.bmType = 0;
168 bitmap.bmWidth = (LONG) page.width;
169 bitmap.bmHeight = (LONG) page.height;
170 bitmap.bmWidthBytes = bitmap.bmWidth * 4;
171 bitmap.bmPlanes = 1;
172 bitmap.bmBitsPixel = 32;
173 bitmap.bmBits = NULL;
174
175 bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,page.width*
176 page.height*bitmap.bmBitsPixel);
177 if (bitmap_bitsH == NULL)
178 return(NULL);
179 bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
180 if ( bitmap.bmBits == NULL )
181 bitmap.bmBits = bitmap_bits;
182 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy309042f2012-02-20 18:59:35 +0000183 TransformImageColorspace(image,sRGBColorspace,exception);
cristyf7836bf2012-02-20 16:32:47 +0000184 /*
185 Extract crop image.
186 */
187 q=bitmap_bits;
188 for (y=0; y < (ssize_t) page.height; y++)
189 {
cristy309042f2012-02-20 18:59:35 +0000190 register ssize_t
191 x;
192
cristyf7836bf2012-02-20 16:32:47 +0000193 p=GetVirtualPixels(image,page.x,page.y+y,page.width,1,exception);
cristy309042f2012-02-20 18:59:35 +0000194 if (p == (const Quantum *) NULL)
cristyf7836bf2012-02-20 16:32:47 +0000195 break;
196
cristy309042f2012-02-20 18:59:35 +0000197 /* Transfer pixels, scaling to Quantum */
198 for( x=(ssize_t) page.width ; x> 0 ; x-- )
199 {
200 q->rgbRed = ScaleQuantumToChar(GetPixelRed(image,p));
201 q->rgbGreen = ScaleQuantumToChar(GetPixelGreen(image,p));
202 q->rgbBlue = ScaleQuantumToChar(GetPixelBlue(image,p));
203 q->rgbReserved = 0;
204 p+=GetPixelChannels(image);
205 q++;
206 }
cristyf7836bf2012-02-20 16:32:47 +0000207 proceed=SetImageProgress(image,CropImageTag,y,page.height);
208 if (proceed == MagickFalse)
209 break;
210 }
211 if (y < (ssize_t) page.height)
212 {
213 GlobalUnlock((HGLOBAL) bitmap_bitsH);
214 GlobalFree((HGLOBAL) bitmap_bitsH);
215 return((void *) NULL);
216 }
217 bitmap.bmBits=bitmap_bits;
218 bitmapH=CreateBitmapIndirect(&bitmap);
219 GlobalUnlock((HGLOBAL) bitmap_bitsH);
220 GlobalFree((HGLOBAL) bitmap_bitsH);
221 return((void *) bitmapH);
222}
223
224/*
225%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226% %
227% %
228% %
229% I s M a g i c k C o n f l i c t %
230% %
231% %
232% %
233%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234%
235% IsMagickConflict() returns true if the image format conflicts with a logical
236% drive (.e.g. X:).
237%
238% The format of the IsMagickConflict method is:
239%
240% MagickBooleanType IsMagickConflict(const char *magick)
241%
242% A description of each parameter follows:
243%
244% o magick: Specifies the image format.
245%
246*/
247MagickExport MagickBooleanType NTIsMagickConflict(const char *magick)
248{
249 MagickBooleanType
250 status;
251
252 assert(magick != (char *) NULL);
253 if (strlen(magick) > 1)
254 return(MagickFalse);
255 status=(GetLogicalDrives() & (1 << ((toupper((int) (*magick)))-'A'))) != 0 ?
256 MagickTrue : MagickFalse;
257 return(status);
258}
259
260/*
261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262% %
263% %
264% %
265+ N T G e t T y pe L i s t %
266% %
267% %
268% %
269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270%
271% NTLoadTypeLists() loads a Windows TrueType fonts.
272%
273% The format of the NTLoadTypeLists method is:
274%
275% MagickBooleanType NTLoadTypeLists(SplayTreeInfo *type_list)
276%
277% A description of each parameter follows:
278%
279% o type_list: A linked list of fonts.
280%
281*/
282MagickExport MagickBooleanType NTLoadTypeLists(SplayTreeInfo *type_list,
283 ExceptionInfo *exception)
284{
285 HKEY
286 reg_key = (HKEY) INVALID_HANDLE_VALUE;
287
288 LONG
289 res;
290
291
292 int
293 list_entries = 0;
294
295 char
296 buffer[MaxTextExtent],
297 system_root[MaxTextExtent],
298 font_root[MaxTextExtent];
299
300 DWORD
301 type,
302 system_root_length;
303
304 MagickBooleanType
305 status;
306
307 /*
308 Try to find the right Windows*\CurrentVersion key, the SystemRoot and
309 then the Fonts key
310 */
311 res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
312 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &reg_key);
313 if (res == ERROR_SUCCESS) {
314 system_root_length=sizeof(system_root)-1;
315 res = RegQueryValueExA(reg_key,"SystemRoot",NULL, &type,
316 (BYTE*) system_root, &system_root_length);
317 }
318 if (res != ERROR_SUCCESS) {
319 res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
320 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", 0, KEY_READ, &reg_key);
321 if (res == ERROR_SUCCESS) {
322 system_root_length=sizeof(system_root)-1;
323 res = RegQueryValueExA(reg_key,"SystemRoot",NULL, &type,
324 (BYTE*)system_root, &system_root_length);
325 }
326 }
327 if (res == ERROR_SUCCESS)
328 res = RegOpenKeyExA (reg_key, "Fonts",0, KEY_READ, &reg_key);
329 if (res != ERROR_SUCCESS)
330 return(MagickFalse);
331 *font_root='\0';
332 (void) CopyMagickString(buffer,system_root,MaxTextExtent);
333 (void) ConcatenateMagickString(buffer,"\\fonts\\arial.ttf",MaxTextExtent);
334 if (IsPathAccessible(buffer) != MagickFalse)
335 {
336 (void) CopyMagickString(font_root,system_root,MaxTextExtent);
337 (void) ConcatenateMagickString(font_root,"\\fonts\\",MaxTextExtent);
338 }
339 else
340 {
341 (void) CopyMagickString(font_root,system_root,MaxTextExtent);
342 (void) ConcatenateMagickString(font_root,"\\",MaxTextExtent);
343 }
344
345 {
346 TypeInfo
347 *type_info;
348
349 DWORD
350 registry_index = 0,
351 type,
352 value_data_size,
353 value_name_length;
354
355 char
356 value_data[MaxTextExtent],
357 value_name[MaxTextExtent];
358
359 res = ERROR_SUCCESS;
360
361 while (res != ERROR_NO_MORE_ITEMS)
362 {
363 char
364 *family_extent,
365 token[MaxTextExtent],
366 *pos,
367 *q;
368
369 value_name_length = sizeof(value_name) - 1;
370 value_data_size = sizeof(value_data) - 1;
371 res = RegEnumValueA ( reg_key, registry_index, value_name,
372 &value_name_length, 0, &type, (BYTE*)value_data, &value_data_size);
373 registry_index++;
374 if (res != ERROR_SUCCESS)
375 continue;
376 if ( (pos = strstr(value_name, " (TrueType)")) == (char*) NULL )
377 continue;
378 *pos='\0'; /* Remove (TrueType) from string */
379
380 type_info=(TypeInfo *) AcquireMagickMemory(sizeof(*type_info));
381 if (type_info == (TypeInfo *) NULL)
382 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
383 (void) ResetMagickMemory(type_info,0,sizeof(TypeInfo));
384
385 type_info->path=ConstantString("Windows Fonts");
386 type_info->signature=MagickSignature;
387
388 /* Name */
389 (void) CopyMagickString(buffer,value_name,MaxTextExtent);
390 for(pos = buffer; *pos != 0 ; pos++)
391 if (*pos == ' ')
392 *pos = '-';
393 type_info->name=ConstantString(buffer);
394
395 /* Fullname */
396 type_info->description=ConstantString(value_name);
397
398 /* Format */
399 type_info->format=ConstantString("truetype");
400
401 /* Glyphs */
402 if (strchr(value_data,'\\') != (char *) NULL)
403 (void) CopyMagickString(buffer,value_data,MaxTextExtent);
404 else
405 {
406 (void) CopyMagickString(buffer,font_root,MaxTextExtent);
407 (void) ConcatenateMagickString(buffer,value_data,MaxTextExtent);
408 }
409
410 LocaleLower(buffer);
411 type_info->glyphs=ConstantString(buffer);
412
413 type_info->stretch=NormalStretch;
414 type_info->style=NormalStyle;
415 type_info->weight=400;
416
417 /* Some fonts are known to require special encodings */
418 if ( (LocaleCompare(type_info->name, "Symbol") == 0 ) ||
419 (LocaleCompare(type_info->name, "Wingdings") == 0 ) ||
420 (LocaleCompare(type_info->name, "Wingdings-2") == 0 ) ||
421 (LocaleCompare(type_info->name, "Wingdings-3") == 0 ) )
422 type_info->encoding=ConstantString("AppleRoman");
423
424 family_extent=value_name;
425
426 for (q=value_name; *q != '\0'; )
427 {
428 GetMagickToken(q,(const char **) &q,token);
429 if (*token == '\0')
430 break;
431
432 if (LocaleCompare(token,"Italic") == 0)
433 {
434 type_info->style=ItalicStyle;
435 }
436
437 else if (LocaleCompare(token,"Oblique") == 0)
438 {
439 type_info->style=ObliqueStyle;
440 }
441
442 else if (LocaleCompare(token,"Bold") == 0)
443 {
444 type_info->weight=700;
445 }
446
447 else if (LocaleCompare(token,"Thin") == 0)
448 {
449 type_info->weight=100;
450 }
451
452 else if ( (LocaleCompare(token,"ExtraLight") == 0) ||
453 (LocaleCompare(token,"UltraLight") == 0) )
454 {
455 type_info->weight=200;
456 }
457
458 else if (LocaleCompare(token,"Light") == 0)
459 {
460 type_info->weight=300;
461 }
462
463 else if ( (LocaleCompare(token,"Normal") == 0) ||
464 (LocaleCompare(token,"Regular") == 0) )
465 {
466 type_info->weight=400;
467 }
468
469 else if (LocaleCompare(token,"Medium") == 0)
470 {
471 type_info->weight=500;
472 }
473
474 else if ( (LocaleCompare(token,"SemiBold") == 0) ||
475 (LocaleCompare(token,"DemiBold") == 0) )
476 {
477 type_info->weight=600;
478 }
479
480 else if ( (LocaleCompare(token,"ExtraBold") == 0) ||
481 (LocaleCompare(token,"UltraBold") == 0) )
482 {
483 type_info->weight=800;
484 }
485
486 else if ( (LocaleCompare(token,"Heavy") == 0) ||
487 (LocaleCompare(token,"Black") == 0) )
488 {
489 type_info->weight=900;
490 }
491
492 else if (LocaleCompare(token,"Condensed") == 0)
493 {
494 type_info->stretch = CondensedStretch;
495 }
496
497 else if (LocaleCompare(token,"Expanded") == 0)
498 {
499 type_info->stretch = ExpandedStretch;
500 }
501
502 else if (LocaleCompare(token,"ExtraCondensed") == 0)
503 {
504 type_info->stretch = ExtraCondensedStretch;
505 }
506
507 else if (LocaleCompare(token,"ExtraExpanded") == 0)
508 {
509 type_info->stretch = ExtraExpandedStretch;
510 }
511
512 else if (LocaleCompare(token,"SemiCondensed") == 0)
513 {
514 type_info->stretch = SemiCondensedStretch;
515 }
516
517 else if (LocaleCompare(token,"SemiExpanded") == 0)
518 {
519 type_info->stretch = SemiExpandedStretch;
520 }
521
522 else if (LocaleCompare(token,"UltraCondensed") == 0)
523 {
524 type_info->stretch = UltraCondensedStretch;
525 }
526
527 else if (LocaleCompare(token,"UltraExpanded") == 0)
528 {
529 type_info->stretch = UltraExpandedStretch;
530 }
531
532 else
533 {
534 family_extent=q;
535 }
536 }
537
538 (void) CopyMagickString(buffer,value_name,family_extent-value_name+1);
539 StripString(buffer);
540 type_info->family=ConstantString(buffer);
541
542 list_entries++;
543 status=AddValueToSplayTree(type_list,ConstantString(type_info->name),
544 type_info);
545 if (status == MagickFalse)
546 (void) ThrowMagickException(exception,GetMagickModule(),
547 ResourceLimitError,"MemoryAllocationFailed","`%s'",type_info->name);
548 }
549 }
550 RegCloseKey ( reg_key );
551 return(MagickTrue);
552}
553
554/*
555%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556% %
557% %
558% %
559% I m a g e T o H B i t m a p %
560% %
561% %
562% %
563%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564%
565% ImageToHBITMAP() creates a Windows HBITMAP from an image.
566%
567% The format of the ImageToHBITMAP method is:
568%
569% HBITMAP ImageToHBITMAP(Image *image,Exceptioninfo *exception)
570%
571% A description of each parameter follows:
572%
573% o image: the image to convert.
574%
575*/
576MagickExport void *ImageToHBITMAP(Image *image,ExceptionInfo *exception)
577{
578 BITMAP
579 bitmap;
580
581 HANDLE
582 bitmap_bitsH;
583
584 HBITMAP
585 bitmapH;
586
587 register ssize_t
588 x;
589
cristy309042f2012-02-20 18:59:35 +0000590 register const Quantum
cristyf7836bf2012-02-20 16:32:47 +0000591 *p;
592
593 register RGBQUAD
594 *q;
595
596 RGBQUAD
597 *bitmap_bits;
598
599 size_t
600 length;
601
602 ssize_t
603 y;
604
605 (void) ResetMagickMemory(&bitmap,0,sizeof(bitmap));
606 bitmap.bmType=0;
607 bitmap.bmWidth=(LONG) image->columns;
608 bitmap.bmHeight=(LONG) image->rows;
609 bitmap.bmWidthBytes=4*bitmap.bmWidth;
610 bitmap.bmPlanes=1;
611 bitmap.bmBitsPixel=32;
612 bitmap.bmBits=NULL;
613 length=bitmap.bmWidthBytes*bitmap.bmHeight;
614 bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,length);
615 if (bitmap_bitsH == NULL)
616 {
617 char
618 *message;
619
620 message=GetExceptionMessage(errno);
621 (void) ThrowMagickException(exception,GetMagickModule(),
622 ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
623 message=DestroyString(message);
624 return(NULL);
625 }
626 bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
627 q=bitmap_bits;
628 if (bitmap.bmBits == NULL)
629 bitmap.bmBits=bitmap_bits;
cristy309042f2012-02-20 18:59:35 +0000630 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristyf7836bf2012-02-20 16:32:47 +0000631 for (y=0; y < (ssize_t) image->rows; y++)
632 {
633 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy309042f2012-02-20 18:59:35 +0000634 if (p == (const Quantum *) NULL)
cristyf7836bf2012-02-20 16:32:47 +0000635 break;
636 for (x=0; x < (ssize_t) image->columns; x++)
637 {
cristy309042f2012-02-20 18:59:35 +0000638 q->rgbRed=ScaleQuantumToChar(GetPixelRed(image,p));
639 q->rgbGreen=ScaleQuantumToChar(GetPixelGreen(image,p));
640 q->rgbBlue=ScaleQuantumToChar(GetPixelBlue(image,p));
cristyf7836bf2012-02-20 16:32:47 +0000641 q->rgbReserved=0;
cristy309042f2012-02-20 18:59:35 +0000642 p+=GetPixelChannels(image);
cristyf7836bf2012-02-20 16:32:47 +0000643 q++;
644 }
645 }
646 bitmap.bmBits=bitmap_bits;
647 bitmapH=CreateBitmapIndirect(&bitmap);
648 if (bitmapH == NULL)
649 {
650 char
651 *message;
652
653 message=GetExceptionMessage(errno);
654 (void) ThrowMagickException(exception,GetMagickModule(),
655 ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
656 message=DestroyString(message);
657 }
658 GlobalUnlock((HGLOBAL) bitmap_bitsH);
659 GlobalFree((HGLOBAL) bitmap_bitsH);
660 return((void *) bitmapH);
661}
662
663#endif