comparison dwt/graphics/Image.d @ 7:e831403a80a9

Add 'cast' to casts
author Frank Benoit <benoit@tionex.de>
date Wed, 27 Aug 2008 14:30:35 +0200
parents 1a8b3cb347e0
children a9ab4c738ed8
comparison
equal deleted inserted replaced
6:b903c16b6f48 7:e831403a80a9
222 device = this.device; 222 device = this.device;
223 this.type = srcImage.type; 223 this.type = srcImage.type;
224 224
225 /* Get source image size */ 225 /* Get source image size */
226 NSSize size = srcImage.handle.size(); 226 NSSize size = srcImage.handle.size();
227 int width = (int)size.width; 227 int width = cast(int)size.width;
228 int height = (int)size.height; 228 int height = cast(int)size.height;
229 NSBitmapImageRep srcRep = srcImage.imageRep; 229 NSBitmapImageRep srcRep = srcImage.imageRep;
230 int bpr = srcRep.bytesPerRow(); 230 int bpr = srcRep.bytesPerRow();
231 231
232 /* Copy transparent pixel and alpha data when necessary */ 232 /* Copy transparent pixel and alpha data when necessary */
233 transparentPixel = srcImage.transparentPixel; 233 transparentPixel = srcImage.transparentPixel;
236 alphaData = new byte[srcImage.alphaData.length]; 236 alphaData = new byte[srcImage.alphaData.length];
237 System.arraycopy(srcImage.alphaData, 0, alphaData, 0, alphaData.length); 237 System.arraycopy(srcImage.alphaData, 0, alphaData, 0, alphaData.length);
238 } 238 }
239 239
240 /* Create the image */ 240 /* Create the image */
241 handle = (NSImage)new NSImage().alloc(); 241 handle = cast(NSImage)new NSImage().alloc();
242 handle = handle.initWithSize(size); 242 handle = handle.initWithSize(size);
243 NSBitmapImageRep rep = imageRep = (NSBitmapImageRep)new NSBitmapImageRep().alloc(); 243 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc();
244 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, width, height, srcRep.bitsPerSample(), srcRep.samplesPerPixel(), srcRep.samplesPerPixel() is 4, srcRep.isPlanar(), new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, srcRep.bytesPerRow(), srcRep.bitsPerPixel()); 244 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, width, height, srcRep.bitsPerSample(), srcRep.samplesPerPixel(), srcRep.samplesPerPixel() is 4, srcRep.isPlanar(), new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, srcRep.bytesPerRow(), srcRep.bitsPerPixel());
245 handle.addRepresentation(rep); 245 handle.addRepresentation(rep);
246 246
247 int data = rep.bitmapData(); 247 int data = rep.bitmapData();
248 OS.memmove(data, srcImage.imageRep.bitmapData(), width * height * 4); 248 OS.memmove(data, srcImage.imageRep.bitmapData(), width * height * 4);
251 /* Apply transformation */ 251 /* Apply transformation */
252 switch (flag) { 252 switch (flag) {
253 case DWT.IMAGE_DISABLE: { 253 case DWT.IMAGE_DISABLE: {
254 Color zeroColor = device.getSystemColor(DWT.COLOR_WIDGET_NORMAL_SHADOW); 254 Color zeroColor = device.getSystemColor(DWT.COLOR_WIDGET_NORMAL_SHADOW);
255 RGB zeroRGB = zeroColor.getRGB(); 255 RGB zeroRGB = zeroColor.getRGB();
256 byte zeroRed = (byte)zeroRGB.red; 256 byte zeroRed = cast(byte)zeroRGB.red;
257 byte zeroGreen = (byte)zeroRGB.green; 257 byte zeroGreen = cast(byte)zeroRGB.green;
258 byte zeroBlue = (byte)zeroRGB.blue; 258 byte zeroBlue = cast(byte)zeroRGB.blue;
259 Color oneColor = device.getSystemColor(DWT.COLOR_WIDGET_BACKGROUND); 259 Color oneColor = device.getSystemColor(DWT.COLOR_WIDGET_BACKGROUND);
260 RGB oneRGB = oneColor.getRGB(); 260 RGB oneRGB = oneColor.getRGB();
261 byte oneRed = (byte)oneRGB.red; 261 byte oneRed = cast(byte)oneRGB.red;
262 byte oneGreen = (byte)oneRGB.green; 262 byte oneGreen = cast(byte)oneRGB.green;
263 byte oneBlue = (byte)oneRGB.blue; 263 byte oneBlue = cast(byte)oneRGB.blue;
264 byte[] line = new byte[bpr]; 264 byte[] line = new byte[bpr];
265 for (int y=0; y<height; y++) { 265 for (int y=0; y<height; y++) {
266 OS.memmove(line, data + (y * bpr), bpr); 266 OS.memmove(line, data + (y * bpr), bpr);
267 int offset = 0; 267 int offset = 0;
268 for (int x=0; x<width; x++) { 268 for (int x=0; x<width; x++) {
292 int offset = 0; 292 int offset = 0;
293 for (int x=0; x<width; x++) { 293 for (int x=0; x<width; x++) {
294 int red = line[offset+1] & 0xFF; 294 int red = line[offset+1] & 0xFF;
295 int green = line[offset+2] & 0xFF; 295 int green = line[offset+2] & 0xFF;
296 int blue = line[offset+3] & 0xFF; 296 int blue = line[offset+3] & 0xFF;
297 byte intensity = (byte)((red+red+green+green+green+green+green+blue) >> 3); 297 byte intensity = cast(byte)((red+red+green+green+green+green+green+blue) >> 3);
298 line[offset+1] = line[offset+2] = line[offset+3] = intensity; 298 line[offset+1] = line[offset+2] = line[offset+3] = intensity;
299 offset += 4; 299 offset += 4;
300 } 300 }
301 OS.memmove(data + (y * bpr), line, bpr); 301 OS.memmove(data + (y * bpr), line, bpr);
302 } 302 }
495 } 495 }
496 496
497 void createAlpha () { 497 void createAlpha () {
498 if (transparentPixel is -1 && alpha is -1 && alphaData is null) return; 498 if (transparentPixel is -1 && alpha is -1 && alphaData is null) return;
499 NSSize size = handle.size(); 499 NSSize size = handle.size();
500 int height = (int)size.height; 500 int height = cast(int)size.height;
501 int bpr = imageRep.bytesPerRow(); 501 int bpr = imageRep.bytesPerRow();
502 int dataSize = height * bpr; 502 int dataSize = height * bpr;
503 byte[] srcData = new byte[dataSize]; 503 byte[] srcData = new byte[dataSize];
504 OS.memmove(srcData, imageRep.bitmapData(), dataSize); 504 OS.memmove(srcData, imageRep.bitmapData(), dataSize);
505 if (transparentPixel !is -1) { 505 if (transparentPixel !is -1) {
506 for (int i=0; i<dataSize; i+=4) { 506 for (int i=0; i<dataSize; i+=4) {
507 int pixel = ((srcData[i+1] & 0xFF) << 16) | ((srcData[i+2] & 0xFF) << 8) | (srcData[i+3] & 0xFF); 507 int pixel = ((srcData[i+1] & 0xFF) << 16) | ((srcData[i+2] & 0xFF) << 8) | (srcData[i+3] & 0xFF);
508 srcData[i] = (byte)(pixel is transparentPixel ? 0 : 0xFF); 508 srcData[i] = cast(byte)(pixel is transparentPixel ? 0 : 0xFF);
509 } 509 }
510 } else if (alpha !is -1) { 510 } else if (alpha !is -1) {
511 byte a = (byte)this.alpha; 511 byte a = cast(byte)this.alpha;
512 for (int i=0; i<dataSize; i+=4) { 512 for (int i=0; i<dataSize; i+=4) {
513 srcData[i] = a; 513 srcData[i] = a;
514 } 514 }
515 } else { 515 } else {
516 int width = (int)size.width; 516 int width = cast(int)size.width;
517 int offset = 0, alphaOffset = 0; 517 int offset = 0, alphaOffset = 0;
518 for (int y = 0; y<height; y++) { 518 for (int y = 0; y<height; y++) {
519 for (int x = 0; x<width; x++) { 519 for (int x = 0; x<width; x++) {
520 srcData[offset] = alphaData[alphaOffset]; 520 srcData[offset] = alphaData[alphaOffset];
521 offset += 4; 521 offset += 4;
546 * @see #hashCode 546 * @see #hashCode
547 */ 547 */
548 public bool equals (Object object) { 548 public bool equals (Object object) {
549 if (object is this) return true; 549 if (object is this) return true;
550 if (!(object instanceof Image)) return false; 550 if (!(object instanceof Image)) return false;
551 Image image = (Image)object; 551 Image image = cast(Image)object;
552 return device is image.device && handle is image.handle && 552 return device is image.device && handle is image.handle &&
553 transparentPixel is image.transparentPixel; 553 transparentPixel is image.transparentPixel;
554 } 554 }
555 555
556 /** 556 /**
596 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 596 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
597 if (width !is -1 && height !is -1) { 597 if (width !is -1 && height !is -1) {
598 return new Rectangle(0, 0, width, height); 598 return new Rectangle(0, 0, width, height);
599 } 599 }
600 NSSize size = handle.size(); 600 NSSize size = handle.size();
601 return new Rectangle(0, 0, width = (int)size.width, height = (int)size.height); 601 return new Rectangle(0, 0, width = cast(int)size.width, height = cast(int)size.height);
602 } 602 }
603 603
604 /** 604 /**
605 * Returns an <code>ImageData</code> based on the receiver 605 * Returns an <code>ImageData</code> based on the receiver
606 * Modifications made to this <code>ImageData</code> will not 606 * Modifications made to this <code>ImageData</code> will not
617 */ 617 */
618 public ImageData getImageData() { 618 public ImageData getImageData() {
619 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 619 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
620 620
621 NSSize size = handle.size(); 621 NSSize size = handle.size();
622 int width = (int)size.width; 622 int width = cast(int)size.width;
623 int height = (int)size.height; 623 int height = cast(int)size.height;
624 NSBitmapImageRep imageRep = this.imageRep; 624 NSBitmapImageRep imageRep = this.imageRep;
625 int bpr = imageRep.bytesPerRow(); 625 int bpr = imageRep.bytesPerRow();
626 int bpp = imageRep.bitsPerPixel(); 626 int bpp = imageRep.bitsPerPixel();
627 int dataSize = height * bpr; 627 int dataSize = height * bpr;
628 byte[] srcData = new byte[dataSize]; 628 byte[] srcData = new byte[dataSize];
713 } 713 }
714 this.type = DWT.BITMAP; 714 this.type = DWT.BITMAP;
715 this.width = width; 715 this.width = width;
716 this.height = height; 716 this.height = height;
717 717
718 handle = (NSImage)new NSImage().alloc(); 718 handle = cast(NSImage)new NSImage().alloc();
719 NSSize size = new NSSize(); 719 NSSize size = new NSSize();
720 size.width = width; 720 size.width = width;
721 size.height = height; 721 size.height = height;
722 handle = handle.initWithSize(size); 722 handle = handle.initWithSize(size);
723 NSBitmapImageRep rep = imageRep = (NSBitmapImageRep)new NSBitmapImageRep().alloc(); 723 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc();
724 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, width, height, 8, 3, false, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, width * 4, 32); 724 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(0, width, height, 8, 3, false, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, width * 4, 32);
725 OS.memset(rep.bitmapData(), 0xFF, width * height * 4); 725 OS.memset(rep.bitmapData(), 0xFF, width * height * 4);
726 handle.addRepresentation(rep); 726 handle.addRepresentation(rep);
727 // rep.release(); 727 // rep.release();
728 } 728 }
755 byte[] srcGreens = new byte[length]; 755 byte[] srcGreens = new byte[length];
756 byte[] srcBlues = new byte[length]; 756 byte[] srcBlues = new byte[length];
757 for (int i = 0; i < rgbs.length; i++) { 757 for (int i = 0; i < rgbs.length; i++) {
758 RGB rgb = rgbs[i]; 758 RGB rgb = rgbs[i];
759 if (rgb is null) continue; 759 if (rgb is null) continue;
760 srcReds[i] = (byte)rgb.red; 760 srcReds[i] = cast(byte)rgb.red;
761 srcGreens[i] = (byte)rgb.green; 761 srcGreens[i] = cast(byte)rgb.green;
762 srcBlues[i] = (byte)rgb.blue; 762 srcBlues[i] = cast(byte)rgb.blue;
763 } 763 }
764 ImageData.blit(ImageData.BLIT_SRC, 764 ImageData.blit(ImageData.BLIT_SRC,
765 image.data, image.depth, image.bytesPerLine, image.getByteOrder(), 0, 0, width, height, srcReds, srcGreens, srcBlues, 765 image.data, image.depth, image.bytesPerLine, image.getByteOrder(), 0, 0, width, height, srcReds, srcGreens, srcBlues,
766 ImageData.ALPHA_OPAQUE, null, 0, 0, 0, 766 ImageData.ALPHA_OPAQUE, null, 0, 0, 0,
767 buffer, 32, bpr, ImageData.MSB_FIRST, 0, 0, width, height, 0xFF0000, 0xFF00, 0xFF, 767 buffer, 32, bpr, ImageData.MSB_FIRST, 0, 0, width, height, 0xFF0000, 0xFF00, 0xFF,
795 byte[] maskData = maskImage.data; 795 byte[] maskData = maskImage.data;
796 int maskBpl = maskImage.bytesPerLine; 796 int maskBpl = maskImage.bytesPerLine;
797 int offset = 0, maskOffset = 0; 797 int offset = 0, maskOffset = 0;
798 for (int y = 0; y<height; y++) { 798 for (int y = 0; y<height; y++) {
799 for (int x = 0; x<width; x++) { 799 for (int x = 0; x<width; x++) {
800 buffer[offset] = ((maskData[maskOffset + (x >> 3)]) & (1 << (7 - (x & 0x7)))) !is 0 ? (byte)0xff : 0; 800 buffer[offset] = ((maskData[maskOffset + (x >> 3)]) & (1 << (7 - (x & 0x7)))) !is 0 ? cast(byte)0xff : 0;
801 offset += 4; 801 offset += 4;
802 } 802 }
803 maskOffset += maskBpl; 803 maskOffset += maskBpl;
804 } 804 }
805 } else { 805 } else {
806 this.type = DWT.BITMAP; 806 this.type = DWT.BITMAP;
807 if (image.alpha !is -1) { 807 if (image.alpha !is -1) {
808 hasAlpha = true; 808 hasAlpha = true;
809 this.alpha = image.alpha; 809 this.alpha = image.alpha;
810 byte a = (byte)this.alpha; 810 byte a = cast(byte)this.alpha;
811 for (int dataIndex=0; dataIndex<buffer.length; dataIndex+=4) { 811 for (int dataIndex=0; dataIndex<buffer.length; dataIndex+=4) {
812 buffer[dataIndex] = a; 812 buffer[dataIndex] = a;
813 } 813 }
814 } else if (image.alphaData !is null) { 814 } else if (image.alphaData !is null) {
815 this.alphaData = new byte[image.alphaData.length]; 815 this.alphaData = new byte[image.alphaData.length];
822 alphaOffset += 1; 822 alphaOffset += 1;
823 } 823 }
824 } 824 }
825 } 825 }
826 } 826 }
827 handle = (NSImage)new NSImage().alloc(); 827 handle = cast(NSImage)new NSImage().alloc();
828 NSSize size = new NSSize(); 828 NSSize size = new NSSize();
829 size.width = width; 829 size.width = width;
830 size.height = height; 830 size.height = height;
831 handle = handle.initWithSize(size); 831 handle = handle.initWithSize(size);
832 NSBitmapImageRep rep = imageRep = (NSBitmapImageRep)new NSBitmapImageRep().alloc(); 832 NSBitmapImageRep rep = imageRep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc();
833 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( 833 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(
834 0, width, height, 8, hasAlpha ? 4 : 3, hasAlpha, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, bpr, 32); 834 0, width, height, 8, hasAlpha ? 4 : 3, hasAlpha, false, new NSString(OS.NSDeviceRGBColorSpace()), OS.NSAlphaFirstBitmapFormat | OS.NSAlphaNonpremultipliedBitmapFormat, bpr, 32);
835 OS.memmove(rep.bitmapData(), buffer, dataSize); 835 OS.memmove(rep.bitmapData(), buffer, dataSize);
836 handle.addRepresentation(rep); 836 handle.addRepresentation(rep);
837 // rep.release(); 837 // rep.release();
857 } 857 }
858 NSGraphicsContext current = NSGraphicsContext.currentContext(); 858 NSGraphicsContext current = NSGraphicsContext.currentContext();
859 NSBitmapImageRep rep = imageRep; 859 NSBitmapImageRep rep = imageRep;
860 if (imageRep.hasAlpha()) { 860 if (imageRep.hasAlpha()) {
861 int bpr = width * 4; 861 int bpr = width * 4;
862 rep = (NSBitmapImageRep)new NSBitmapImageRep().alloc(); 862 rep = cast(NSBitmapImageRep)new NSBitmapImageRep().alloc();
863 int bitmapData = imageRep.bitmapData(); 863 int bitmapData = imageRep.bitmapData();
864 if (data.bitmapDataAddress !is 0) OS.free(data.bitmapDataAddress); 864 if (data.bitmapDataAddress !is 0) OS.free(data.bitmapDataAddress);
865 data.bitmapDataAddress = OS.malloc(4); 865 data.bitmapDataAddress = OS.malloc(4);
866 OS.memmove(data.bitmapDataAddress, new int[] {bitmapData}, 4); 866 OS.memmove(data.bitmapDataAddress, new int[] {bitmapData}, 4);
867 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_( 867 rep = rep.initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(
959 public void setBackground(Color color) { 959 public void setBackground(Color color) {
960 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 960 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
961 if (color is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 961 if (color is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
962 if (color.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 962 if (color.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
963 if (transparentPixel is -1) return; 963 if (transparentPixel is -1) return;
964 // byte red = (byte)((transparentPixel >> 16) & 0xFF); 964 // byte red = cast(byte)((transparentPixel >> 16) & 0xFF);
965 // byte green = (byte)((transparentPixel >> 8) & 0xFF); 965 // byte green = cast(byte)((transparentPixel >> 8) & 0xFF);
966 // byte blue = (byte)((transparentPixel >> 0) & 0xFF); 966 // byte blue = cast(byte)((transparentPixel >> 0) & 0xFF);
967 // byte newRed = (byte)((int)(color.handle[0] * 255) & 0xFF); 967 // byte newRed = cast(byte)(cast(int)(color.handle[0] * 255) & 0xFF);
968 // byte newGreen = (byte)((int)(color.handle[1] * 255) & 0xFF); 968 // byte newGreen = cast(byte)(cast(int)(color.handle[1] * 255) & 0xFF);
969 // byte newBlue = (byte)((int)(color.handle[2] * 255) & 0xFF); 969 // byte newBlue = cast(byte)(cast(int)(color.handle[2] * 255) & 0xFF);
970 // int height = OS.CGImageGetHeight(handle); 970 // int height = OS.CGImageGetHeight(handle);
971 // int bpl = OS.CGImageGetBytesPerRow(handle); 971 // int bpl = OS.CGImageGetBytesPerRow(handle);
972 // byte[] line = new byte[bpl]; 972 // byte[] line = new byte[bpl];
973 // for (int i = 0, offset = 0; i < height; i++, offset += bpl) { 973 // for (int i = 0, offset = 0; i < height; i++, offset += bpl) {
974 // OS.memmove(line, data + offset, bpl); 974 // OS.memmove(line, data + offset, bpl);