ResContext *readResource(FILE *in) { ResContext *c; c = malloc(sizeof(ResContext)); if(c == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); goto error0; } if(fread(&(c->hdr), 1, sizeof(ResHdr), in) < sizeof(ResHdr)) { fprintf(stderr, "Failed to read header.\n"); goto error1; } if(memcmp(c->hdr.magic, resMagic, sizeof(resMagic)) != 0) { fprintf(stderr, "Bad signature.\n"); goto error1; } if(fread(c->palette, 1, sizeof(PaletteEntry) * PALETTESIZE, in) < sizeof(PaletteEntry) * PALETTESIZE) { fprintf(stderr, "Failed to read palette.\n"); goto error1; } c->hdr.compDataSize -= 2; // 2 bytes of junk at the end c->compData = malloc(c->hdr.compDataSize); if(c->compData == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); goto error1; } if(fread(c->compData, 1, c->hdr.compDataSize, in) < c->hdr.compDataSize) { fprintf(stderr, "Failed to read compressed data.\n"); goto error2; } c->imageData = malloc(c->hdr.width * c->hdr.height); if(c->imageData == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); goto error2; } if(readImageData(c) < 0) { fprintf(stderr, "Failed to decompress data.\n"); goto error3; } return(c); error3: free(c->imageData); error2: free(c->compData); error1: free(c); error0: return(NULL); }
Texture* TextureReader::read() { verifySignature(reader); initialisePNGReader(reader); const unsigned char* data = readImageData(); const unsigned int width = png_get_image_width(_png, _pngInfo); const unsigned int height = png_get_image_height(_png, _pngInfo); _data = (GLubyte*)data; Texture* texture = new Texture(_data, width, height, 0); texture->setData(data); delete[] data; png_destroy_read_struct(&_png, &_pngInfo, NULL); return texture; }
/*! ***************************************************************************** * \brief * Read the TIFF file named 'path' into 't'. * ***************************************************************************** */ static int readTiff (Tiff * t, char * path) { assert( t); assert( path); if (readFileIntoMemory( t, path)) goto Error; if (readImageFileHeader( t)) goto Error; if (readImageFileDirectory( t)) goto Error; if (readImageData( t)) goto Error; return 0; Error: free_pointer( t->fileInMemory); free_pointer( t->img); return 1; }
int main(int argc, char *argv[]) { // Initialize options to default values double* step_size = new double; TrainingOptions training_options = initDefaultTrainingOptions(step_size); BenchmarkOptions benchmark_options = initDefaultBenchmarkOptions(); std::string path_to_data("data"); // Parse arguments to adjust options parse_command_line_args(argc, argv, &training_options, &benchmark_options, &path_to_data); printf("in main error goal is %f\n",benchmark_options.error_goal); printf("in main run number is %d\n",benchmark_options.num_runs); printf("in main step size is %f \n",*training_options.step_size); // Model variables FeatureType* data_points = new FeatureType[DATA_SET_SIZE]; // Data points LabelType* labels = new LabelType[NUM_SAMPLES]; // Labels FeatureType* parameter_vector = new FeatureType[PARAMETER_SIZE]; // Model parameters //build train file path std::string str_train_points_filepath(path_to_data + std::string("/train-images-idx3-ubyte")); const char* train_points_filepath = str_train_points_filepath.c_str(); std::string str_train_labels_filepath(path_to_data + std::string("/train-labels-idx1-ubyte")); const char* train_labels_filepath = str_train_labels_filepath.c_str(); //build test file path std::string str_test_points_filepath(path_to_data + std::string("/t10k-images-idx3-ubyte")); const char* test_points_filepath = str_test_points_filepath.c_str(); std::string str_test_labels_filepath(path_to_data + std::string("/t10k-labels-idx1-ubyte")); const char* test_labels_filepath = str_test_labels_filepath.c_str(); // Read train data from files and insert into variables if( readImageData (TRAIN_SET_SIZE, train_points_filepath, data_points) != TRAIN_SET_SIZE) return EXIT_FAILURE; if( readImageLables(NUM_TRAINING, train_labels_filepath, labels) != NUM_TRAINING) return EXIT_FAILURE; //Read test data from files and insert behind if(readImageData(TEST_SET_SIZE, test_points_filepath, &data_points[TRAIN_SET_SIZE])!= TEST_SET_SIZE) return EXIT_FAILURE; if(readImageLables(NUM_TESTING, test_labels_filepath, &labels[NUM_TRAINING])!= NUM_TESTING) return EXIT_FAILURE; cout<<"read mnist data success"<<endl; // Initialize data set and options structs DataSet data_set; data_set.data_points = data_points; data_set.labels = labels; data_set.parameter_vector = parameter_vector; data_set.num_data_points = NUM_SAMPLES; data_set.num_features = NUM_FEATURES; // Initial shuffle of the data set to mix spam with ham // shuffleKeyValue(data_points, labels, NUM_SAMPLES, NUM_FEATURES); // runConvergenceRate(data_set, training_options, benchmark_options); // runTrainAndTest(data_set, training_options, benchmark_options); runConvergenceTime(data_set, training_options, benchmark_options); // Free memory and exit delete step_size; delete[] data_points; delete[] labels; delete[] parameter_vector; return 0; }
MMBitmapRef newMMBitmapFromBMP(const char *path, MMBMPReadError *err) { FILE *fp; struct BITMAP_FILE_HEADER fileHeader = {0}; /* Initialize elements to 0. */ struct BITMAP_INFO_HEADER dibHeader = {0}; uint32_t headerSize = 0; uint8_t bytesPerPixel; size_t bytewidth; uint8_t *imageBuf; if ((fp = fopen(path, "rb")) == NULL) { if (err != NULL) *err = kBMPAccessError; return NULL; } /* Initialize error code to generic value. */ if (err != NULL) *err = kBMPGenericError; if (fread(&fileHeader, sizeof(fileHeader), 1, fp) == 0) goto bail; /* Convert from little-endian if it's not already. */ convertBitmapFileHeader(&fileHeader); /* First two bytes should always be 0x4D42. */ if (fileHeader.magic != BMP_MAGIC) { if (err != NULL) *err = kBMPInvalidKeyError; goto bail; } /* Get header size. */ if (fread(&headerSize, sizeof(headerSize), 1, fp) == 0) goto bail; headerSize = swapLittleAndHost32(headerSize); /* Back up before reading header. */ if (fseek(fp, -(long)sizeof(headerSize), SEEK_CUR) < 0) goto bail; if (headerSize == 12) { /* OS/2 v1 header */ struct BITMAP_CORE_HEADER coreHeader = {0}; if (fread(&coreHeader, sizeof(coreHeader), 1, fp) == 0) goto bail; dibHeader.width = coreHeader.width; dibHeader.height = coreHeader.height; dibHeader.colorPlanes = coreHeader.colorPlanes; dibHeader.bitsPerPixel = coreHeader.bitsPerPixel; } else if (headerSize == 40 || headerSize == 108 || headerSize == 124) { /* Windows v3/v4/v5 header */ /* Read only the common part (v3) and skip over the rest. */ if (fread(&dibHeader, sizeof(dibHeader), 1, fp) == 0) goto bail; } else { if (err != NULL) *err = kBMPUnsupportedHeaderError; goto bail; } convertBitmapInfoHeader(&dibHeader); if (dibHeader.colorPlanes != 1) { if (err != NULL) *err = kBMPInvalidColorPanesError; goto bail; } /* Currently only 24-bit and 32-bit are supported. */ if (dibHeader.bitsPerPixel != 24 && dibHeader.bitsPerPixel != 32) { if (err != NULL) *err = kBMPUnsupportedColorDepthError; goto bail; } if (dibHeader.compression != kBMP_RGB) { if (err != NULL) *err = kBMPUnsupportedCompressionError; goto bail; } /* This can happen because we don't fully parse Windows v4/v5 headers. */ if (ftell(fp) != (long)fileHeader.imageOffset) { fseek(fp, fileHeader.imageOffset, SEEK_SET); } /* Get bytes per row, including padding. */ bytesPerPixel = dibHeader.bitsPerPixel / 8; bytewidth = ADD_PADDING(dibHeader.width * bytesPerPixel); imageBuf = readImageData(fp, dibHeader.width, abs(dibHeader.height), bytesPerPixel, bytewidth); fclose(fp); if (imageBuf == NULL) { if (err != NULL) *err = kBMPInvalidPixelDataError; return NULL; } /* A negative height indicates that the image is flipped. * * We store our bitmaps as "flipped" according to the BMP format; i.e., (0, 0) * is the top left, not bottom left. So we only need to flip the bitmap if * the height is NOT negative. */ if (dibHeader.height < 0) { dibHeader.height = -dibHeader.height; } else { flipBitmapData(imageBuf, dibHeader.width, dibHeader.height, bytewidth); } return createMMBitmap(imageBuf, dibHeader.width, dibHeader.height, bytewidth, (uint8_t)dibHeader.bitsPerPixel, bytesPerPixel); bail: fclose(fp); return NULL; }