-4

I am receiving a stream of bits over the Ethernet. I am collecting the bits in a byte[] array in Java(I am collecting them in a byte[] because I think its relevant).The stream is a digitized image where every 10 bits represent a pixel. There are 1280*1024 pixels. Every pixel is represented by 10 bits. Hence,1280*1024*10 = 13107200 bits = 1638400 bytes is the image size.

sto_user
  • 31
  • 1
  • 8
  • jpgs are 8bit-per-color. it's not possible to have an "original" 10bit jpeg... – Marc B Dec 24 '15 at 16:06
  • Sounds like an incredibly inefficient way to transfer a jpeg. Jpeg images are a compressed format, transferring it this way would be like using a bitmap but without the higher image quality. – Steve Harris Dec 24 '15 at 16:13
  • Thank u @ Marc. I corrected my question. Is there a way to convert the 10bit to 8bit and make a jpg file out of it ? – sto_user Dec 24 '15 at 16:15
  • If the source format is supported by Java you can just read it in with `ImageIO.read` and then write it out as a jpg with `ImageIO.write`. But I'm guessing your 10 bit images are not supported so you have to write your own code for reading them in. – Tesseract Dec 24 '15 at 16:24
  • Can you give us some more details about the input format? 10 bits per pixel is not enough information. – Tesseract Dec 24 '15 at 16:26

2 Answers2

0

here's the solution - but if the 10 bits represent actually 8 bits with some 'nonsense' in the other two bits its better to cut that like b=b>>2 - if your image is color then it sounds strange but use all 10 bits

int[] pix=new int[1280*1024];
for(i=0; i<pix.length; i++) {
  read next ten bits put then in an int
  int b=read();
  pix[i]=0xff000000|b;
}
BufferedImage bim=new BufferedImage(1280, 1024, BufferedImage.TYPE_INT_RGB);
bim.setRGB(0, 0, 1280, 1024, pix, 0, 1280);
  try {
    ImageIO.write(bim, "jpg", new File(path+".jpg"));
  } catch (IOException ex) { ex.printStackTrace(); }
gpasch
  • 2,672
  • 3
  • 10
  • 12
  • Thank you for your reply. Sorry for my ignorance. I have another question. I have the binary file with me with the image data. The smallest unit I can read in Java is a byte. I am reading it using ByteArrayOutputStream() object and then converting it to .toByteArray(). How can I possibly read every 10bits from that file? – sto_user Dec 24 '15 at 17:28
  • Thank you. The pixels are actually intensities and hence I should probably make a gray scale image out of it. Is there a gray scale equivalent for setRGB ? Java has a parameter TYPE_USHORT_GRAY ,but setRGB accepts only int[]. How can this be handled ? – sto_user Jan 12 '16 at 12:36
0

Here is a method that can take a byte array and "split" it into groups of 10 bit. Each group is saved as an int.

static int[] getPixel(byte[] in) {
  int bits = 0, bitCount = 0, posOut = 0;
  int[] out = new int[(in.length * 8) / 10];
  for(int posIn = 0; posIn < in.length; posIn++) {
    bits = (bits << 8) | (in[posIn] & 0xFF);
    bitCount += 8;
    if(bitCount >= 10) {
      out[posOut++] = (bits >>> (bitCount - 10)) & 0x3FF;
      bitCount -= 10;
    }
  }
  return out;
}
Tesseract
  • 8,049
  • 2
  • 20
  • 37
  • I will try the above two pieces of code and let you know. Thank you so much. – sto_user Dec 24 '15 at 18:42
  • Thank you. My data in the binary file is unsigned. In that case, what do you suggest, how should I store them? Per pixel I have 10 bits. Instead of int[], may be char[] or short[] (both 16bits )could be a choice. But again short[] is signed. I am left out with just one option which is char[]. – sto_user Jan 12 '16 at 21:29
  • You can use long[], int[], short[] or char[]. It doesn't matter if the data type is signed or unsigned. The array elements just need to be at least 10 bit long. – Tesseract Jan 13 '16 at 16:03