I am trying to use the MNIST data set of handwritten digits for a project, and I am trying to read each picture in as 28 by 28 2D array of ints from 1-255, corresponding to the greyscale color of each pixel. I downloaded the training file (train-images-idx3-ubyte.gz) off of their website (http://yann.lecun.com/exdb/mnist/), and am having trouble actually processing this file. It describes the file format as 16 bytes of header info, followed by unsigned bites, each of which holding one pixel, organized row-wise. See the website for more details.
In my code I try and read the file into a byte array (which, when I run it, is the same size as the file specified: 9,912,422 bytes). I then start at the seventeenth byte so as to skip the header, and compensate for the fact that java tries to make the byte a signed integer adding 128 to the absolut value of all the negative negative numbers (their first bit was a one). To see if this was working I tried to print it using a drawing panel class which I know works, and I only see static, there is no pattern at all to the pixels. What am I doing wrong with handling the file? Thanks!
File file=new File("train-images-idx3-ubyte.gz");
long size = file.length();
System.out.println(size);
byte[] contents=new byte[(int)size];
FileInputStream in = new FileInputStream(file);
in.read(contents);
in.close();
DrawingPanel panel = new DrawingPanel(400, 400);
Graphics g = panel.getGraphics();
int xloc = 0;
int yloc = 0;
for(int jj = 0; jj < 28; jj++)
{
for(int ii = 0; ii < 28; ii++)
{
int x = (int) contents[17+jj*28+ii];
if(x < 0)
{
x = (x * (0-1)) + 128;
}
System.out.print(x + " ");
int color = (255 - x);
g.setColor(new Color(x,x,x));
g.fillRect(xloc,yloc,10,10);
xloc += 10;
}
System.out.println();
yloc+= 10;
xloc = 0;
}