Convert java code to pascal

0

I need to convert the following java code to Pascal / Firemonkey:

The main questions are:

  • How to extract each pixel in firemonkey?

  • How does this code work: slice |= (byte) ((v ? 1 : 0) << (7 -b));

Follow the complete code:

public static boolean ImageToEsc(Bitmap im, OutputStream out, int bytes, Integer density)
        {           
            try 
            {           

                int wid = im.getWidth();
                int hei = im.getHeight();
                byte[] blen = new byte[2];

                int [] dots = new int[wid * hei];
                im.getPixels(dots, 0, wid, 0, 0, wid, hei);

                blen[0] = (byte) (wid / 8);
                blen[1] = (byte) bytes; 

                int offset = 0;
                while (offset < hei) 
                {               

                    for (int x = 0; x < wid; ++x) 
                    {
                        for (int k = 0; k < bytes; ++k) 
                        { 
                            byte slice = 0;

                            for (int b = 0; b < 8; ++b) 
                            {
                                int y = (((offset / 8) + k) * 8) + b;

                                int i = (y * wid) + x;

                                boolean v = false;
                                if (i < dots.length) {
                                    v = (dots[i]==Color.BLACK);
                                }

                                slice |= (byte) ((v ? 1 : 0) << (7 -b));
                            }

                            out.write(slice);
                        }
                    }                   
                    offset += (bytes * 8);                  
                }
    
asked by anonymous 14.09.2016 / 14:32

1 answer

3

I'll only answer this part:

slice |= (byte) ((v ? 1 : 0) << (7 -b));

Each part:

|=

The pipe operator "|" in java means OU operation, and when associated with "=" in short form it implies

a |= b -> a = a ou b

That in Pascal would be

a := a or b;

(byte) is a type coercion, which means that the result must be converted to the byte type, possibly being truncated in the least significant byte. In pascal we make byte (expression), although we have other means that can make more sense in the context.

v ? a : b

This is the ternary operator of C, which does not exist in pascal, should be replaced by an if:

If V Then
    Result := A
Else
    Result := B;

<< is the language shift operator C, which in pascal can be replaced by shl .

All in all, I would stay:

if V <> 0 then
    slice := byte(1 shl 7 - b)
else
    slice := byte(0 shl 7 - b)

I put V

18.09.2016 / 02:09