Convert java code to pascal

Asked

Viewed 397 times

0

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

the main doubts are:

  • how to extract each pixel in firemonkey?

  • how this code operates: slice |= (byte) ((v ? 1 : 0) << (7 -b));

Follow the full 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);                  
                }
  • See if it helps http://stackoverflow.com/questions/tagged/firemonkey

1 answer

3

I’ll just answer that part :

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

Each part :

|=

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

a |= b -> a = a ou b

which 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 to the least significant byte. In pascal we do byte(expression), despite having 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 displacement operator of the language C, which in pascal can be replaced by shl.

All in all, I’d be :

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

I put V <> 0 why in practice the if (and the operator ? ) from c, consider as true anything other than 0 (actually C has no true bolean types).

  • perfect, clear, complete and didactic response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.