ibrahimyilmaz
ibrahimyilmaz

Reputation: 18889

how to convert int to uint8 in java

I write an image filter in Matlab and I want to translate it to java in order to use my android application. How can I convert short or int to uint8 data format.

BTW:I know there is no type like uint8 in java.

Any help will be greatly appreciated.

Matlab:

for iX = 1:imageX
    for iY = 1:imageY
        x(1:3) = input(iX,iY,:);
        output(iX, iY, :) = uint8(p1.*x.^5 + p2.*x.^4 + p3.*x.^3 + p4.*x.^2 + p5.*x + p6);
    end
end

Java:

for (int iX = 0; iX < width; iX++) {
            for (int iY = 0; iY < height; iY++) {
                Arrays.fill(x, bitmap.getPixel(iX, iY));
                short total = 0;
                for (int i = 0; i < 3; i++) {
                    total +=p1[i] * Math.pow(x[i], 5) + p2[i]
                            * Math.pow(x[i], 4) + p3[i] * Math.pow(x[i], 3)
                            + p4[i] * Math.pow(x[i], 2) + p5[i] * x[i] + p6[i];
                }
                output.setPixel(iX, iY, total);
            }
        }

Upvotes: 2

Views: 7886

Answers (5)

shuangwhywhy
shuangwhywhy

Reputation: 5625

Use a longer type in Java, for example, short<->uint8, int<->uint16, long<->uint32.

Or just use int for all those unsigned types under 32 bits and long for 32 bits:

When you operate on it, do & op in the end:

int i = 256 & 0xff;                             //uint8
int i = 65536 & 0xffff;                         //uint16
long i = (long) Math.pow(2, 32) & 0xffffffff;   //uint32;
// And So on

There is a "bit waste" as it always uses half of the type length. But in most cases, it won't be a serious problem.

Upvotes: 3

Mikhail
Mikhail

Reputation: 8028

This is an unfortunate limitation of java. This is what I would do.

  1. Don't worry about the size, use an char or int
  2. Write you own addition wrapper for byte using a bit mask to check the sign. This will be slow so I would go with #1. An alternative to this is to cast to a different data-type during the operation. Such as using an int before serializing the truncated version to a byte data-type.
  3. See Is there a Java library for unsigned number type wrappers?

Upvotes: 1

Jatin Khurana
Jatin Khurana

Reputation: 1175

There is no unsigned data type in java so u will have to go with one out of them

int,char,short,byte.

These all are signed data type in java but keep in mind the type conversion rules while u assign the value of one data type value to another.

Upvotes: 0

Tuto
Tuto

Reputation: 92

Compiler will make an transformation for you:

int value = 115;
byte transformed = (byte) value;
// value == transformed => true

Warning: In case of transforming int to a byte you risk loosing 3 * 8 bits of information:

int value2 = 357;
byte transformed2 = (byte) value2;
// value2 == transformed2 => false

Upvotes: 0

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

Don't create total. In matlab you handle each color component separately. Do same in java:

for (int iX = 0; iX < width; iX++) {
        for (int iY = 0; iY < height; iY++) {
            Arrays.fill(x, bitmap.getPixel(iX, iY));
            // Result color components.
            int[] res = new int[3];
            short total = 0;
            for (int i = 0; i < 3; i++) {
                res[i] = p1[i] * Math.pow(x[i], 5) + p2[i]
                        * Math.pow(x[i], 4) + p3[i] * Math.pow(x[i], 3)
                        + p4[i] * Math.pow(x[i], 2) + p5[i] * x[i] + p6[i];
                // Make sure final value is in range [0, 255]
                res[i] = Math.min(255, Math.max(0, res[i]));
            }
            output.setPixel(iX, iY, Color.rgb(res[0], res[1], res[2]);
        }
    }

Upvotes: 1

Related Questions