DWk Posted September 7, 2005 Share Posted September 7, 2005 Ok, so I'm trying to convert an 24-bit colored bitmap to an 8-bit (256 colors) grayscale bitmap. I am able to convert the bitmap to a 24-bit greyscale (sort of, if you understand what I'm talking about), by using this: FileInputStream file = new FileInputStream(new File(archivo)); FileOutputStream gray = new FileOutputStream(new File("gray.bmp")); byte[] header = new byte[54]; byte[] data = new byte[921600]; file.read(header); file.read(data); byte[] redmatrix = new byte[921600]; byte[] greenmatrix = new byte[921600]; byte[] bluematrix = new byte[921600]; byte[] mix = new byte[921600]; int n; for (n=2; n+3 < 921600; n=n+3){ redmatrix[n] = (byte)(Math.round(((int)data[n-2] + (int)data[n-1] + (int)data[n])/3)); } for (n=1; n+3 < 921600; n=n+3){ greenmatrix[n] = (byte)(Math.round(((int)data[n-1]+(int)data[n]+(int)data[n+1])/3)); } for (n=0; n+3 < 921600; n=n+3){ bluematrix[n] = (byte)(Math.round(((int)data[n]+(int)data[n+1]+(int)data[n+2])/3)); } for (n=2; n+3 < 921600; n=n+3){ mix[n] = redmatrix[n]; } for (n=1; n+3 < 921600; n=n+3){ mix[n] = greenmatrix[n]; } for (n=0; n+3 < 921600; n=n+3){ mix[n] = bluematrix[n]; } gray.write(header); gray.write(mix); gray.close(); } catch (Exception e){ System.out.println(e); I do get a greyscale image, but not the right one. Link to comment https://www.neowin.net/forum/topic/369150-how-to-convert-a-24-bit-rgb-bmp-to-8-bit-grayscale/ Share on other sites More sharing options...
0 yyy Posted September 8, 2005 Share Posted September 8, 2005 There's a free image library called FreeImage which can allow you to do it very easily. Maybe you want to use it - it's free and easy :) Link to comment https://www.neowin.net/forum/topic/369150-how-to-convert-a-24-bit-rgb-bmp-to-8-bit-grayscale/#findComment-586495319 Share on other sites More sharing options...
0 DWk Posted September 9, 2005 Author Share Posted September 9, 2005 Actually I'm not supposed to do this with extra libraries, just using the java.io Link to comment https://www.neowin.net/forum/topic/369150-how-to-convert-a-24-bit-rgb-bmp-to-8-bit-grayscale/#findComment-586497558 Share on other sites More sharing options...
0 DWk Posted September 12, 2005 Author Share Posted September 12, 2005 Bump? Can someone help out? Link to comment https://www.neowin.net/forum/topic/369150-how-to-convert-a-24-bit-rgb-bmp-to-8-bit-grayscale/#findComment-586515762 Share on other sites More sharing options...
Question
DWk
Ok, so I'm trying to convert an 24-bit colored bitmap to an 8-bit (256 colors) grayscale bitmap.
I am able to convert the bitmap to a 24-bit greyscale (sort of, if you understand what I'm talking about), by using this:
I do get a greyscale image, but not the right one.
Link to comment
https://www.neowin.net/forum/topic/369150-how-to-convert-a-24-bit-rgb-bmp-to-8-bit-grayscale/Share on other sites
3 answers to this question
Recommended Posts