Java buffered greyscale image byte and int -


please, please can explain below me. driving me nuts. playing creating 8-bit grayscale images arrays in java.

the code below should produce black > white horiz gradients. parts 1 , 2 produce desired image, in int[] passed getimagefromarray in 1 goes -128 127, , in 2 goes 0 255, yet produce same image. 3 produces (undesired) image expected 1 produce, going on max , min values alone.

why this? how can be?

import java.awt.image.bufferedimage; import java.awt.image.writableraster; import java.io.fileoutputstream; import java.io.ioexception;  import javax.imageio.imageio;  public class test {      final static int width = 320;     final static int height = 256;      // black , white int     final static int black = 0;     final static int white = 255;     //...same, not     final static int black_wrong = 127;     final static int white_wrong = -128;      // black , white byte val     final static byte black_byte = -0x80; //i.e -128     final static byte white_byte = 0x7f; // i.e. 127        public static void main(string[] args) throws ioexception {           int[] pixels = new int[width*height];         int[] m;          // generates gradient1.bmp         // produces expected - black @ top, white @ bottom         int numb=0,c=0;         byte grey1 = black;         while (numb<pixels.length){              // inc through greyscales down image             if (c>width){                 grey1++;                 c=0;             }                     // cast byte int             pixels[numb] = grey1;               // inc column , count             c++;             numb++;         }          m = getmaxmin(pixels); // max 127 , min -128         system.out.printf("maxmin %s; %s;\n",m[0], m[1]);         getimagefromarray("gradient1.bmp", pixels,width, height);          //*************************************************************************         // generates gradient3.bmp         // produces expected - black @ top, white @ bottom          numb=0;              c=0;         int grey2 = black; //i.e 0         while (numb<pixels.length){              // inc through greyscales down image             if (c>width){                 grey2++;                 c=0;             }                     // no cast             pixels[numb] = grey2;               // inc column , count             c++;             numb++;         }          m = getmaxmin(pixels); // max 255, min 0         system.out.printf("maxmin %s; %s;\n",m[0], m[1]);         getimagefromarray("gradient2.bmp", pixels,width, height);          //*************************************************************************         // generates gradient3.bmp         // produces unexpected - midgrey > white. black > midgrey         numb=0;              c=0;         byte grey3 = black_byte; //i.e 0         while (numb<pixels.length){              // inc through greyscales down image             if (c>width){                 grey3++;                 c=0;             }                     // no cast             pixels[numb] = grey3;               // inc column , count             c++;             numb++;         }          m = getmaxmin(pixels); // max 127 , min -128         system.out.printf("maxmin %s; %s;\n",m[0], m[1]);         getimagefromarray("gradient3.bmp", pixels,width, height);     }    //*******************************************************************************     static int swidth,sheight = 0;             static bufferedimage simage = null;     static writableraster sraster=null;      public static bufferedimage getimagefromarray(string filename, int pixels[], int width, int height) throws ioexception {         if (simage == null){         simage = new bufferedimage(width, height, bufferedimage.type_byte_gray);         sraster = simage.getraster();         }          sraster.setpixels(0,0,width,height,pixels);         try {             imageio.write(simage, "bmp", new fileoutputstream(filename));         } catch (ioexception e) {             e.printstacktrace();         }         return simage;     }       static int[] getmaxmin(int[] v){         int max=0,min=0;         (int i:v){             if (i>max) max = i;             if (i<min) min = i;         }         int[] r = {max,min};         return r;     } } 

assuming bufferedimage takes rgba (or similarly) encoded colors input, needs construct ints respective rgba components.

to convert signed bytes r = 0, g = 0, b = 0, a = 127 gray, you'd have convert them unsigned ints , combine them 1 int, so:

int color = (((a & 0xff) << 24 | ((b & 0xff) << 16) | ((g << 8) | (r & 0xff)); 

this same assigning hex values of 7f 7f 7f ff. & 0xff masking necessary convert signed bytes (-128 127) unsigned int (0 255).

the << 'left shifts' bytes around inside int.

you may have change input order of rgba correct result.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -