2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 % Windows NT Feature Methods for MagickCore %
21 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
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. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #if defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
45 #define WIN32_LEAN_AND_MEAN
48 #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"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/quantum.h"
60 #include "MagickCore/string_.h"
61 #include "MagickCore/token.h"
62 #include "MagickCore/splay-tree.h"
63 #include "MagickCore/utility.h"
64 #include "MagickCore/nt-feature.h"
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 % C r o p I m a g e T o H B i t m a p %
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 % CropImageToHBITMAP() extracts a specified region of the image and returns
78 % it as a Windows HBITMAP. While the same functionality can be accomplished by
79 % invoking CropImage() followed by ImageToHBITMAP(), this method is more
80 % efficient since it copies pixels directly to the HBITMAP.
82 % The format of the CropImageToHBITMAP method is:
84 % HBITMAP CropImageToHBITMAP(Image* image,const RectangleInfo *geometry,
85 % ExceptionInfo *exception)
87 % A description of each parameter follows:
91 % o geometry: Define the region of the image to crop with members
92 % x, y, width, and height.
94 % o exception: return any errors or warnings in this structure.
97 MagickExport void *CropImageToHBITMAP(Image *image,
98 const RectangleInfo *geometry,ExceptionInfo *exception)
100 #define CropImageTag "Crop/Image"
117 register const Quantum
132 assert(image != (const Image *) NULL);
133 assert(image->signature == MagickSignature);
134 if (image->debug != MagickFalse)
135 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
136 assert(geometry != (const RectangleInfo *) NULL);
137 assert(exception != (ExceptionInfo *) NULL);
138 assert(exception->signature == MagickSignature);
139 if (((geometry->x+(ssize_t) geometry->width) < 0) ||
140 ((geometry->y+(ssize_t) geometry->height) < 0) ||
141 (geometry->x >= (ssize_t) image->columns) ||
142 (geometry->y >= (ssize_t) image->rows))
143 ThrowImageException(OptionError,"GeometryDoesNotContainImage");
145 if ((page.x+(ssize_t) page.width) > (ssize_t) image->columns)
146 page.width=image->columns-page.x;
147 if ((page.y+(ssize_t) page.height) > (ssize_t) image->rows)
148 page.height=image->rows-page.y;
160 if ((page.width == 0) || (page.height == 0))
161 ThrowImageException(OptionError,"GeometryDimensionsAreZero");
163 Initialize crop image attributes.
166 bitmap.bmWidth = (LONG) page.width;
167 bitmap.bmHeight = (LONG) page.height;
168 bitmap.bmWidthBytes = bitmap.bmWidth * 4;
170 bitmap.bmBitsPixel = 32;
171 bitmap.bmBits = NULL;
173 bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,page.width*
174 page.height*bitmap.bmBitsPixel);
175 if (bitmap_bitsH == NULL)
177 bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
178 if ( bitmap.bmBits == NULL )
179 bitmap.bmBits = bitmap_bits;
180 if (IsRGBColorspace(image->colorspace) == MagickFalse)
181 TransformImageColorspace(image,RGBColorspace);
186 for (y=0; y < (ssize_t) page.height; y++)
188 p=GetVirtualPixels(image,page.x,page.y+y,page.width,1,exception);
189 if (p == (const Quantum *) NULL)
192 #if MAGICKCORE_QUANTUM_DEPTH == 8
193 /* Form of PixelPacket is identical to RGBQUAD when MAGICKCORE_QUANTUM_DEPTH==8 */
194 CopyMagickMemory((void*)q,(const void*)p,page.width*sizeof(PixelPacket));
197 #else /* 16 or 32 bit Quantum */
202 /* Transfer pixels, scaling to Quantum */
203 for( x=(ssize_t) page.width ; x> 0 ; x-- )
205 q->rgbRed = ScaleQuantumToChar(GetPixelRed(image,p));
206 q->rgbGreen = ScaleQuantumToChar(GetPixelGreen(image,p));
207 q->rgbBlue = ScaleQuantumToChar(GetPixelBlue(image,p));
214 proceed=SetImageProgress(image,CropImageTag,y,page.height);
215 if (proceed == MagickFalse)
218 if (y < (ssize_t) page.height)
220 GlobalUnlock((HGLOBAL) bitmap_bitsH);
221 GlobalFree((HGLOBAL) bitmap_bitsH);
222 return((void *) NULL);
224 bitmap.bmBits=bitmap_bits;
225 bitmapH=CreateBitmapIndirect(&bitmap);
226 GlobalUnlock((HGLOBAL) bitmap_bitsH);
227 GlobalFree((HGLOBAL) bitmap_bitsH);
228 return((void *) bitmapH);
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % I s M a g i c k C o n f l i c t %
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % IsMagickConflict() returns true if the image format conflicts with a logical
245 % The format of the IsMagickConflict method is:
247 % MagickBooleanType IsMagickConflict(const char *magick)
249 % A description of each parameter follows:
251 % o magick: Specifies the image format.
254 MagickExport MagickBooleanType NTIsMagickConflict(const char *magick)
259 assert(magick != (char *) NULL);
260 if (strlen(magick) > 1)
262 status=(GetLogicalDrives() & (1 << ((toupper((int) (*magick)))-'A'))) != 0 ?
263 MagickTrue : MagickFalse;
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 + N T G e t T y pe L i s t %
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 % NTLoadTypeLists() loads a Windows TrueType fonts.
280 % The format of the NTLoadTypeLists method is:
282 % MagickBooleanType NTLoadTypeLists(SplayTreeInfo *type_list)
284 % A description of each parameter follows:
286 % o type_list: A linked list of fonts.
289 MagickExport MagickBooleanType NTLoadTypeLists(SplayTreeInfo *type_list,
290 ExceptionInfo *exception)
293 reg_key = (HKEY) INVALID_HANDLE_VALUE;
303 buffer[MaxTextExtent],
304 system_root[MaxTextExtent],
305 font_root[MaxTextExtent];
315 Try to find the right Windows*\CurrentVersion key, the SystemRoot and
318 res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
319 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, ®_key);
320 if (res == ERROR_SUCCESS) {
321 system_root_length=sizeof(system_root)-1;
322 res = RegQueryValueExA(reg_key,"SystemRoot",NULL, &type,
323 (BYTE*) system_root, &system_root_length);
325 if (res != ERROR_SUCCESS) {
326 res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
327 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", 0, KEY_READ, ®_key);
328 if (res == ERROR_SUCCESS) {
329 system_root_length=sizeof(system_root)-1;
330 res = RegQueryValueExA(reg_key,"SystemRoot",NULL, &type,
331 (BYTE*)system_root, &system_root_length);
334 if (res == ERROR_SUCCESS)
335 res = RegOpenKeyExA (reg_key, "Fonts",0, KEY_READ, ®_key);
336 if (res != ERROR_SUCCESS)
339 (void) CopyMagickString(buffer,system_root,MaxTextExtent);
340 (void) ConcatenateMagickString(buffer,"\\fonts\\arial.ttf",MaxTextExtent);
341 if (IsPathAccessible(buffer) != MagickFalse)
343 (void) CopyMagickString(font_root,system_root,MaxTextExtent);
344 (void) ConcatenateMagickString(font_root,"\\fonts\\",MaxTextExtent);
348 (void) CopyMagickString(font_root,system_root,MaxTextExtent);
349 (void) ConcatenateMagickString(font_root,"\\",MaxTextExtent);
363 value_data[MaxTextExtent],
364 value_name[MaxTextExtent];
368 while (res != ERROR_NO_MORE_ITEMS)
372 token[MaxTextExtent],
376 value_name_length = sizeof(value_name) - 1;
377 value_data_size = sizeof(value_data) - 1;
378 res = RegEnumValueA ( reg_key, registry_index, value_name,
379 &value_name_length, 0, &type, (BYTE*)value_data, &value_data_size);
381 if (res != ERROR_SUCCESS)
383 if ( (pos = strstr(value_name, " (TrueType)")) == (char*) NULL )
385 *pos='\0'; /* Remove (TrueType) from string */
387 type_info=(TypeInfo *) AcquireMagickMemory(sizeof(*type_info));
388 if (type_info == (TypeInfo *) NULL)
389 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
390 (void) ResetMagickMemory(type_info,0,sizeof(TypeInfo));
392 type_info->path=ConstantString("Windows Fonts");
393 type_info->signature=MagickSignature;
396 (void) CopyMagickString(buffer,value_name,MaxTextExtent);
397 for(pos = buffer; *pos != 0 ; pos++)
400 type_info->name=ConstantString(buffer);
403 type_info->description=ConstantString(value_name);
406 type_info->format=ConstantString("truetype");
409 if (strchr(value_data,'\\') != (char *) NULL)
410 (void) CopyMagickString(buffer,value_data,MaxTextExtent);
413 (void) CopyMagickString(buffer,font_root,MaxTextExtent);
414 (void) ConcatenateMagickString(buffer,value_data,MaxTextExtent);
418 type_info->glyphs=ConstantString(buffer);
420 type_info->stretch=NormalStretch;
421 type_info->style=NormalStyle;
422 type_info->weight=400;
424 /* Some fonts are known to require special encodings */
425 if ( (LocaleCompare(type_info->name, "Symbol") == 0 ) ||
426 (LocaleCompare(type_info->name, "Wingdings") == 0 ) ||
427 (LocaleCompare(type_info->name, "Wingdings-2") == 0 ) ||
428 (LocaleCompare(type_info->name, "Wingdings-3") == 0 ) )
429 type_info->encoding=ConstantString("AppleRoman");
431 family_extent=value_name;
433 for (q=value_name; *q != '\0'; )
435 GetMagickToken(q,(const char **) &q,token);
439 if (LocaleCompare(token,"Italic") == 0)
441 type_info->style=ItalicStyle;
444 else if (LocaleCompare(token,"Oblique") == 0)
446 type_info->style=ObliqueStyle;
449 else if (LocaleCompare(token,"Bold") == 0)
451 type_info->weight=700;
454 else if (LocaleCompare(token,"Thin") == 0)
456 type_info->weight=100;
459 else if ( (LocaleCompare(token,"ExtraLight") == 0) ||
460 (LocaleCompare(token,"UltraLight") == 0) )
462 type_info->weight=200;
465 else if (LocaleCompare(token,"Light") == 0)
467 type_info->weight=300;
470 else if ( (LocaleCompare(token,"Normal") == 0) ||
471 (LocaleCompare(token,"Regular") == 0) )
473 type_info->weight=400;
476 else if (LocaleCompare(token,"Medium") == 0)
478 type_info->weight=500;
481 else if ( (LocaleCompare(token,"SemiBold") == 0) ||
482 (LocaleCompare(token,"DemiBold") == 0) )
484 type_info->weight=600;
487 else if ( (LocaleCompare(token,"ExtraBold") == 0) ||
488 (LocaleCompare(token,"UltraBold") == 0) )
490 type_info->weight=800;
493 else if ( (LocaleCompare(token,"Heavy") == 0) ||
494 (LocaleCompare(token,"Black") == 0) )
496 type_info->weight=900;
499 else if (LocaleCompare(token,"Condensed") == 0)
501 type_info->stretch = CondensedStretch;
504 else if (LocaleCompare(token,"Expanded") == 0)
506 type_info->stretch = ExpandedStretch;
509 else if (LocaleCompare(token,"ExtraCondensed") == 0)
511 type_info->stretch = ExtraCondensedStretch;
514 else if (LocaleCompare(token,"ExtraExpanded") == 0)
516 type_info->stretch = ExtraExpandedStretch;
519 else if (LocaleCompare(token,"SemiCondensed") == 0)
521 type_info->stretch = SemiCondensedStretch;
524 else if (LocaleCompare(token,"SemiExpanded") == 0)
526 type_info->stretch = SemiExpandedStretch;
529 else if (LocaleCompare(token,"UltraCondensed") == 0)
531 type_info->stretch = UltraCondensedStretch;
534 else if (LocaleCompare(token,"UltraExpanded") == 0)
536 type_info->stretch = UltraExpandedStretch;
545 (void) CopyMagickString(buffer,value_name,family_extent-value_name+1);
547 type_info->family=ConstantString(buffer);
550 status=AddValueToSplayTree(type_list,ConstantString(type_info->name),
552 if (status == MagickFalse)
553 (void) ThrowMagickException(exception,GetMagickModule(),
554 ResourceLimitError,"MemoryAllocationFailed","`%s'",type_info->name);
557 RegCloseKey ( reg_key );
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % I m a g e T o H B i t m a p %
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572 % ImageToHBITMAP() creates a Windows HBITMAP from an image.
574 % The format of the ImageToHBITMAP method is:
576 % HBITMAP ImageToHBITMAP(Image *image)
578 % A description of each parameter follows:
580 % o image: the image to convert.
583 MagickExport void *ImageToHBITMAP(Image *image)
600 register const Quantum
615 (void) ResetMagickMemory(&bitmap,0,sizeof(bitmap));
617 bitmap.bmWidth=(LONG) image->columns;
618 bitmap.bmHeight=(LONG) image->rows;
619 bitmap.bmWidthBytes=4*bitmap.bmWidth;
621 bitmap.bmBitsPixel=32;
623 length=bitmap.bmWidthBytes*bitmap.bmHeight;
624 bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,length);
625 if (bitmap_bitsH == NULL)
630 message=GetExceptionMessage(errno);
631 (void) ThrowMagickException(&image->exception,GetMagickModule(),
632 ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
633 message=DestroyString(message);
636 bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
638 if (bitmap.bmBits == NULL)
639 bitmap.bmBits=bitmap_bits;
640 (void) TransformImageColorspace(image,RGBColorspace);
641 exception=(&image->exception);
642 for (y=0; y < (ssize_t) image->rows; y++)
644 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
645 if (p == (const Quantum *) NULL)
647 for (x=0; x < (ssize_t) image->columns; x++)
649 q->rgbRed=ScaleQuantumToChar(GetPixelRed(image,p));
650 q->rgbGreen=ScaleQuantumToChar(GetPixelGreen(image,p));
651 q->rgbBlue=ScaleQuantumToChar(GetPixelBlue(image,p));
653 p+=GetPixelChannels(image);
657 bitmap.bmBits=bitmap_bits;
658 bitmapH=CreateBitmapIndirect(&bitmap);
664 message=GetExceptionMessage(errno);
665 (void) ThrowMagickException(&image->exception,GetMagickModule(),
666 ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
667 message=DestroyString(message);
669 GlobalUnlock((HGLOBAL) bitmap_bitsH);
670 GlobalFree((HGLOBAL) bitmap_bitsH);
671 return((void *) bitmapH);