开发者

signed int to unsigned byte and vice versa

开发者 https://www.devze.com 2023-02-27 00:36 出处:网络
I have a signed value e.g. -7368817 when I cast it to byte it will be something like: -113 I convert it to unsigned byte bye & 0xff and it will be something like 143

I have a signed value e.g. -7368817 when I cast it to byte it will be something like: -113 I convert it to unsigned byte bye & 0xff and it will be something like 143 now I manipulate this byte value and after that I want to whole way back! to get the signed integer of the new byte. :)

Update

The whole story:

I have an image which is 8-bit depth and gray scale! it means that all pixels are presented using 1 byte

BufferedImage image = ImageIO.read(new File("my_grayscal_8bit_photo.jpg"));

int intPixel = image.getRGB(1, 1);

now I need some bit manipulation in this pixel but since it is int I must convert it to byte first:

byte bytePixel = (byte) intPixel;

and to make it unsigned:

int intPixel2 = bytePixel & 0xff;

now I do my bit manipulation and want to convert it to int and do:

im开发者_开发问答age.setRGB(1, 1, neworiginalint);


The getRGB(int x, int y) method always returns an int pixel in the TYPE_INT_ARGB color model. To manually extract the red, green, blue and alpha values for the pixel you can do this:

int pixel = image.getRGB(1, 1);
int a = (pixel >> 24) & 0xFF;
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;

Or use the Color(int rgba, boolean hasalpha) constructor for convenience (at the cost of performance). Once you've manipulated the red, green, blue and alpha values (in the range of 0 to 255) you can recombine them back into an int for setting pixels:

int newPixel = (a << 24) | (r << 16) | (g << 8) | b;

Using the -7368817 pixel you mentioned with this code, the alpha is 255 (so no transparency) and the red, green and blue values are all 143. Since you're dealing with grayscale you could just pick any of red, green or blue to get the gray value. However on setting the pixel you're going to have set all three to maintain the grayscale since it's RGB. You could shortcut it a bit like so:

int pixel = image.getRGB(1, 1);

// extract your gray value from blue, assume red and green are same
int gray = pixel & 0xFF; 

// this method does your manipulation on the gray value, 0 to 255
gray = manipulate(gray);

// recombine back into int, preserving the original alpha
int newPixel = (pixel & 0xFF000000) | (gray << 16) | (gray << 8) | gray;

// now you can set your new pixel
image.setRGB(1, 1, nexPixel);

Basically the trick is to use int as your unsigned byte. Just make sure you keep the values from 0 to 255 and everything should work fine.


Are you asking how to manipulate the least-significant byte in an signed int, without touching the more significant byte?
This can be done like this:

int i = -7368817; // 0xFF8F 8F8F
int b = i & 0xFF; // "signed byte", = 0x8F = 143
b += 0x2C; // your manipulation, result = 0xBB
i = (i & 0xFFFFFF00) | (b & 0xFF); // result of LSB modification = 0xFF8F8FBB

or in one step: i = (i & 0xFFFFFF00) | ((i + 0x2C) & 0xFF); if it is a simple manipulation.

If the manipulation can't ever produce an overflow, you can simply do it on the whole int:
i ^= 0x34; // i = 0xFF8F8FBB

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号