PageRenderTime 46ms CodeModel.GetById 2ms app.highlight 38ms RepoModel.GetById 2ms app.codeStats 0ms

/binding/win32/gdiplusbitmap.d

http://github.com/wilkie/djehuty
D | 712 lines | 475 code | 152 blank | 85 comment | 18 complexity | a313dbd271f532047e5ec055d6f42261 MD5 | raw file
  1/*
  2 * gdiplusbitmap.d
  3 *
  4 * This module implements GdiPlusBitmap.h for D. The original
  5 * copyright info is given below.
  6 *
  7 * Author: Dave Wilkinson
  8 * Originated: November 25th, 2009
  9 *
 10 */
 11
 12module binding.win32.gdiplusbitmap;
 13
 14import binding.win32.windef;
 15import binding.win32.winbase;
 16import binding.win32.winnt;
 17import binding.win32.wingdi;
 18import binding.win32.guiddef;
 19import binding.win32.gdiplusbase;
 20import binding.win32.gdiplustypes;
 21import binding.win32.gdiplusenums;
 22import binding.win32.gdipluspixelformats;
 23import binding.win32.gdiplusgpstubs;
 24import binding.win32.gdiplusmetaheader;
 25import binding.win32.gdipluspixelformats;
 26import binding.win32.gdipluscolor;
 27import binding.win32.gdipluscolormatrix;
 28import binding.win32.gdiplusflat;
 29import binding.win32.gdiplusimaging;
 30import binding.win32.gdiplusgraphics;
 31
 32class Image : GdiplusBase {
 33
 34    this(in WCHAR* filename, in BOOL useEmbeddedColorManagement = FALSE) {
 35	    nativeImage = null;
 36	    if(useEmbeddedColorManagement)
 37	    {
 38	        lastResult = GdipLoadImageFromFileICM(
 39	            filename, 
 40	            &nativeImage
 41	        );
 42	    }
 43	    else
 44	    {      
 45	        lastResult = GdipLoadImageFromFile(
 46	            filename, 
 47	            &nativeImage
 48	        );
 49	    }
 50    }
 51
 52    //this(in IStream* stream, in BOOL useEmbeddedColorManagement = FALSE) {
 53    //}
 54
 55    static Image FromFile(in WCHAR* filename, in BOOL useEmbeddedColorManagement = FALSE) {
 56	    return new Image(
 57	        filename, 
 58	        useEmbeddedColorManagement
 59	    );
 60    }
 61
 62//    static Image* FromStream(
 63//        in IStream* stream,
 64//        in BOOL useEmbeddedColorManagement = FALSE
 65//    );
 66
 67	~this() {
 68    	GdipDisposeImage(nativeImage);
 69	}
 70
 71    Image Clone() {
 72	    GpImage *cloneimage = null;
 73
 74	    SetStatus(GdipCloneImage(nativeImage, &cloneimage));
 75
 76	    return new Image(cloneimage, lastResult);
 77    }
 78
 79    Status Save(in WCHAR* filename,
 80                in CLSID* clsidEncoder,
 81                in EncoderParameters *encoderParams = null) {
 82	    return SetStatus(GdipSaveImageToFile(nativeImage,
 83	                                                     filename,
 84	                                                     clsidEncoder,
 85	                                                     encoderParams));
 86    }
 87//    Status Save(in IStream* stream,
 88  //              in CLSID* clsidEncoder,
 89    //            in EncoderParameters *encoderParams = null);
 90    Status SaveAdd(in EncoderParameters* encoderParams) {
 91	    return SetStatus(GdipSaveAdd(nativeImage,
 92	                                             encoderParams));
 93    }
 94
 95    Status SaveAdd(in Image* newImage,
 96                   in EncoderParameters* encoderParams) {
 97	    if ( newImage is null ) {
 98	        return SetStatus(Status.InvalidParameter);
 99	    }
100	
101	    return SetStatus(GdipSaveAddImage(nativeImage,
102	                                                  newImage.nativeImage,
103	                                                  encoderParams));
104    }
105
106    ImageType GetType() {
107	    ImageType type = ImageType.ImageTypeUnknown;
108	
109	    SetStatus(GdipGetImageType(nativeImage, &type));
110	
111	    return type;
112    }
113
114    Status GetPhysicalDimension(SizeF* size) {
115	    if (size is null) {
116	        return SetStatus(Status.InvalidParameter);
117	    }
118	    
119	    REAL width, height;
120	    Status status;
121	
122	    status = SetStatus(GdipGetImageDimension(nativeImage,
123	                                                         &width, &height));
124	
125	    size.Width  = width;
126	    size.Height = height;
127	
128	    return status;
129    }
130
131    Status GetBounds(RectF* srcRect,
132                     Unit* srcUnit) {
133	    return SetStatus(GdipGetImageBounds(nativeImage,
134	                    srcRect, srcUnit));
135    }
136
137    UINT GetWidth() {
138	    UINT width = 0;
139	
140	    SetStatus(GdipGetImageWidth(nativeImage, &width));
141	
142	    return width;
143    }
144
145    UINT GetHeight() {
146	    UINT height = 0;
147	
148	    SetStatus(GdipGetImageHeight(nativeImage, &height));
149	
150	    return height;
151    }
152
153    REAL GetHorizontalResolution() {
154	    REAL resolution = 0.0f;
155	
156	    SetStatus(GdipGetImageHorizontalResolution(nativeImage, &resolution));
157
158	    return resolution;
159    }
160
161    REAL GetVerticalResolution() {
162	    REAL resolution = 0.0f;
163
164	    SetStatus(GdipGetImageVerticalResolution(nativeImage, &resolution));
165	
166	    return resolution;
167    }
168
169    UINT GetFlags() {
170	    UINT flags = 0;
171	
172	    SetStatus(GdipGetImageFlags(nativeImage, &flags));
173	
174	    return flags;
175    }
176
177    Status GetRawFormat(GUID *format) {
178   		return SetStatus(GdipGetImageRawFormat(nativeImage, format));
179    }
180
181    PixelFormat GetPixelFormat() {
182	    PixelFormat format;
183	
184	    SetStatus(GdipGetImagePixelFormat(nativeImage, &format));
185	
186	    return format;
187    }
188
189    INT GetPaletteSize() {
190	    INT size = 0;
191	    
192	    SetStatus(GdipGetImagePaletteSize(nativeImage, &size));
193	    
194	    return size;
195    }
196
197    Status GetPalette(ColorPalette* palette,
198                      in INT size) {
199	    return SetStatus(GdipGetImagePalette(nativeImage, palette, size));
200    }
201
202    Status SetPalette(in ColorPalette* palette) {
203	    return SetStatus(GdipSetImagePalette(nativeImage, palette));
204    }
205
206    Image GetThumbnailImage(in UINT thumbWidth,
207                             in UINT thumbHeight,
208                             in GetThumbnailImageAbort callback = null,
209                             in VOID* callbackData = null) {
210	    GpImage *thumbimage = null;
211
212	    SetStatus(GdipGetImageThumbnail(nativeImage,
213	                                                thumbWidth, thumbHeight,
214	                                                &thumbimage,
215	                                                callback, callbackData));
216
217	    Image newImage = new Image(thumbimage, lastResult);
218
219	    if (newImage is null) {
220	        GdipDisposeImage(thumbimage);
221	    }
222
223	    return newImage;
224    }
225
226    UINT GetFrameDimensionsCount() {
227	    UINT count = 0;
228	
229	    SetStatus(GdipImageGetFrameDimensionsCount(nativeImage,
230	                                                                  &count));
231	
232	    return count;
233    }
234
235    Status GetFrameDimensionsList(GUID* dimensionIDs,
236                                  in UINT count) {
237	    return SetStatus(GdipImageGetFrameDimensionsList(nativeImage,
238	                                                                 dimensionIDs,
239                                                                 count));
240    }
241
242    UINT GetFrameCount(in GUID* dimensionID) {
243	    UINT count = 0;
244	
245	    SetStatus(GdipImageGetFrameCount(nativeImage,
246	                                                        dimensionID,
247	                                                        &count));
248	    return count;
249    }
250
251    Status SelectActiveFrame(in GUID* dimensionID,
252                             in UINT frameIndex) {
253	    return SetStatus(GdipImageSelectActiveFrame(nativeImage,
254	                                                            dimensionID,
255	                                                            frameIndex));
256    }
257
258    Status RotateFlip(in RotateFlipType rotateFlipType) {
259	    return SetStatus(GdipImageRotateFlip(nativeImage,
260	                                                     rotateFlipType));
261    }
262
263    UINT GetPropertyCount() {
264	    UINT numProperty = 0;
265	
266	    SetStatus(GdipGetPropertyCount(nativeImage,
267	                                               &numProperty));
268	
269	    return numProperty;
270    }
271
272    Status GetPropertyIdList(in UINT numOfProperty,
273                             PROPID* list) {
274	    return SetStatus(GdipGetPropertyIdList(nativeImage,
275	                                                       numOfProperty, list));
276    }
277
278    UINT GetPropertyItemSize(in PROPID propId) {
279	    UINT size = 0;
280	
281	    SetStatus(GdipGetPropertyItemSize(nativeImage,
282	                                                  propId,
283	                                                  &size));
284	
285	    return size;
286    }
287
288    Status GetPropertyItem(in PROPID propId,
289                           in UINT propSize,
290                           PropertyItem* buffer) {
291	    return SetStatus(GdipGetPropertyItem(nativeImage,
292	                                                     propId, propSize, buffer));
293	}
294
295    Status GetPropertySize(UINT* totalBufferSize,
296                           UINT* numProperties) {
297	    return SetStatus(GdipGetPropertySize(nativeImage,
298	                                                     totalBufferSize,
299	                                                     numProperties));
300    }
301
302    Status GetAllPropertyItems(in UINT totalBufferSize,
303                               in UINT numProperties,
304                               PropertyItem* allItems) {
305	    if (allItems == null) {
306	        return SetStatus(Status.InvalidParameter);
307	    }
308	    return SetStatus(GdipGetAllPropertyItems(nativeImage,
309	                                                         totalBufferSize,
310	                                                         numProperties,
311	                                                         allItems));
312    }
313
314    Status RemovePropertyItem(in PROPID propId) {
315	    return SetStatus(GdipRemovePropertyItem(nativeImage, propId));
316    }
317
318    Status SetPropertyItem(in PropertyItem* item) {
319	    return SetStatus(GdipSetPropertyItem(nativeImage, item));
320    }
321
322    UINT  GetEncoderParameterListSize(in CLSID* clsidEncoder) {
323	    UINT size = 0;
324
325	    SetStatus(GdipGetEncoderParameterListSize(nativeImage,
326	                                                          clsidEncoder,
327	                                                          &size));
328	    return size;
329    }
330
331    Status GetEncoderParameterList(in CLSID* clsidEncoder,
332                                   in UINT size,
333                                   EncoderParameters* buffer) {
334	    return SetStatus(GdipGetEncoderParameterList(nativeImage,
335	                                                             clsidEncoder,
336	                                                             size,
337	                                                             buffer));
338    }
339
340version(GDIPLUS6) {
341    Status FindFirstItem(in ImageItemData *item) {
342	    return SetStatus(GdipFindFirstImageItem(nativeImage, item));
343    }
344
345    Status FindNextItem(in ImageItemData *item) {
346	    return SetStatus(GdipFindNextImageItem(nativeImage, item));
347    }
348
349    Status GetItemData(in ImageItemData *item) {
350	    return SetStatus(GdipGetImageItemData(nativeImage, item));
351    }
352
353    Status SetAbort(GdiplusAbort *pIAbort) {
354	    return SetStatus(GdipImageSetAbort(
355	        nativeImage,
356	        pIAbort
357	        ));
358    }
359}
360
361    Status GetLastStatus() {
362	    Status lastStatus = lastResult;
363	    lastResult = Status.Ok;
364
365	    return lastStatus;
366    }
367
368protected:
369
370    this() {}
371
372    package this(GpImage *nativeImage, Status status) {
373	    SetNativeImage(nativeImage);
374	    lastResult = status;
375    }
376
377    VOID SetNativeImage(GpImage* nativeImage) {
378	    this.nativeImage = nativeImage;
379    }
380
381    Status SetStatus(Status status) {
382        if (status != Status.Ok)
383            return (lastResult = status);
384        else
385            return status;
386    }
387
388    package GpImage* nativeImage;
389    package Status lastResult;
390    package Status loadStatus;
391}
392
393
394
395class Bitmap : Image {
396
397    this(in WCHAR* filename, in BOOL useEmbeddedColorManagement = FALSE) {
398	    GpBitmap *bitmap = null;
399	
400	    if(useEmbeddedColorManagement) {
401	        lastResult = GdipCreateBitmapFromFileICM(filename, &bitmap);
402	    }
403	    else {
404	        lastResult = GdipCreateBitmapFromFile(filename, &bitmap);
405	    }
406	
407	    SetNativeImage(bitmap);    	
408    }
409
410	/*
411    this(in IStream *stream, in BOOL useEmbeddedColorManagement = FALSE) {
412	    GpBitmap *bitmap = null;
413	
414	    if(useEmbeddedColorManagement) {
415	        lastResult = GdipCreateBitmapFromStreamICM(stream, &bitmap);
416	    }
417	    else {
418	        lastResult = GdipCreateBitmapFromStream(stream, &bitmap);
419	    }
420	
421	    SetNativeImage(bitmap);    	
422    }
423    */
424
425    static Bitmap FromFile(in WCHAR* filename, in BOOL useEmbeddedColorManagement = FALSE) {
426	    return new Bitmap(
427	        filename,
428	        useEmbeddedColorManagement
429	    );
430    }
431
432	/*
433    static Bitmap FromStream(in IStream *stream, in BOOL useEmbeddedColorManagement = FALSE) {    	
434	    return new Bitmap(
435	        stream,
436	        useEmbeddedColorManagement
437	    );
438    }
439    */
440
441    this(in INT width, in INT height, in INT stride, PixelFormat format, in BYTE* scan0) {   
442	    GpBitmap *bitmap = null;
443	
444	    lastResult = GdipCreateBitmapFromScan0(width,
445	                                                       height,
446	                                                       stride,
447	                                                       format,
448	                                                       scan0,
449	                                                       &bitmap);
450	
451	    SetNativeImage(bitmap);        	
452    }
453    
454    this(in INT width, in INT height, in PixelFormat format = PixelFormat32bppARGB) {
455	    GpBitmap *bitmap = null;
456	
457	    lastResult = GdipCreateBitmapFromScan0(width,
458	                                                       height,
459	                                                       0,
460	                                                       format,
461	                                                       null,
462	                                                       &bitmap);
463	
464	    SetNativeImage(bitmap);
465    }
466    
467    this(in INT width, in INT height, in Graphics target) { 
468	    GpBitmap *bitmap = null;
469	
470	    lastResult = GdipCreateBitmapFromGraphics(width,
471	                                                          height,
472	                                                          target.nativeGraphics,
473	                                                          &bitmap);
474	
475	    SetNativeImage(bitmap);   	
476    }
477
478    Bitmap Clone(in Rect rect, in PixelFormat format) {
479    	return Clone(rect.X, rect.Y, rect.Width, rect.Height, format);    	
480    }
481    
482    Bitmap Clone(in INT x, in INT y, in INT width, in INT height, in PixelFormat format) {
483	   GpBitmap* gpdstBitmap = null;
484	   Bitmap bitmap;
485	
486	   lastResult = GdipCloneBitmapAreaI(
487	                               x,
488	                               y,
489	                               width,
490	                               height,
491	                               format,
492	                               cast(GpBitmap *)nativeImage,
493	                               &gpdstBitmap);
494	
495	   if (lastResult == Status.Ok) {
496	       bitmap = new Bitmap(gpdstBitmap);
497	
498	       if (bitmap is null) {
499	           GdipDisposeImage(gpdstBitmap);
500	       }
501	
502	       return bitmap;
503	   }
504	   else
505	       return null;
506    	
507    }
508    
509    Bitmap Clone(in RectF rect, in PixelFormat format) {
510    	return Clone(rect.X, rect.Y, rect.Width, rect.Height, format);    	    	
511    }
512    
513    Bitmap Clone(in REAL x, in REAL y, in REAL width, in REAL height, in PixelFormat format) {
514	   GpBitmap* gpdstBitmap = null;
515	   Bitmap bitmap;
516	
517	   SetStatus(GdipCloneBitmapArea(
518	                               x,
519	                               y,
520	                               width,
521	                               height,
522	                               format,
523	                               cast(GpBitmap *)nativeImage,
524	                               &gpdstBitmap));
525	
526	   if (lastResult == Status.Ok) {
527	       bitmap = new Bitmap(gpdstBitmap);
528	
529	       if (bitmap is null) {
530	       		GdipDisposeImage(gpdstBitmap);
531	       }
532	
533	       return bitmap;
534	   }
535	   else
536	       return null;
537    	
538    }
539
540    Status LockBits(in Rect* rect, in UINT flags, in PixelFormat format, BitmapData* lockedBitmapData) {
541	    return SetStatus(GdipBitmapLockBits(
542	                                    cast(GpBitmap*)(nativeImage),
543	                                    rect,
544	                                    flags,
545	                                    format,
546	                                    lockedBitmapData));    	
547    }
548    
549    Status UnlockBits(in BitmapData* lockedBitmapData) {
550	    return SetStatus(GdipBitmapUnlockBits(
551	                                    cast(GpBitmap*)(nativeImage),
552	                                    lockedBitmapData));    	
553    }
554    
555    Status GetPixel(in INT x, in INT y, Color *color) {
556	    ARGB argb;
557	
558	    Status status = SetStatus(GdipBitmapGetPixel(
559	        cast(GpBitmap *)(nativeImage),
560	        x, y,        
561	        &argb));
562	
563	    if (status == Status.Ok) {
564	        color.SetValue(argb);
565	    }
566	
567	    return  status;    	
568    }
569    
570    Status SetPixel(in INT x, in INT y, in Color color) {
571	    return SetStatus(GdipBitmapSetPixel(
572	        cast(GpBitmap *)(nativeImage),
573	        x, y,
574	        color.GetValue()));    	
575    }
576    /*
577    Status ConvertFormat(PixelFormat format, DitherType dithertype, PaletteType palettetype, ColorPalette *palette, REAL alphaThresholdPercent) {
578    	
579    }
580    
581    // The palette must be allocated and count must be set to the number of
582    // entries in the palette. If there are not enough, the API will fail.
583    
584    static Status InitializePalette(
585        in ColorPalette *palette,  // Palette to initialize.
586        PaletteType palettetype,       // palette enumeration type.
587        INT optimalColors,             // how many optimal colors
588        BOOL useTransparentColor,      // add a transparent color to the palette.
589        Bitmap *bitmap                 // optional bitmap for median cut.
590        ) {
591        	
592    }
593        
594    Status ApplyEffect(Effect *effect, RECT* ROI) {
595    }
596    
597    static Status ApplyEffect(
598        in  Bitmap **inputs,
599        in  INT numInputs,
600        in  Effect *effect, 
601        in  RECT *ROI,           // optional parameter.
602        RECT *outputRect,    // optional parameter.
603        Bitmap **output
604    ) {    	
605    }
606    
607    Status GetHistogram(
608        in HistogramFormat format,
609        in UINT NumberOfEntries,
610        UINT *channel0,
611        UINT *channel1,
612        UINT *channel2,
613        UINT *channel3
614    ) {    	
615    }
616    
617    static Status GetHistogramSize(in HistogramFormat format, UINT *NumberOfEntries) {    	
618    }
619    */
620    
621    Status SetResolution(in REAL xdpi, in REAL ydpi) {  
622	    return SetStatus(GdipBitmapSetResolution(
623	        cast(GpBitmap *)(nativeImage),
624	        xdpi, ydpi));  	
625    }
626
627    this(in IDirectDrawSurface7* surface) {
628	    GpBitmap *bitmap = null;
629	
630	    lastResult = GdipCreateBitmapFromDirectDrawSurface(surface,
631	                                                       &bitmap);
632	
633	    SetNativeImage(bitmap);    	
634    }
635    
636    this(in BITMAPINFO* gdiBitmapInfo, in VOID* gdiBitmapData) {   
637	    GpBitmap *bitmap = null;
638	
639	    lastResult = GdipCreateBitmapFromGdiDib(gdiBitmapInfo,
640	                                                        gdiBitmapData,
641	                                                        &bitmap);
642	
643	    SetNativeImage(bitmap); 	
644    }
645    
646    this(in HBITMAP hbm, in HPALETTE hpal) {  
647	    GpBitmap *bitmap = null;
648	
649	    lastResult = GdipCreateBitmapFromHBITMAP(hbm, hpal, &bitmap);
650	
651	    SetNativeImage(bitmap);  	
652    }
653    
654    this(in HICON hicon) {  
655	    GpBitmap *bitmap = null;
656	
657	    lastResult = GdipCreateBitmapFromHICON(hicon, &bitmap);
658	
659	    SetNativeImage(bitmap);  	
660    }
661    
662    this(in HINSTANCE hInstance, in WCHAR * bitmapName) { 
663	    GpBitmap *bitmap = null;
664	
665	    lastResult = GdipCreateBitmapFromResource(hInstance,
666	                                                          bitmapName,
667	                                                          &bitmap);
668	
669	    SetNativeImage(bitmap);   	
670    }
671    
672    static Bitmap FromDirectDrawSurface7(in IDirectDrawSurface7* surface) {    
673    	return new Bitmap(surface);	
674    }
675    
676    static Bitmap FromBITMAPINFO(in BITMAPINFO* gdiBitmapInfo, in VOID* gdiBitmapData) {    
677    	return new Bitmap(gdiBitmapInfo, gdiBitmapData);	
678    }
679    
680    static Bitmap FromHBITMAP(in HBITMAP hbm, in HPALETTE hpal) { 
681    	return new Bitmap(hbm, hpal);   	
682    }
683    
684    static Bitmap FromHICON(in HICON hicon) {
685    	return new Bitmap(hicon);    	
686    }
687    
688    static Bitmap FromResource(in HINSTANCE hInstance, in WCHAR * bitmapName) {  
689    	return new Bitmap(hInstance, bitmapName);  	
690    }
691    
692    Status GetHBITMAP(in Color colorBackground, HBITMAP *hbmReturn) {
693	    return SetStatus(GdipCreateHBITMAPFromBitmap(
694	                                        cast(GpBitmap*)(nativeImage),
695	                                        hbmReturn,
696	                                        colorBackground.GetValue()));    	
697    }
698    
699    Status GetHICON(HICON *hicon) {   
700	    return SetStatus(GdipCreateHICONFromBitmap(
701	                                        cast(GpBitmap*)(nativeImage),
702	                                        hicon)); 	
703    }
704    
705protected:
706    package this(GpBitmap *nativeBitmap) {  
707	    lastResult = Status.Ok;
708	
709	    SetNativeImage(nativeBitmap);  	
710    }
711}
712