/* * read_images: Reads the images from and mnist image db file and puts the * images into and array of vectors. * * Parameters: * - train: boolean value, read from training db if 0, test db if 1 * * Return value: * - array of vectors containing mnist images */ mnist_images_t read_images(uint32_t train) { // Set the filename based the mode (train or test) char *full_path; if (train) { full_path = concat_fname(mnist_path, train_image_fname); } else { full_path = concat_fname(mnist_path, test_image_fname); } // Open the file for reading char *mode = FILE_MODE; FILE *fp = Fopen(full_path, mode); // Read the header of the file uint8_t header[IMAGE_HEADER_SIZE]; Fread(header, sizeof(uint8_t), IMAGE_HEADER_SIZE, fp); // Extract size info from mnist db header uint32_t num_images = read_word(header, NUM_ITEMS_OFFSET); uint32_t rows = read_word(header, ROW_OFFSET); uint32_t cols = read_word(header, COL_OFFSET); uint32_t img_size = rows * cols; // Create array of mnist image vectors vector_t *mnist_data = (vector_t*) Calloc(num_images, sizeof(vector_t)); // Populate vectors with one image each for (uint32_t i = 0; i < num_images; i++) { mnist_data[i] = Vector((size_t) img_size); uint8_t *image_bytes = (uint8_t*) Calloc(img_size, sizeof(uint8_t)); uint32_t actual_size; // Read img_size bytes from the db file into the byte array if ((actual_size = fread(image_bytes, sizeof(uint8_t), img_size, fp)) < img_size) { Free(image_bytes); for (uint32_t i = 0; i < num_images; i++) vector_destroy(mnist_data[i]); return NULL; } // Move 8 bit data to 32 bit integer for more precision uint32_t *vector_data = (uint32_t*) Calloc(img_size, sizeof(uint32_t)); for (uint32_t j = 0; j < img_size; j++) { vector_data[j] = (uint32_t) image_bytes[j]; } mnist_data[i]->data = vector_data; Free(image_bytes); } // Create the mnist_images_t pointer and populate the struct it points to mnist_images_t mnist_imgs = Mnist_images((size_t) num_images); mnist_imgs->imgs = mnist_data; Free(full_path); Fclose(fp); return mnist_imgs; }
mnist_labels_t read_labels(uint32_t train) { char *full_path; if (train) { full_path = concat_fname(mnist_path, train_label_fname); } else { full_path = concat_fname(mnist_path, test_label_fname); } char *mode = FILE_MODE; FILE *fp = Fopen(full_path, mode); uint8_t header[LABEL_HEADER_SIZE]; Fread(header, sizeof(uint8_t), LABEL_HEADER_SIZE, fp); uint32_t num_labels = read_word(header, NUM_ITEMS_OFFSET); vector_t lbls = Vector((size_t) num_labels); uint8_t *label_bytes = (uint8_t*) Calloc(num_labels, sizeof(uint8_t)); uint32_t actual_size; if ((actual_size = fread(label_bytes, sizeof(uint8_t), num_labels, fp)) < num_labels) { Free(label_bytes); vector_destroy(lbls); return NULL; } uint32_t *vector_data = (uint32_t*) Calloc(num_labels, sizeof(uint32_t)); for (uint32_t i = 0; i < num_labels; i++) { vector_data[i] = (uint32_t) label_bytes[i]; } lbls->data = vector_data; mnist_labels_t mnist_lbls = Mnist_labels((size_t) num_labels); mnist_lbls->labels = lbls; Free(full_path); Free(label_bytes); Fclose(fp); return mnist_lbls; }
static int lua_concatfname(lua_State *L) { int i; int narg = lua_gettop(L); lua_cwd(L); for (i=1; i<=narg; i++) { concat_fname(L, luaL_checkstring(L, i)); lua_remove(L, -2); } return 1; }
void file_write (File *file, const char* directory) { char *path = NULL; assert (file); if (!file) return; if (file->name == NULL) { file->name = strdup( TNEF_DEFAULT_FILENAME ); debug_print ("No file name specified, using default %s.\n", TNEF_DEFAULT_FILENAME); } if ( file->path == NULL ) { file->path = munge_fname( file->name ); if (file->path == NULL) { file->path = strdup( TNEF_DEFAULT_FILENAME ); debug_print ("No path name available, using default %s.\n", TNEF_DEFAULT_FILENAME); } } path = concat_fname( directory, file->path ); if (path == NULL) { path = strdup( TNEF_DEFAULT_FILENAME ); debug_print ("No path generated, using default %s.\n", TNEF_DEFAULT_FILENAME); } debug_print ("%sWRITING\t|\t%s\t|\t%s\n", ((LIST_ONLY==0)?"":"NOT "), file->name, path); if (!LIST_ONLY) { FILE *fp = NULL; if (!confirm_action ("extract %s?", file->name)) return; if (!OVERWRITE_FILES) { if (file_exists (path)) { if (!NUMBER_FILES) { fprintf (stderr, "tnef: %s: Could not create file: File exists\n", path); return; } else { char *tmp = find_free_number (path); debug_print ("Renaming %s to %s\n", path, tmp); XFREE (path); path = tmp; } } } fp = fopen (path, "wb"); if (fp == NULL) { perror (path); exit (1); } if (fwrite (file->data, 1, file->len, fp) != file->len) { perror (path); exit (1); } fclose (fp); } if (LIST_ONLY || VERBOSE_ON) { if (LIST_ONLY && VERBOSE_ON) { /* FIXME: print out date and stuff */ const char *date_str = date_to_str(&file->dt); fprintf (stdout, "%11lu\t|\t%s\t|\t%s\t|\t%s", (unsigned long)file->len, date_str+4, /* skip the day of week */ file->name, path); } else { fprintf (stdout, "%s\t|\t%s", file->name, path); } if ( SHOW_MIME ) { fprintf (stdout, "\t|\t%s", file->mime_type ? file->mime_type : "unknown"); fprintf (stdout, "\t|\t%s", file->content_id ? file->content_id : ""); } fprintf (stdout, "\n"); } XFREE(path); }