comparison dwt/graphics/Image.d @ 46:cfa563df4fdd

Updated Widget and Display to 3.514
author Jacob Carlborg <doob@me.com>
date Fri, 05 Dec 2008 16:00:41 +0100
parents d8635bb48c7c
children ad4e1fe71a5a
comparison
equal deleted inserted replaced
45:d8635bb48c7c 46:cfa563df4fdd
41 import dwt.graphics.ImageData; 41 import dwt.graphics.ImageData;
42 import dwt.graphics.PaletteData; 42 import dwt.graphics.PaletteData;
43 import dwt.graphics.RGB; 43 import dwt.graphics.RGB;
44 import dwt.graphics.Rectangle; 44 import dwt.graphics.Rectangle;
45 import dwt.graphics.Resource; 45 import dwt.graphics.Resource;
46 import dwt.internal.cocoa.NSInteger; 46 import dwt.internal.objc.cocoa.Cocoa;
47 import objc = dwt.internal.objc.runtime; 47 import objc = dwt.internal.objc.runtime;
48 48
49 /** 49 /**
50 * Instances of this class are graphics which have been prepared 50 * Instances of this class are graphics which have been prepared
51 * for display on a specific device. That is, they are ready 51 * for display on a specific device. That is, they are ready
249 default: 249 default:
250 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 250 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
251 } 251 }
252 252
253 NSAutoreleasePool pool = null; 253 NSAutoreleasePool pool = null;
254 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 254 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
255 try { 255 try {
256 device = this.device; 256 device = this.device;
257 this.type = srcImage.type; 257 this.type = srcImage.type;
258 /* Get source image size */ 258 /* Get source image size */
259 NSSize size = srcImage.handle.size(); 259 NSSize size = srcImage.handle.size();
272 272
273 /* Create the image */ 273 /* Create the image */
274 handle = cast(NSImage)(new NSImage()).alloc(); 274 handle = cast(NSImage)(new NSImage()).alloc();
275 handle = handle.initWithSize(size); 275 handle = handle.initWithSize(size);
276 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc(); 276 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
277 rep = rep.initWithBitmapDataPlanes(null, width, height, srcRep.bitsPerSample(), srcRep.samplesPerPixel(), srcRep.samplesPerPixel() is 4, srcRep.isPlanar(), OS.NSDeviceRGBColorSpace, NSAlphaFirstBitmapFormat | NSAlphaNonpremultipliedBitmapFormat, srcRep.bytesPerRow(), srcRep.bitsPerPixel()); 277 rep = rep.initWithBitmapDataPlanes(null, width, height, srcRep.bitsPerSample(), srcRep.samplesPerPixel(), srcRep.samplesPerPixel() is 4, srcRep.isPlanar(), OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, srcRep.bytesPerRow(), srcRep.bitsPerPixel());
278 handle.addRepresentation(rep); 278 handle.addRepresentation(rep);
279 279
280 objc.id data = rep.bitmapData(); 280 objc.id data = rep.bitmapData();
281 OS.memmove(data, srcImage.imageRep.bitmapData(), width * height * 4); 281 OS.memmove(data, srcImage.imageRep.bitmapData(), width * height * 4);
282 if (flag !is DWT.IMAGE_COPY) { 282 if (flag !is DWT.IMAGE_COPY) {
292 Color oneColor = device.getSystemColor(DWT.COLOR_WIDGET_BACKGROUND); 292 Color oneColor = device.getSystemColor(DWT.COLOR_WIDGET_BACKGROUND);
293 RGB oneRGB = oneColor.getRGB(); 293 RGB oneRGB = oneColor.getRGB();
294 byte oneRed = cast(byte)oneRGB.red; 294 byte oneRed = cast(byte)oneRGB.red;
295 byte oneGreen = cast(byte)oneRGB.green; 295 byte oneGreen = cast(byte)oneRGB.green;
296 byte oneBlue = cast(byte)oneRGB.blue; 296 byte oneBlue = cast(byte)oneRGB.blue;
297 byte[] line = new byte[(int)/*64*/bpr]; 297 byte[] line = new byte[bpr];
298 for (int y=0; y<height; y++) { 298 for (int y=0; y<height; y++) {
299 OS.memmove(line.ptr, data + (y * bpr), bpr); 299 OS.memmove(line.ptr, data + (y * bpr), bpr);
300 int offset = 0; 300 int offset = 0;
301 for (int x=0; x<width; x++) { 301 for (int x=0; x<width; x++) {
302 int red = line[offset+1] & 0xFF; 302 int red = line[offset+1] & 0xFF;
317 OS.memmove(data + (y * bpr), line.ptr, bpr); 317 OS.memmove(data + (y * bpr), line.ptr, bpr);
318 } 318 }
319 break; 319 break;
320 } 320 }
321 case DWT.IMAGE_GRAY: { 321 case DWT.IMAGE_GRAY: {
322 byte[] line = new byte[(int)/*64*/bpr]; 322 byte[] line = new byte[bpr];
323 for (int y=0; y<height; y++) { 323 for (int y=0; y<height; y++) {
324 OS.memmove(line.ptr, data + (y * bpr), bpr); 324 OS.memmove(line.ptr, data + (y * bpr), bpr);
325 int offset = 0; 325 int offset = 0;
326 for (int x=0; x<width; x++) { 326 for (int x=0; x<width; x++) {
327 int red = line[offset+1] & 0xFF; 327 int red = line[offset+1] & 0xFF;
375 */ 375 */
376 public this(Device device, Rectangle bounds) { 376 public this(Device device, Rectangle bounds) {
377 super(device); 377 super(device);
378 if (bounds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 378 if (bounds is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
379 NSAutoreleasePool pool = null; 379 NSAutoreleasePool pool = null;
380 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 380 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
381 try { 381 try {
382 init_(bounds.width, bounds.height); 382 init_(bounds.width, bounds.height);
383 init_(); 383 init_();
384 } finally { 384 } finally {
385 if (pool !is null) pool.release(); 385 if (pool !is null) pool.release();
405 * </ul> 405 * </ul>
406 */ 406 */
407 public this(Device device, ImageData data) { 407 public this(Device device, ImageData data) {
408 super(device); 408 super(device);
409 NSAutoreleasePool pool = null; 409 NSAutoreleasePool pool = null;
410 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 410 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
411 try { 411 try {
412 init_(data); 412 init_(data);
413 init_(); 413 init_();
414 } finally { 414 } finally {
415 if (pool !is null) pool.release(); 415 if (pool !is null) pool.release();
447 if (mask is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 447 if (mask is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
448 if (source.width !is mask.width || source.height !is mask.height) { 448 if (source.width !is mask.width || source.height !is mask.height) {
449 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 449 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
450 } 450 }
451 NSAutoreleasePool pool = null; 451 NSAutoreleasePool pool = null;
452 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 452 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
453 try { 453 try {
454 mask = ImageData.convertMask(mask); 454 mask = ImageData.convertMask(mask);
455 ImageData image = new ImageData(source.width, source.height, source.depth, source.palette, source.scanlinePad, source.data); 455 ImageData image = new ImageData(source.width, source.height, source.depth, source.palette, source.scanlinePad, source.data);
456 image.maskPad = mask.scanlinePad; 456 image.maskPad = mask.scanlinePad;
457 image.maskData = mask.data; 457 image.maskData = mask.data;
561 } 561 }
562 562
563 void createAlpha () { 563 void createAlpha () {
564 if (transparentPixel is -1 && alpha is -1 && alphaData is null) return; 564 if (transparentPixel is -1 && alpha is -1 && alphaData is null) return;
565 NSAutoreleasePool pool = null; 565 NSAutoreleasePool pool = null;
566 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 566 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
567 try { 567 try {
568 NSSize size = handle.size(); 568 NSSize size = handle.size();
569 int height = cast(int)size.height; 569 int height = cast(int)size.height;
570 NSInteger bpr = imageRep.bytesPerRow(); 570 NSInteger bpr = imageRep.bytesPerRow();
571 size_t dataSize = height * bpr; 571 size_t dataSize = height * bpr;
667 * </ul> 667 * </ul>
668 */ 668 */
669 public Rectangle getBounds() { 669 public Rectangle getBounds() {
670 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 670 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
671 NSAutoreleasePool pool = null; 671 NSAutoreleasePool pool = null;
672 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 672 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
673 try { 673 try {
674 if (width !is -1 && height !is -1) { 674 if (width !is -1 && height !is -1) {
675 return new Rectangle(0, 0, width, height); 675 return new Rectangle(0, 0, width, height);
676 } 676 }
677 NSSize size = handle.size(); 677 NSSize size = handle.size();
696 * @see ImageData 696 * @see ImageData
697 */ 697 */
698 public ImageData getImageData() { 698 public ImageData getImageData() {
699 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 699 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
700 NSAutoreleasePool pool = null; 700 NSAutoreleasePool pool = null;
701 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 701 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
702 try { 702 try {
703 NSSize size = handle.size(); 703 NSSize size = handle.size();
704 int width = cast(int)size.width; 704 int width = cast(int)size.width;
705 int height = cast(int)size.height; 705 int height = cast(int)size.height;
706 NSBitmapImageRep imageRep = this.imageRep; 706 NSBitmapImageRep imageRep = this.imageRep;
709 size_t dataSize = height * bpr; 709 size_t dataSize = height * bpr;
710 byte[] srcData = new byte[dataSize]; 710 byte[] srcData = new byte[dataSize];
711 OS.memmove(srcData.ptr, imageRep.bitmapData(), dataSize); 711 OS.memmove(srcData.ptr, imageRep.bitmapData(), dataSize);
712 712
713 PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF); 713 PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
714 ImageData data = new ImageData(width, height, (int)/*64*/bpp, palette, 4, srcData); 714 ImageData data = new ImageData(width, height, cast(int)/*64*/bpp, palette, 4, srcData);
715 data.bytesPerLine = (int)/*64*/bpr; 715 data.bytesPerLine = cast(int)/*64*/bpr;
716 716
717 data.transparentPixel = transparentPixel; 717 data.transparentPixel = transparentPixel;
718 if (transparentPixel is -1 && type is DWT.ICON) { 718 if (transparentPixel is -1 && type is DWT.ICON) {
719 /* Get the icon mask data */ 719 /* Get the icon mask data */
720 int maskPad = 2; 720 int maskPad = 2;
769 public static Image cocoa_new(Device device, int type, NSImage nsImage) { 769 public static Image cocoa_new(Device device, int type, NSImage nsImage) {
770 Image image = new Image(device); 770 Image image = new Image(device);
771 image.type = type; 771 image.type = type;
772 image.handle = nsImage; 772 image.handle = nsImage;
773 NSAutoreleasePool pool = null; 773 NSAutoreleasePool pool = null;
774 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 774 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
775 try { 775 try {
776 NSImageRep rep = nsImage.bestRepresentationForDevice(null); 776 NSImageRep rep = nsImage.bestRepresentationForDevice(null);
777 if (rep.isKindOfClass(OS.class_NSBitmapImageRep)) { 777 if (rep.isKindOfClass(cast(objc.Class)OS.class_NSBitmapImageRep)) {
778 rep.retain(); 778 rep.retain();
779 image.imageRep = new NSBitmapImageRep(rep.id_); 779 image.imageRep = new NSBitmapImageRep(rep.id);
780 } 780 }
781 return image; 781 return image;
782 } finally { 782 } finally {
783 if (pool !is null) pool.release(); 783 if (pool !is null) pool.release();
784 } 784 }
793 * @return the receiver's hash 793 * @return the receiver's hash
794 * 794 *
795 * @see #equals 795 * @see #equals
796 */ 796 */
797 public hash_t toHash () { 797 public hash_t toHash () {
798 return handle !is null ? cast(hash_t) handle.id_ : 0; 798 return handle !is null ? cast(hash_t) handle.id : 0;
799 } 799 }
800 800
801 alias toHash hashCode; 801 alias toHash hashCode;
802 802
803 void init_(int width, int height) { 803 void init_(int width, int height) {
812 NSSize size = NSSize(); 812 NSSize size = NSSize();
813 size.width = width; 813 size.width = width;
814 size.height = height; 814 size.height = height;
815 handle = handle.initWithSize(size); 815 handle = handle.initWithSize(size);
816 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc(); 816 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
817 rep = rep.initWithBitmapDataPlanes_(null, width, height, 8, 3, false, false, OS.NSDeviceRGBColorSpace, NSAlphaFirstBitmapFormat | NSAlphaNonpremultipliedBitmapFormat, width * 4, 32); 817 rep = rep.initWithBitmapDataPlanes(null, width, height, 8, 3, false, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, width * 4, 32);
818 OS.memset(rep.bitmapData(), 0xFF, cast(size_t) (width * height * 4)); 818 OS.memset(rep.bitmapData(), 0xFF, cast(size_t) (width * height * 4));
819 handle.addRepresentation(rep); 819 handle.addRepresentation(rep);
820 // rep.release(); 820 // rep.release();
821 } 821 }
822 822
921 NSSize size = NSSize(); 921 NSSize size = NSSize();
922 size.width = width; 922 size.width = width;
923 size.height = height; 923 size.height = height;
924 handle = handle.initWithSize(size); 924 handle = handle.initWithSize(size);
925 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc(); 925 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
926 rep = rep.initWithBitmapDataPlanes(0, width, height, 8, hasAlpha ? 4 : 3, hasAlpha, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, bpr, 32); 926 rep = rep.initWithBitmapDataPlanes(null, width, height, 8, hasAlpha ? 4 : 3, hasAlpha, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, bpr, 32);
927 OS.memmove(rep.bitmapData(), buffer.ptr, dataSize); 927 OS.memmove(rep.bitmapData(), buffer.ptr, dataSize);
928 handle.addRepresentation(rep); 928 handle.addRepresentation(rep);
929 // rep.release(); 929 // rep.release();
930 } 930 }
931 931
946 if (handle is null) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 946 if (handle is null) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
947 if (type !is DWT.BITMAP || memGC !is null) { 947 if (type !is DWT.BITMAP || memGC !is null) {
948 DWT.error(DWT.ERROR_INVALID_ARGUMENT); 948 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
949 } 949 }
950 NSAutoreleasePool pool = null; 950 NSAutoreleasePool pool = null;
951 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); 951 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
952 try { 952 try {
953 NSBitmapImageRep rep = imageRep; 953 NSBitmapImageRep rep = imageRep;
954 if (imageRep.hasAlpha()) { 954 if (imageRep.hasAlpha()) {
955 NSInteger bpr = width * 4; 955 NSInteger bpr = width * 4;
956 rep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc(); 956 rep = cast(NSBitmapImageRep)(new NSBitmapImageRep()).alloc();
957 objc.id bitmapData = imageRep.bitmapData(); 957 objc.id bitmapData = imageRep.bitmapData();
958 if (data.bitmapDataAddress !is null) OS.free(data.bitmapDataAddress); 958 if (data.bitmapDataAddress !is null) OS.free(data.bitmapDataAddress);
959 data.bitmapDataAddress = cast(ubyte*) OS.malloc(C.PTR_SIZEOF); 959 data.bitmapDataAddress = cast(ubyte*) OS.malloc(C.PTR_SIZEOF);
960 OS.memmove(data.bitmapDataAddress, bitmapData, C.PTR_SIZEOF); 960 OS.memmove(data.bitmapDataAddress, bitmapData, C.PTR_SIZEOF);
961 rep = rep.initWithBitmapDataPlanes(data.bitmapDataAddress, width, height, 8, 3, false, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat , bpr, 32); 961 rep = rep.initWithBitmapDataPlanes(&data.bitmapDataAddress, width, height, 8, 3, false, false, OS.NSDeviceRGBColorSpace, OS.NSAlphaFirstBitmapFormat , bpr, 32);
962 rep.autorelease(); 962 rep.autorelease();
963 } 963 }
964 handle.setCacheMode(OS.NSImageCacheNever); 964 handle.setCacheMode(OS.NSImageCacheNever);
965 NSGraphicsContext context = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep); 965 NSGraphicsContext context = NSGraphicsContext.graphicsContextWithBitmapImageRep(rep);
966 NSGraphicsContext.setCurrentContext(context); 966 NSGraphicsContext.setCurrentContext(context);
978 data.background = device.COLOR_WHITE.handle; 978 data.background = device.COLOR_WHITE.handle;
979 data.foreground = device.COLOR_BLACK.handle; 979 data.foreground = device.COLOR_BLACK.handle;
980 data.font = device.systemFont; 980 data.font = device.systemFont;
981 data.image = this; 981 data.image = this;
982 } 982 }
983 return context.id_; 983 return context.id;
984 } finally { 984 } finally {
985 if (pool !is null) pool.release(); 985 if (pool !is null) pool.release();
986 } 986 }
987 } 987 }
988 988