/* * MNIST Data file from http://yann.lecun.com/exdb/mnist/ * 2006.10.25 A.Date */ #include #include // exit() #include #define IMAGE_FILE "train-images-idx3-ubyte" #define NUM_IMAGES 60000 #define SIZE 784 /* 28 x 28 */ static int num[10]; static unsigned char image[NUM_IMAGES][SIZE]; /* TRAINING SET IMAGE FILE (train-images-idx3-ubyte): [offset] [type] [value] [description] 0000 32 bit integer 0x00000803(2051) magic number 0004 32 bit integer 60000 number of images 0008 32 bit integer 28 number of rows 0012 32 bit integer 28 number of columns 0016 unsigned byte ?? pixel 0017 unsigned byte ?? pixel ........ ==== unsigned byte ?? pixel Pixels are organized row-wise. Pixel values are 0 to 255. 0 means background (white), 255 means foreground (black). */ /* http://www.kk.iij4u.or.jp/~kondo/wave/swab.html */ void FlipLong(unsigned char * ptr) { register unsigned char val; /* Swap 1st and 4th bytes */ val = *(ptr); *(ptr) = *(ptr+3); *(ptr+3) = val; /* Swap 2nd and 3rd bytes */ ptr += 1; val = *(ptr); *(ptr) = *(ptr+1); *(ptr+1) = val; } /* Read the training images into memory */ void read_images(){ int i, j,k, fd; char image_name[80]; float f; unsigned char *ptr; if ((fd=open(IMAGE_FILE,O_RDONLY))==-1){ printf("couldn't open image file"); exit(0); } read(fd,num, 4*sizeof(int) ); for (i=0; i<4; i++) { ptr = (unsigned char *)(num + i); FlipLong( ptr); printf("%d\n", num[i]); ptr = ptr + sizeof(int); } for (i=0; i