2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 % Windows NT Feature Methods for MagickCore %
21 % Copyright 1999-2012 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"
66 #include "MagickCore/nt-base-private.h"
69 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 % C r o p I m a g e T o H B i t m a p %
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
84 % The format of the CropImageToHBITMAP method is:
86 % HBITMAP CropImageToHBITMAP(Image* image,const RectangleInfo *geometry,
87 % ExceptionInfo *exception)
89 % A description of each parameter follows:
93 % o geometry: Define the region of the image to crop with members
94 % x, y, width, and height.
96 % o exception: return any errors or warnings in this structure.
99 MagickExport void *CropImageToHBITMAP(Image *image,
100 const RectangleInfo *geometry,ExceptionInfo *exception)
102 #define CropImageTag "Crop/Image"
119 register const Quantum
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");
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;
162 if ((page.width == 0) || (page.height == 0))
163 ThrowImageException(OptionError,"GeometryDimensionsAreZero");
165 Initialize crop image attributes.
168 bitmap.bmWidth = (LONG) page.width;
169 bitmap.bmHeight = (LONG) page.height;
170 bitmap.bmWidthBytes = bitmap.bmWidth * 4;
172 bitmap.bmBitsPixel = 32;
173 bitmap.bmBits = NULL;
175 bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,page.width*
176 page.height*bitmap.bmBitsPixel);
177 if (bitmap_bitsH == NULL)
179 bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
180 if ( bitmap.bmBits == NULL )
181 bitmap.bmBits = bitmap_bits;
182 if ((IssRGBColorspace(image->colorspace) == MagickFalse) &&
183 (IsGrayColorspace(image->colorspace) == MagickFalse))
184 TransformImageColorspace(image,sRGBColorspace,exception);
189 for (y=0; y < (ssize_t) page.height; y++)
194 p=GetVirtualPixels(image,page.x,page.y+y,page.width,1,exception);
195 if (p == (const Quantum *) NULL)
198 /* Transfer pixels, scaling to Quantum */
199 for( x=(ssize_t) page.width ; x> 0 ; x-- )
201 q->rgbRed = ScaleQuantumToChar(GetPixelRed(image,p));
202 q->rgbGreen = ScaleQuantumToChar(GetPixelGreen(image,p));
203 q->rgbBlue = ScaleQuantumToChar(GetPixelBlue(image,p));
205 p+=GetPixelChannels(image);
208 proceed=SetImageProgress(image,CropImageTag,y,page.height);
209 if (proceed == MagickFalse)
212 if (y < (ssize_t) page.height)
214 GlobalUnlock((HGLOBAL) bitmap_bitsH);
215 GlobalFree((HGLOBAL) bitmap_bitsH);
216 return((void *) NULL);
218 bitmap.bmBits=bitmap_bits;
219 bitmapH=CreateBitmapIndirect(&bitmap);
220 GlobalUnlock((HGLOBAL) bitmap_bitsH);
221 GlobalFree((HGLOBAL) bitmap_bitsH);
222 return((void *) bitmapH);
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 % I s M a g i c k C o n f l i c t %
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % IsMagickConflict() returns true if the image format conflicts with a logical
239 % The format of the IsMagickConflict method is:
241 % MagickBooleanType IsMagickConflict(const char *magick)
243 % A description of each parameter follows:
245 % o magick: Specifies the image format.
248 MagickExport MagickBooleanType NTIsMagickConflict(const char *magick)
253 assert(magick != (char *) NULL);
254 if (strlen(magick) > 1)
256 status=(GetLogicalDrives() & (1 << ((toupper((int) (*magick)))-'A'))) != 0 ?
257 MagickTrue : MagickFalse;
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 + N T G e t T y pe L i s t %
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 % NTLoadTypeLists() loads a Windows TrueType fonts.
274 % The format of the NTLoadTypeLists method is:
276 % MagickBooleanType NTLoadTypeLists(SplayTreeInfo *type_list)
278 % A description of each parameter follows:
280 % o type_list: A linked list of fonts.
283 MagickExport MagickBooleanType NTLoadTypeLists(SplayTreeInfo *type_list,
284 ExceptionInfo *exception)
287 reg_key = (HKEY) INVALID_HANDLE_VALUE;
297 buffer[MaxTextExtent],
298 system_root[MaxTextExtent],
299 font_root[MaxTextExtent];
309 Try to find the right Windows*\CurrentVersion key, the SystemRoot and
312 res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
313 "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, ®_key);
314 if (res == ERROR_SUCCESS) {
315 system_root_length=sizeof(system_root)-1;
316 res = RegQueryValueExA(reg_key,"SystemRoot",NULL, &type,
317 (BYTE*) system_root, &system_root_length);
319 if (res != ERROR_SUCCESS) {
320 res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
321 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion", 0, KEY_READ, ®_key);
322 if (res == ERROR_SUCCESS) {
323 system_root_length=sizeof(system_root)-1;
324 res = RegQueryValueExA(reg_key,"SystemRoot",NULL, &type,
325 (BYTE*)system_root, &system_root_length);
328 if (res == ERROR_SUCCESS)
329 res = RegOpenKeyExA (reg_key, "Fonts",0, KEY_READ, ®_key);
330 if (res != ERROR_SUCCESS)
333 (void) CopyMagickString(buffer,system_root,MaxTextExtent);
334 (void) ConcatenateMagickString(buffer,"\\fonts\\arial.ttf",MaxTextExtent);
335 if (IsPathAccessible(buffer) != MagickFalse)
337 (void) CopyMagickString(font_root,system_root,MaxTextExtent);
338 (void) ConcatenateMagickString(font_root,"\\fonts\\",MaxTextExtent);
342 (void) CopyMagickString(font_root,system_root,MaxTextExtent);
343 (void) ConcatenateMagickString(font_root,"\\",MaxTextExtent);
357 value_data[MaxTextExtent],
358 value_name[MaxTextExtent];
362 while (res != ERROR_NO_MORE_ITEMS)
366 token[MaxTextExtent],
370 value_name_length = sizeof(value_name) - 1;
371 value_data_size = sizeof(value_data) - 1;
372 res = RegEnumValueA ( reg_key, registry_index, value_name,
373 &value_name_length, 0, &type, (BYTE*)value_data, &value_data_size);
375 if (res != ERROR_SUCCESS)
377 if ( (pos = strstr(value_name, " (TrueType)")) == (char*) NULL )
379 *pos='\0'; /* Remove (TrueType) from string */
381 type_info=(TypeInfo *) AcquireMagickMemory(sizeof(*type_info));
382 if (type_info == (TypeInfo *) NULL)
383 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
384 (void) ResetMagickMemory(type_info,0,sizeof(TypeInfo));
386 type_info->path=ConstantString("Windows Fonts");
387 type_info->signature=MagickSignature;
390 (void) CopyMagickString(buffer,value_name,MaxTextExtent);
391 for(pos = buffer; *pos != 0 ; pos++)
394 type_info->name=ConstantString(buffer);
397 type_info->description=ConstantString(value_name);
400 type_info->format=ConstantString("truetype");
403 if (strchr(value_data,'\\') != (char *) NULL)
404 (void) CopyMagickString(buffer,value_data,MaxTextExtent);
407 (void) CopyMagickString(buffer,font_root,MaxTextExtent);
408 (void) ConcatenateMagickString(buffer,value_data,MaxTextExtent);
412 type_info->glyphs=ConstantString(buffer);
414 type_info->stretch=NormalStretch;
415 type_info->style=NormalStyle;
416 type_info->weight=400;
418 /* Some fonts are known to require special encodings */
419 if ( (LocaleCompare(type_info->name, "Symbol") == 0 ) ||
420 (LocaleCompare(type_info->name, "Wingdings") == 0 ) ||
421 (LocaleCompare(type_info->name, "Wingdings-2") == 0 ) ||
422 (LocaleCompare(type_info->name, "Wingdings-3") == 0 ) )
423 type_info->encoding=ConstantString("AppleRoman");
425 family_extent=value_name;
427 for (q=value_name; *q != '\0'; )
429 GetMagickToken(q,(const char **) &q,token);
433 if (LocaleCompare(token,"Italic") == 0)
435 type_info->style=ItalicStyle;
438 else if (LocaleCompare(token,"Oblique") == 0)
440 type_info->style=ObliqueStyle;
443 else if (LocaleCompare(token,"Bold") == 0)
445 type_info->weight=700;
448 else if (LocaleCompare(token,"Thin") == 0)
450 type_info->weight=100;
453 else if ( (LocaleCompare(token,"ExtraLight") == 0) ||
454 (LocaleCompare(token,"UltraLight") == 0) )
456 type_info->weight=200;
459 else if (LocaleCompare(token,"Light") == 0)
461 type_info->weight=300;
464 else if ( (LocaleCompare(token,"Normal") == 0) ||
465 (LocaleCompare(token,"Regular") == 0) )
467 type_info->weight=400;
470 else if (LocaleCompare(token,"Medium") == 0)
472 type_info->weight=500;
475 else if ( (LocaleCompare(token,"SemiBold") == 0) ||
476 (LocaleCompare(token,"DemiBold") == 0) )
478 type_info->weight=600;
481 else if ( (LocaleCompare(token,"ExtraBold") == 0) ||
482 (LocaleCompare(token,"UltraBold") == 0) )
484 type_info->weight=800;
487 else if ( (LocaleCompare(token,"Heavy") == 0) ||
488 (LocaleCompare(token,"Black") == 0) )
490 type_info->weight=900;
493 else if (LocaleCompare(token,"Condensed") == 0)
495 type_info->stretch = CondensedStretch;
498 else if (LocaleCompare(token,"Expanded") == 0)
500 type_info->stretch = ExpandedStretch;
503 else if (LocaleCompare(token,"ExtraCondensed") == 0)
505 type_info->stretch = ExtraCondensedStretch;
508 else if (LocaleCompare(token,"ExtraExpanded") == 0)
510 type_info->stretch = ExtraExpandedStretch;
513 else if (LocaleCompare(token,"SemiCondensed") == 0)
515 type_info->stretch = SemiCondensedStretch;
518 else if (LocaleCompare(token,"SemiExpanded") == 0)
520 type_info->stretch = SemiExpandedStretch;
523 else if (LocaleCompare(token,"UltraCondensed") == 0)
525 type_info->stretch = UltraCondensedStretch;
528 else if (LocaleCompare(token,"UltraExpanded") == 0)
530 type_info->stretch = UltraExpandedStretch;
539 (void) CopyMagickString(buffer,value_name,family_extent-value_name+1);
541 type_info->family=ConstantString(buffer);
544 status=AddValueToSplayTree(type_list,ConstantString(type_info->name),
546 if (status == MagickFalse)
547 (void) ThrowMagickException(exception,GetMagickModule(),
548 ResourceLimitError,"MemoryAllocationFailed","'%s'",type_info->name);
551 RegCloseKey ( reg_key );
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % I m a g e T o H B i t m a p %
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % ImageToHBITMAP() creates a Windows HBITMAP from an image.
568 % The format of the ImageToHBITMAP method is:
570 % HBITMAP ImageToHBITMAP(Image *image,Exceptioninfo *exception)
572 % A description of each parameter follows:
574 % o image: the image to convert.
577 MagickExport void *ImageToHBITMAP(Image *image,ExceptionInfo *exception)
591 register const Quantum
606 (void) ResetMagickMemory(&bitmap,0,sizeof(bitmap));
608 bitmap.bmWidth=(LONG) image->columns;
609 bitmap.bmHeight=(LONG) image->rows;
610 bitmap.bmWidthBytes=4*bitmap.bmWidth;
612 bitmap.bmBitsPixel=32;
614 length=bitmap.bmWidthBytes*bitmap.bmHeight;
615 bitmap_bitsH=(HANDLE) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,length);
616 if (bitmap_bitsH == NULL)
621 message=GetExceptionMessage(errno);
622 (void) ThrowMagickException(exception,GetMagickModule(),
623 ResourceLimitError,"MemoryAllocationFailed","'%s'",message);
624 message=DestroyString(message);
627 bitmap_bits=(RGBQUAD *) GlobalLock((HGLOBAL) bitmap_bitsH);
629 if (bitmap.bmBits == NULL)
630 bitmap.bmBits=bitmap_bits;
631 (void) TransformImageColorspace(image,sRGBColorspace,exception);
632 for (y=0; y < (ssize_t) image->rows; y++)
634 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
635 if (p == (const Quantum *) NULL)
637 for (x=0; x < (ssize_t) image->columns; x++)
639 q->rgbRed=ScaleQuantumToChar(GetPixelRed(image,p));
640 q->rgbGreen=ScaleQuantumToChar(GetPixelGreen(image,p));
641 q->rgbBlue=ScaleQuantumToChar(GetPixelBlue(image,p));
643 p+=GetPixelChannels(image);
647 bitmap.bmBits=bitmap_bits;
648 bitmapH=CreateBitmapIndirect(&bitmap);
654 message=GetExceptionMessage(errno);
655 (void) ThrowMagickException(exception,GetMagickModule(),
656 ResourceLimitError,"MemoryAllocationFailed","'%s'",message);
657 message=DestroyString(message);
659 GlobalUnlock((HGLOBAL) bitmap_bitsH);
660 GlobalFree((HGLOBAL) bitmap_bitsH);
661 return((void *) bitmapH);