comparison dwt/graphics/Image.d @ 36:db5a898b2119

Fixed a lot of compile errors
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Tue, 07 Oct 2008 12:56:18 +0200
parents 7d135fe0caf2
children d8635bb48c7c
comparison
equal deleted inserted replaced
35:7d135fe0caf2 36:db5a898b2119
91 * @see ImageData 91 * @see ImageData
92 * @see ImageLoader 92 * @see ImageLoader
93 */ 93 */
94 public final class Image : Resource, Drawable { 94 public final class Image : Resource, Drawable {
95 95
96 alias Resource.init_ init_;
97
96 /** 98 /**
97 * specifies whether the receiver is a bitmap or an icon 99 * specifies whether the receiver is a bitmap or an icon
98 * (one of <code>DWT.BITMAP</code>, <code>DWT.ICON</code>) 100 * (one of <code>DWT.BITMAP</code>, <code>DWT.ICON</code>)
99 * <p> 101 * <p>
100 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT 102 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT
187 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> 189 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
188 * </ul> 190 * </ul>
189 */ 191 */
190 public this(Device device, int width, int height) { 192 public this(Device device, int width, int height) {
191 super(device); 193 super(device);
192 init(width, height); 194 init_(width, height);
193 init(); 195 init_();
194 } 196 }
195 197
196 /** 198 /**
197 * Constructs a new instance of this class based on the 199 * Constructs a new instance of this class based on the
198 * provided image, with an appearance that varies depending 200 * provided image, with an appearance that varies depending
256 258
257 /* Create the image */ 259 /* Create the image */
258 handle = cast(NSImage)(new NSImage()).alloc(); 260 handle = cast(NSImage)(new NSImage()).alloc();
259 handle = handle.initWithSize(size); 261 handle = handle.initWithSize(size);
260 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc(); 262 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
261 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(null, cast(NSInteger) width, cast(NSInteger) height, srcRep.bitsPerSample(), srcRep.samplesPerPixel(), srcRep.samplesPerPixel() is 4, srcRep.isPlanar(), new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, srcRep.bytesPerRow(), srcRep.bitsPerPixel()); 263 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(null, cast(NSInteger) width, cast(NSInteger) height, srcRep.bitsPerSample(), srcRep.samplesPerPixel(), srcRep.samplesPerPixel() is 4, srcRep.isPlanar(), OS.NSDeviceRGBColorSpace, NSAlphaFirstBitmapFormat | NSAlphaNonpremultipliedBitmapFormat, srcRep.bytesPerRow(), srcRep.bitsPerPixel());
262 handle.addRepresentation(rep); 264 handle.addRepresentation(rep);
263 265
264 objc.id data = rep.bitmapData(); 266 objc.id data = rep.bitmapData();
265 OS.memmove(data, srcImage.imageRep.bitmapData(), cast(size_t) (width * height * 4)); 267 OS.memmove(data, srcImage.imageRep.bitmapData(), cast(size_t) (width * height * 4));
266 if (flag !is DWT.IMAGE_COPY) { 268 if (flag !is DWT.IMAGE_COPY) {
319 } 321 }
320 break; 322 break;
321 } 323 }
322 } 324 }
323 } 325 }
324 init(); 326 init_();
325 } 327 }
326 328
327 /** 329 /**
328 * Constructs an empty instance of this class with the 330 * Constructs an empty instance of this class with the
329 * width and height of the specified rectangle. The result 331 * width and height of the specified rectangle. The result
355 * </ul> 357 * </ul>
356 */ 358 */
357 public this(Device device, Rectangle bounds) { 359 public this(Device device, Rectangle bounds) {
358 super(device); 360 super(device);
359 if (bounds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 361 if (bounds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
360 init(bounds.width, bounds.height); 362 init_(bounds.width, bounds.height);
361 init(); 363 init_();
362 } 364 }
363 365
364 /** 366 /**
365 * Constructs an instance of this class from the given 367 * Constructs an instance of this class from the given
366 * <code>ImageData</code>. 368 * <code>ImageData</code>.
379 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> 381 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
380 * </ul> 382 * </ul>
381 */ 383 */
382 public this(Device device, ImageData data) { 384 public this(Device device, ImageData data) {
383 super(device); 385 super(device);
384 init(data); 386 init_(data);
385 init(); 387 init_();
386 } 388 }
387 389
388 /** 390 /**
389 * Constructs an instance of this class, whose type is 391 * Constructs an instance of this class, whose type is
390 * <code>DWT.ICON</code>, from the two given <code>ImageData</code> 392 * <code>DWT.ICON</code>, from the two given <code>ImageData</code>
419 } 421 }
420 mask = ImageData.convertMask(mask); 422 mask = ImageData.convertMask(mask);
421 ImageData image = new ImageData(source.width, source.height, source.depth, source.palette, source.scanlinePad, source.data); 423 ImageData image = new ImageData(source.width, source.height, source.depth, source.palette, source.scanlinePad, source.data);
422 image.maskPad = mask.scanlinePad; 424 image.maskPad = mask.scanlinePad;
423 image.maskData = mask.data; 425 image.maskData = mask.data;
424 init(image); 426 init_(image);
425 } 427 }
426 428
427 /** 429 /**
428 * Constructs an instance of this class by loading its representation 430 * Constructs an instance of this class by loading its representation
429 * from the specified input stream. Throws an error if an error 431 * from the specified input stream. Throws an error if an error
472 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> 474 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
473 * </ul> 475 * </ul>
474 */ 476 */
475 public this(Device device, InputStream stream) { 477 public this(Device device, InputStream stream) {
476 super(device); 478 super(device);
477 init(new ImageData(stream)); 479 init_(new ImageData(stream));
478 init(); 480 init_();
479 } 481 }
480 482
481 /** 483 /**
482 * Constructs an instance of this class by loading its representation 484 * Constructs an instance of this class by loading its representation
483 * from the file with the specified name. Throws an error if an error 485 * from the file with the specified name. Throws an error if an error
505 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li> 507 * <li>ERROR_NO_HANDLES if a handle could not be obtained for image creation</li>
506 * </ul> 508 * </ul>
507 */ 509 */
508 public this(Device device, String filename) { 510 public this(Device device, String filename) {
509 super(device); 511 super(device);
510 init(new ImageData(filename)); 512 init_(new ImageData(filename));
511 init(); 513 init_();
512 } 514 }
513 515
514 void createAlpha () { 516 void createAlpha () {
515 if (transparentPixel is -1 && alpha is -1 && alphaData is null) return; 517 if (transparentPixel is -1 && alpha is -1 && alphaData is null) return;
516 NSSize size = handle.size(); 518 NSSize size = handle.size();
594 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 596 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
595 if (transparentPixel is -1) return null; 597 if (transparentPixel is -1) return null;
596 int red = (transparentPixel >> 16) & 0xFF; 598 int red = (transparentPixel >> 16) & 0xFF;
597 int green = (transparentPixel >> 8) & 0xFF; 599 int green = (transparentPixel >> 8) & 0xFF;
598 int blue = (transparentPixel >> 0) & 0xFF; 600 int blue = (transparentPixel >> 0) & 0xFF;
599 return Color.cocoa_new(device, new float[][red / 255f, green / 255f, blue / 255f, 1]); 601 return Color.cocoa_new(device, [red / 255f, green / 255f, blue / 255f, 1]);
600 } 602 }
601 603
602 /** 604 /**
603 * Returns the bounds of the receiver. The rectangle will always 605 * Returns the bounds of the receiver. The rectangle will always
604 * have x and y values of 0, and the width and height of the 606 * have x and y values of 0, and the width and height of the
721 * @return the receiver's hash 723 * @return the receiver's hash
722 * 724 *
723 * @see #equals 725 * @see #equals
724 */ 726 */
725 public hash_t toHash () { 727 public hash_t toHash () {
726 return handle !is null ? handle.id_ : null; 728 return handle !is null ? cast(hash_t) handle.id_ : 0;
727 } 729 }
728 730
729 alias toHash hashCode; 731 alias toHash hashCode;
730 732
731 void init(int width, int height) { 733 void init_(int width, int height) {
732 if (width <= 0 || height <= 0) { 734 if (width <= 0 || height <= 0) {
733 DWT.error (DWT.ERROR_INVALID_ARGUMENT); 735 DWT.error (DWT.ERROR_INVALID_ARGUMENT);
734 } 736 }
735 this.type = DWT.BITMAP; 737 this.type = DWT.BITMAP;
736 this.width = width; 738 this.width = width;
740 NSSize size = NSSize(); 742 NSSize size = NSSize();
741 size.width = width; 743 size.width = width;
742 size.height = height; 744 size.height = height;
743 handle = handle.initWithSize(size); 745 handle = handle.initWithSize(size);
744 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc(); 746 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
745 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, cast(NSInteger) width, cast(NSInteger) height, 8, 3, false, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, cast(NSInteger) (width * 4), 32); 747 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(null, cast(NSInteger) width, cast(NSInteger) height, 8, 3, false, false, OS.NSDeviceRGBColorSpace, NSAlphaFirstBitmapFormat | NSAlphaNonpremultipliedBitmapFormat, cast(NSInteger) (width * 4), 32);
746 OS.memset(rep.bitmapData(), 0xFF, cast(size_t) (width * height * 4)); 748 OS.memset(rep.bitmapData(), 0xFF, cast(size_t) (width * height * 4));
747 handle.addRepresentation(rep); 749 handle.addRepresentation(rep);
748 // rep.release(); 750 // rep.release();
749 } 751 }
750 752
751 void init(ImageData image) { 753 void init_(ImageData image) {
752 if (image is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 754 if (image is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
753 this.width = image.width; 755 this.width = image.width;
754 this.height = image.height; 756 this.height = image.height;
755 PaletteData palette = image.palette; 757 PaletteData palette = image.palette;
756 if (!(((image.depth is 1 || image.depth is 2 || image.depth is 4 || image.depth is 8) && !palette.isDirect) || 758 if (!(((image.depth is 1 || image.depth is 2 || image.depth is 4 || image.depth is 8) && !palette.isDirect) ||
850 size.width = width; 852 size.width = width;
851 size.height = height; 853 size.height = height;
852 handle = handle.initWithSize(size); 854 handle = handle.initWithSize(size);
853 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc(); 855 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
854 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( 856 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(
855 null, cast(NSInteger) width, cast(NSInteger) height, 8, hasAlpha ? 4 : 3, hasAlpha, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, cast(NSInteger) bpr, 32); 857 null, cast(NSInteger) width, cast(NSInteger) height, 8, hasAlpha ? 4 : 3, hasAlpha, false, OS.NSDeviceRGBColorSpace, NSAlphaFirstBitmapFormat | NSAlphaNonpremultipliedBitmapFormat, cast(NSInteger) bpr, 32);
856 OS.memmove(rep.bitmapData(), buffer.ptr, dataSize); 858 OS.memmove(rep.bitmapData(), buffer.ptr, dataSize);
857 handle.addRepresentation(rep); 859 handle.addRepresentation(rep);
858 // rep.release(); 860 // rep.release();
859 } 861 }
860 862
869 * </p> 871 * </p>
870 * 872 *
871 * @param data the platform specific GC data 873 * @param data the platform specific GC data
872 * @return the platform specific GC handle 874 * @return the platform specific GC handle
873 */ 875 */
874 public int internal_new_GC (GCData data) { 876 public objc.id internal_new_GC (GCData data) {
875 if (handle is null) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 877 if (handle is null) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
876 if (type !is DWT.BITMAP || memGC !is null) { 878 if (type !is DWT.BITMAP || memGC !is null) {
877 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 879 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
878 } 880 }
879 NSGraphicsContext current = NSGraphicsContext.currentContext(); 881 NSGraphicsContext current = NSGraphicsContext.currentContext();
880 NSBitmapImageRep rep = imageRep; 882 NSBitmapImageRep rep = imageRep;
881 if (imageRep.hasAlpha()) { 883 if (imageRep.hasAlpha()) {
882 NSInteger bpr = width * 4; 884 NSInteger bpr = width * 4;
883 rep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc(); 885 rep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
884 int bitmapData = imageRep.bitmapData(); 886 objc.id bitmapData = imageRep.bitmapData();
885 if (data.bitmapDataAddress !is null) OS.free(data.bitmapDataAddress); 887 if (data.bitmapDataAddress !is null) OS.free(data.bitmapDataAddress);
886 data.bitmapDataAddress = OS.malloc((void*).sizeof); 888 data.bitmapDataAddress = cast(ubyte*) OS.malloc((void*).sizeof);
887 OS.memmove(data.bitmapDataAddress, (new int[] (bitmapData)).ptr, (void*).sizeof); 889 OS.memmove(data.bitmapDataAddress, bitmapData, (void*).sizeof);
888 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( 890 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(
889 &data.bitmapDataAddress, cast(NSInteger) width, cast(NSInteger) height, 8, 3, false, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat , bpr, 32); 891 &data.bitmapDataAddress, cast(NSInteger) width, cast(NSInteger) height, 8, 3, false, false, OS.NSDeviceRGBColorSpace, NSAlphaFirstBitmapFormat , bpr, 32);
890 rep.autorelease(); 892 rep.autorelease();
891 } 893 }
892 NSGraphicsContext context = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep); 894 NSGraphicsContext context = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep);
893 NSGraphicsContext.setCurrentContext(context); 895 NSGraphicsContext.setCurrentContext(context);
894 NSAffineTransform transform = NSAffineTransform.transform(); 896 NSAffineTransform transform = NSAffineTransform.transform();
906 data.foreground = device.COLOR_BLACK.handle; 908 data.foreground = device.COLOR_BLACK.handle;
907 data.font = device.systemFont; 909 data.font = device.systemFont;
908 data.image = this; 910 data.image = this;
909 } 911 }
910 NSGraphicsContext.setCurrentContext(current); 912 NSGraphicsContext.setCurrentContext(current);
911 return context.id; 913 return context.id_;
912 } 914 }
913 915
914 /** 916 /**
915 * Invokes platform specific functionality to dispose a GC handle. 917 * Invokes platform specific functionality to dispose a GC handle.
916 * <p> 918 * <p>
922 * </p> 924 * </p>
923 * 925 *
924 * @param hDC the platform specific GC handle 926 * @param hDC the platform specific GC handle
925 * @param data the platform specific GC data 927 * @param data the platform specific GC data
926 */ 928 */
927 public void internal_dispose_GC (int context, GCData data) { 929 public void internal_dispose_GC (objc.id context, GCData data) {
928 if (data.bitmapDataAddress !is null) OS.free(data.bitmapDataAddress); 930 if (data.bitmapDataAddress !is null) OS.free(data.bitmapDataAddress);
929 data.bitmapDataAddress = null; 931 data.bitmapDataAddress = null;
930 } 932 }
931 933
932 /** 934 /**