2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 % Windows NT Feature Methods for MagickCore %
21 % Copyright 1999-2014 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/nt-base.h"
59 #include "MagickCore/nt-base-private.h"
60 #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"
68 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 % C r o p I m a g e T o H B i t m a p %
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % CropImageToHBITMAP() extracts a specified region of the image and returns
79 % it as a Windows HBITMAP. While the same functionality can be accomplished by
80 % invoking CropImage() followed by ImageToHBITMAP(), this method is more
81 % efficient since it copies pixels directly to the HBITMAP.
83 % The format of the CropImageToHBITMAP method is:
85 % HBITMAP CropImageToHBITMAP(Image* image,const RectangleInfo *geometry,
86 % ExceptionInfo *exception)
88 % A description of each parameter follows:
92 % o geometry: Define the region of the image to crop with members
93 % x, y, width, and height.
95 % o exception: return any errors or warnings in this structure.
98 MagickExport void *CropImageToHBITMAP(Image *image,
99 const RectangleInfo *geometry,ExceptionInfo *exception)
101 #define CropImageTag "Crop/Image"
118 register const Quantum
133 assert(image != (const Image *) NULL);
134 assert(image->signature == MagickSignature);
135 if (image->debug != MagickFalse)
136 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
137 assert(geometry != (const RectangleInfo *) NULL);
138 assert(exception != (ExceptionInfo *) NULL);
139 assert(exception->signature == MagickSignature);
140 if (((geometry->x+(ssize_t) geometry->width) < 0) ||
141 ((geometry->y+(ssize_t) geometry->height) < 0) ||
142 (geometry->x >= (ssize_t) image->columns) ||
143 (geometry->y >= (ssize_t) image->rows))
144 ThrowImageException(OptionError,"GeometryDoesNotContainImage");
146 if ((page.x+(ssize_t) page.width) > (ssize_t) image->columns)
147 page.width=image->columns-page.x;
148 if ((page.y+(ssize_t) page.height) > (ssize_t) image->rows)
149 page.height=image->rows-page.y;
161 if ((page.width == 0) || (page.height == 0))
162 ThrowImageException(OptionError,"GeometryDimensionsAreZero");
164 Initialize crop image attributes.
167 bitmap.bmWidth = (LONG) page.width;
168 bitmap.bmHeight = (LONG) page.height;
169 bitmap.bmWidthBytes = bitmap.bmWidth * 4;
171 bitmap.bmBitsPixel = 32;
172 bitmap.bmBits = NULL;
174 bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,page.width*
175 page.height*bitmap.bmBitsPixel);
176 if (bitmap_bitsH == NULL)
178 bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
179 if ( bitmap.bmBits == NULL )
180 bitmap.bmBits = bitmap_bits;
181 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
182 SetImageColorspace(image,sRGBColorspace,exception);
187 for (y=0; y < (ssize_t) page.height; y++)
192 p=GetVirtualPixels(image,page.x,page.y+y,page.width,1,exception);
193 if (p == (const Quantum *) NULL)
196 /* Transfer pixels, scaling to Quantum */
197 for( x=(ssize_t) page.width ; x> 0 ; x-- )
199 q->rgbRed = ScaleQuantumToChar(GetPixelRed(image,p));
200 q->rgbGreen = ScaleQuantumToChar(GetPixelGreen(image,p));
201 q->rgbBlue = ScaleQuantumToChar(GetPixelBlue(image,p));
203 p+=GetPixelChannels(image);
206 proceed=SetImageProgress(image,CropImageTag,y,page.height);
207 if (proceed == MagickFalse)
210 if (y < (ssize_t) page.height)
212 GlobalUnlock((HGLOBAL) bitmap_bitsH);
213 GlobalFree((HGLOBAL) bitmap_bitsH);
214 return((void *) NULL);
216 bitmap.bmBits=bitmap_bits;
217 bitmapH=CreateBitmapIndirect(&bitmap);
218 GlobalUnlock((HGLOBAL) bitmap_bitsH);
219 GlobalFree((HGLOBAL) bitmap_bitsH);
220 return((void *) bitmapH);
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 % I s M a g i c k C o n f l i c t %
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % IsMagickConflict() returns true if the image format conflicts with a logical
237 % The format of the IsMagickConflict method is:
239 % MagickBooleanType IsMagickConflict(const char *magick)
241 % A description of each parameter follows:
243 % o magick: Specifies the image format.
246 MagickExport MagickBooleanType NTIsMagickConflict(const char *magick)
251 assert(magick != (char *) NULL);
252 if (strlen(magick) > 1)
254 status=(GetLogicalDrives() & (1 << ((toupper((int) (*magick)))-'A'))) != 0 ?
255 MagickTrue : MagickFalse;
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 + N T G e t T y pe L i s t %
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 % NTLoadTypeLists() loads a Windows TrueType fonts.
272 % The format of the NTLoadTypeLists method is:
274 % MagickBooleanType NTLoadTypeLists(SplayTreeInfo *type_list)
276 % A description of each parameter follows:
278 % o type_list: A linked list of fonts.
281 MagickExport MagickBooleanType NTLoadTypeLists(SplayTreeInfo *type_list,
282 ExceptionInfo *exception)
285 reg_key = (HKEY) INVALID_HANDLE_VALUE;
294 buffer[MaxTextExtent],
295 system_root[MaxTextExtent],
296 font_root[MaxTextExtent];
306 Try to find the right Windows*\CurrentVersion key, the SystemRoot and
309 res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
310 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, ®_key);
311 if (res == ERROR_SUCCESS) {
312 system_root_length=sizeof(system_root)-1;
313 res = RegQueryValueExA(reg_key,"SystemRoot",NULL, &type,
314 (BYTE*) system_root, &system_root_length);
316 if (res != ERROR_SUCCESS) {
317 res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
318 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", 0, KEY_READ, ®_key);
319 if (res == ERROR_SUCCESS) {
320 system_root_length=sizeof(system_root)-1;
321 res = RegQueryValueExA(reg_key,"SystemRoot",NULL, &type,
322 (BYTE*)system_root, &system_root_length);
325 if (res == ERROR_SUCCESS)
326 res = RegOpenKeyExA (reg_key, "Fonts",0, KEY_READ, ®_key);
327 if (res != ERROR_SUCCESS)
330 (void) CopyMagickString(buffer,system_root,MaxTextExtent);
331 (void) ConcatenateMagickString(buffer,"\\fonts\\arial.ttf",MaxTextExtent);
332 if (IsPathAccessible(buffer) != MagickFalse)
334 (void) CopyMagickString(font_root,system_root,MaxTextExtent);
335 (void) ConcatenateMagickString(font_root,"\\fonts\\",MaxTextExtent);
339 (void) CopyMagickString(font_root,system_root,MaxTextExtent);
340 (void) ConcatenateMagickString(font_root,"\\",MaxTextExtent);
354 value_data[MaxTextExtent],
355 value_name[MaxTextExtent];
359 while (res != ERROR_NO_MORE_ITEMS)
363 token[MaxTextExtent],
367 value_name_length = sizeof(value_name) - 1;
368 value_data_size = sizeof(value_data) - 1;
369 res = RegEnumValueA ( reg_key, registry_index, value_name,
370 &value_name_length, 0, &type, (BYTE*)value_data, &value_data_size);
372 if (res != ERROR_SUCCESS)
374 if ( (pos = strstr(value_name, " (TrueType)")) == (char*) NULL )
376 *pos='\0'; /* Remove (TrueType) from string */
378 type_info=(TypeInfo *) AcquireMagickMemory(sizeof(*type_info));
379 if (type_info == (TypeInfo *) NULL)
380 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
381 (void) ResetMagickMemory(type_info,0,sizeof(TypeInfo));
383 type_info->path=ConstantString("Windows Fonts");
384 type_info->signature=MagickSignature;
387 (void) CopyMagickString(buffer,value_name,MaxTextExtent);
388 for(pos = buffer; *pos != 0 ; pos++)
391 type_info->name=ConstantString(buffer);
394 type_info->description=ConstantString(value_name);
397 type_info->format=ConstantString("truetype");
400 if (strchr(value_data,'\\') != (char *) NULL)
401 (void) CopyMagickString(buffer,value_data,MaxTextExtent);
404 (void) CopyMagickString(buffer,font_root,MaxTextExtent);
405 (void) ConcatenateMagickString(buffer,value_data,MaxTextExtent);
409 type_info->glyphs=ConstantString(buffer);
411 type_info->stretch=NormalStretch;
412 type_info->style=NormalStyle;
413 type_info->weight=400;
415 /* Some fonts are known to require special encodings */
416 if ( (LocaleCompare(type_info->name, "Symbol") == 0 ) ||
417 (LocaleCompare(type_info->name, "Wingdings") == 0 ) ||
418 (LocaleCompare(type_info->name, "Wingdings-2") == 0 ) ||
419 (LocaleCompare(type_info->name, "Wingdings-3") == 0 ) )
420 type_info->encoding=ConstantString("AppleRoman");
422 family_extent=value_name;
424 for (q=value_name; *q != '\0'; )
426 GetMagickToken(q,(const char **) &q,token);
430 if (LocaleCompare(token,"Italic") == 0)
432 type_info->style=ItalicStyle;
435 else if (LocaleCompare(token,"Oblique") == 0)
437 type_info->style=ObliqueStyle;
440 else if (LocaleCompare(token,"Bold") == 0)
442 type_info->weight=700;
445 else if (LocaleCompare(token,"Thin") == 0)
447 type_info->weight=100;
450 else if ( (LocaleCompare(token,"ExtraLight") == 0) ||
451 (LocaleCompare(token,"UltraLight") == 0) )
453 type_info->weight=200;
456 else if (LocaleCompare(token,"Light") == 0)
458 type_info->weight=300;
461 else if ( (LocaleCompare(token,"Normal") == 0) ||
462 (LocaleCompare(token,"Regular") == 0) )
464 type_info->weight=400;
467 else if (LocaleCompare(token,"Medium") == 0)
469 type_info->weight=500;
472 else if ( (LocaleCompare(token,"SemiBold") == 0) ||
473 (LocaleCompare(token,"DemiBold") == 0) )
475 type_info->weight=600;
478 else if ( (LocaleCompare(token,"ExtraBold") == 0) ||
479 (LocaleCompare(token,"UltraBold") == 0) )
481 type_info->weight=800;
484 else if ( (LocaleCompare(token,"Heavy") == 0) ||
485 (LocaleCompare(token,"Black") == 0) )
487 type_info->weight=900;
490 else if (LocaleCompare(token,"Condensed") == 0)
492 type_info->stretch = CondensedStretch;
495 else if (LocaleCompare(token,"Expanded") == 0)
497 type_info->stretch = ExpandedStretch;
500 else if (LocaleCompare(token,"ExtraCondensed") == 0)
502 type_info->stretch = ExtraCondensedStretch;
505 else if (LocaleCompare(token,"ExtraExpanded") == 0)
507 type_info->stretch = ExtraExpandedStretch;
510 else if (LocaleCompare(token,"SemiCondensed") == 0)
512 type_info->stretch = SemiCondensedStretch;
515 else if (LocaleCompare(token,"SemiExpanded") == 0)
517 type_info->stretch = SemiExpandedStretch;
520 else if (LocaleCompare(token,"UltraCondensed") == 0)
522 type_info->stretch = UltraCondensedStretch;
525 else if (LocaleCompare(token,"UltraExpanded") == 0)
527 type_info->stretch = UltraExpandedStretch;
536 (void) CopyMagickString(buffer,value_name,family_extent-value_name+1);
538 type_info->family=ConstantString(buffer);
541 status=AddValueToSplayTree(type_list,ConstantString(type_info->name),
543 if (status == MagickFalse)
544 (void) ThrowMagickException(exception,GetMagickModule(),
545 ResourceLimitError,"MemoryAllocationFailed","`%s'",type_info->name);
548 RegCloseKey ( reg_key );
553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
557 % I m a g e T o H B i t m a p %
561 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 % ImageToHBITMAP() creates a Windows HBITMAP from an image.
565 % The format of the ImageToHBITMAP method is:
567 % HBITMAP ImageToHBITMAP(Image *image,Exceptioninfo *exception)
569 % A description of each parameter follows:
571 % o image: the image to convert.
574 MagickExport void *ImageToHBITMAP(Image *image,ExceptionInfo *exception)
588 register const Quantum
603 (void) ResetMagickMemory(&bitmap,0,sizeof(bitmap));
605 bitmap.bmWidth=(LONG) image->columns;
606 bitmap.bmHeight=(LONG) image->rows;
607 bitmap.bmWidthBytes=4*bitmap.bmWidth;
609 bitmap.bmBitsPixel=32;
611 length=bitmap.bmWidthBytes*bitmap.bmHeight;
612 bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,length);
613 if (bitmap_bitsH == NULL)
618 message=GetExceptionMessage(errno);
619 (void) ThrowMagickException(exception,GetMagickModule(),
620 ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
621 message=DestroyString(message);
624 bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
626 if (bitmap.bmBits == NULL)
627 bitmap.bmBits=bitmap_bits;
628 (void) SetImageColorspace(image,sRGBColorspace,exception);
629 for (y=0; y < (ssize_t) image->rows; y++)
631 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
632 if (p == (const Quantum *) NULL)
634 for (x=0; x < (ssize_t) image->columns; x++)
636 q->rgbRed=ScaleQuantumToChar(GetPixelRed(image,p));
637 q->rgbGreen=ScaleQuantumToChar(GetPixelGreen(image,p));
638 q->rgbBlue=ScaleQuantumToChar(GetPixelBlue(image,p));
640 p+=GetPixelChannels(image);
644 bitmap.bmBits=bitmap_bits;
645 bitmapH=CreateBitmapIndirect(&bitmap);
651 message=GetExceptionMessage(errno);
652 (void) ThrowMagickException(exception,GetMagickModule(),
653 ResourceLimitError,"MemoryAllocationFailed","`%s'",message);
654 message=DestroyString(message);
656 GlobalUnlock((HGLOBAL) bitmap_bitsH);
657 GlobalFree((HGLOBAL) bitmap_bitsH);
658 return((void *) bitmapH);