@@ -267,11 +267,13 @@ public void drawString(String str, int x, int y, int anchor)
267
267
final int ascent = metrics .getAscent ();
268
268
269
269
x = AnchorX (x , strWidth , anchor );
270
- y = y + ascent - 1 ;
271
- y = AnchorY (y , strHeight , anchor );
270
+ y = AnchorY (y + ascent , strHeight , anchor );
272
271
273
272
try { gc .drawString (str , x , y ); }
274
- catch (Exception e ) { }
273
+ catch (Exception e )
274
+ {
275
+ Mobile .log (Mobile .LOG_ERROR , PlatformGraphics .class .getPackage ().getName () + "." + PlatformGraphics .class .getSimpleName () + ": " + "drawString():" + e .getMessage ());
276
+ }
275
277
}
276
278
}
277
279
@@ -467,7 +469,7 @@ public void drawPixels(byte[] pixels, byte[] transparencyMask, int offset, int s
467
469
case DirectGraphics .TYPE_BYTE_1_GRAY_VERTICAL : // TYPE_BYTE_1_GRAY_VERTICAL - Used by Munkiki's Castles
468
470
int ods = offset / scanlength ;
469
471
int oms = offset % scanlength ;
470
- int b = ods % 8 ; // Bit offset in a byte
472
+ int b = ods % 8 ; // Bit offset in a byte, since GRAY_VERTICAL is packing 8 vertical pixel bits in a byte.
471
473
for (int yj = 0 ; yj < height ; yj ++)
472
474
{
473
475
int ypos = yj * width ;
@@ -487,22 +489,24 @@ public void drawPixels(byte[] pixels, byte[] transparencyMask, int offset, int s
487
489
break ;
488
490
489
491
case DirectGraphics .TYPE_BYTE_1_GRAY : // TYPE_BYTE_1_GRAY - Also used by Munkiki's Castles
490
- for (int i = (offset / 8 ); i < pixels .length ; i ++)
492
+ b = 7 - offset % 8 ;
493
+ for (int yj = 0 ; yj < height ; yj ++)
491
494
{
492
- for (int j = 7 ; j >= 0 ; j --)
495
+ int line = offset + yj * scanlength ;
496
+ int ypos = yj * width ;
497
+ for (int xj = 0 ; xj < width ; xj ++)
493
498
{
494
- int pixelIndex = (i * 8 ) + (7 - j );
495
-
496
- // Ensure we don't exceed data length based on image's width and height (as here the pixel can go out of bounds)
497
- if (pixelIndex >= width * height ) { break ; }
498
-
499
- c = ((pixels [i ] >> j ) & 1 );
499
+ c = ((pixels [(line + xj ) / 8 ] >> b ) & 1 );
500
500
if (transparencyMask != null )
501
501
{
502
- c |= (((transparencyMask [i ] >> j ) & 1 ) ^ 1 ) << 1 ;
502
+ c |= (((transparencyMask [( line + xj ) / 8 ] >> b ) & 1 ) ^ 1 ) << 1 ; // Apply transparency mask
503
503
}
504
- data [pixelIndex ] = Type1 [c ];
504
+ data [ypos + xj ] = Type1 [c ];
505
+ b --;
506
+ if (b < 0 ) b = 7 ;
505
507
}
508
+ b = b - (scanlength - width ) % 8 ;
509
+ if (b < 0 ) b = 8 + b ;
506
510
}
507
511
break ;
508
512
@@ -627,7 +631,57 @@ public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int arg
627
631
628
632
public void getPixels (byte [] pixels , byte [] transparencyMask , int offset , int scanlength , int x , int y , int width , int height , int format )
629
633
{
630
- Mobile .log (Mobile .LOG_WARNING , PlatformGraphics .class .getPackage ().getName () + "." + PlatformGraphics .class .getSimpleName () + ": " + "getPixels A" );
634
+ if (width <= 0 || height <= 0 ) { return ; } // We have no pixels to copy
635
+ if (pixels == null ) { throw new NullPointerException ("Byte array cannot be null" );}
636
+ if (x < 0 || y < 0 || x + width > canvas .getWidth () || y + height > canvas .getHeight ())
637
+ {
638
+ throw new IllegalArgumentException ("Requested copy area exceeds bounds of the image" );
639
+ }
640
+ if (Math .abs (scanlength ) < width ) { throw new IllegalArgumentException ("scanlength must be >= width" );}
641
+
642
+ // Temporary canvas data array to read raw pixel data from.
643
+ int [] canvasData = ((DataBufferInt ) canvas .getRaster ().getDataBuffer ()).getData ();
644
+
645
+ // Just like DrawPixels(byte), we only handle BYTE_1_GRAY_VERTICAL and BYTE_1_GRAY yet
646
+ switch (format )
647
+ {
648
+ case DirectGraphics .TYPE_BYTE_1_GRAY_VERTICAL :
649
+ for (int row = 0 ; row < height ; row ++)
650
+ {
651
+ for (int col = 0 ; col < width ; col ++)
652
+ {
653
+ int pixelIndex = (y + row ) * canvas .getWidth () + (x + col );
654
+ int pixelValue = canvasData [pixelIndex ];
655
+
656
+ // Store pixel value as a bit in the pixels array
657
+ int byteIndex = (offset + row ) * scanlength + (col / 8 );
658
+ int bitIndex = col % 8 ;
659
+
660
+ // Set the bit in the retrieved byte to the expected value.
661
+ pixels [byteIndex ] |= ((pixelValue & 0xFF ) != 0 ? 0 : 1 ) << (7 - bitIndex );
662
+ if (transparencyMask != null ) { transparencyMask [byteIndex ] |= ((pixelValue & 0xFF000000 ) != 0 ? 0 : 1 ) << (7 - bitIndex ); }
663
+ }
664
+ }
665
+ break ;
666
+
667
+ case DirectGraphics .TYPE_BYTE_1_GRAY : // Pretty similar to the one above
668
+ for (int row = 0 ; row < height ; row ++)
669
+ {
670
+ for (int col = 0 ; col < width ; col ++)
671
+ {
672
+ int pixelIndex = (y + row ) * canvas .getWidth () + (x + col );
673
+ int pixelValue = canvasData [pixelIndex ];
674
+ int byteIndex = (offset / 8 ) + ((row * width + col ) / 8 );
675
+ int bitIndex = (row * width + col ) % 8 ;
676
+
677
+ pixels [byteIndex ] |= ((pixelValue & 0xFF ) != 0 ? 0 : 1 ) << (7 - bitIndex );
678
+ if (transparencyMask != null ) { transparencyMask [byteIndex ] |= ((pixelValue & 0xFF000000 ) != 0 ? 0 : 1 ) << (7 - bitIndex ); }
679
+ }
680
+ }
681
+ break ;
682
+
683
+ default : Mobile .log (Mobile .LOG_WARNING , PlatformGraphics .class .getPackage ().getName () + "." + PlatformGraphics .class .getSimpleName () + ": " + "getPixels A : Format " + format + " Not Implemented" );
684
+ }
631
685
}
632
686
633
687
public void getPixels (int [] pixels , int offset , int scanlength , int x , int y , int width , int height , int format )
0 commit comments