int main (int argc, char ** argv) { int ret = EXIT_SUCCESS; // unless otherwise noted const char * in_filename = argv[1]; const char * out_filename = argv[2]; // Read the file Image * im = Image_load(in_filename); if(im == NULL) { fprintf(stderr, "Error: failed to read '%s'\n", in_filename); return EXIT_FAILURE; } // Invert pixel intensity int n_pixels = im->width * im->height; int ind; for(ind = 0; ind < n_pixels; ++ind) im->data[ind] = 255 - im->data[ind]; // Write out a new file if(!Image_save(out_filename, im)) { fprintf(stderr, "Error attempting to write '%s'\n", out_filename); ret = EXIT_FAILURE; } Image_free(im); // a memory leak until you write this function return ret; }
void compress_and_decompress(FILE *fp,bool lossy){ Image_T img; img = Image_new(fp); Image_compress(img,lossy); Image_decompress(img,lossy); Image_write(img, stdout); Image_free(&img); }
Image* Image_load(char* filename) { int rows = 0; int cols = 0; FILE* f = fopen(filename, "r"); if(f == NULL) { printf("Error while opening file %s... aborting!\n", filename); exit(-1); } char buffer[1024]; if(fscanf(f, "%s", buffer) == 1) { rows = atoi(buffer); } else { printf("Error while reading file %s, cannot read number of rows of the image... aborting!\n", filename); exit(-1); } if(fscanf(f, "%s", buffer) == 1) { cols = atoi(buffer); } else { printf("Error while reading file %s, cannot read number of cols of the image... aborting!\n", filename); exit(-1); } if(rows == 0 || cols == 0) { printf("Error while reading file %s, either rows or columns number of the image are zero... aborting!\n", filename); exit(-1); } int i = 0; Image* image = Image_alloc(rows, cols); while(i < rows * cols && fscanf(f, "%s", buffer) == 1) { image->pixels[i] = atoi(buffer); i++; } if(i != rows * cols) { printf("Error while reading file %s, cannot read all image pixels... aborting!\n", filename); Image_free(image); exit(-1); } return image; }
Image * Image_load(const char * filename) { FILE * fp = fopen(filename, "rb"); size_t read; ImageHeader header; Image * tmp_im = NULL, * im = NULL; size_t n_bytes = 0; int err = FALSE; //(1) Make sure you can open the file if(!fp) { err = TRUE; } //(2) Read the header: if(!err) { read = fread(&header, sizeof(ImageHeader), 1, fp); //(2.a) Make sure that you can read all the bytes of the header if(!err){ if(read != 1) { err = TRUE; } } //(2.b) Make sure that the magic_number in the header is correct if(!err){ if(header.magic_number != ECE264_IMAGE_MAGIC_NUMBER) { err = TRUE; } } //(2.c) Make sure that neither the width nor height is zero if(!err){ if(header.width == 0 || header.height == 0) { err = TRUE; } } //(2.d) Make sure that the comment length is not zero. (Remember, it includes the null-byte.) if(!err){ if(header.comment_len == 0) { err = TRUE; } } } //(3) Allocate space for the image, comment, and pixels. Remember,malloc will return NULL if it cannot do its job. if(!err){ tmp_im = malloc(sizeof(Image)); if(tmp_im == NULL) { err = TRUE; } } if(!err){ n_bytes = sizeof(char) * header.comment_len; tmp_im->comment = malloc(n_bytes); if(tmp_im->comment == NULL) { err = TRUE; } } if(!err){ n_bytes = sizeof(uint8_t) * header.width * header.height; tmp_im->data = malloc(n_bytes); if(tmp_im->data == NULL) { err = TRUE; } } //(4) Read the comment if(!err){ char * buffer = malloc(header.comment_len * sizeof(char)); read = fread(buffer, sizeof(char), header.comment_len,fp); //(4.a) Make sure you read the entire comment if(read != header.comment_len) { err = TRUE; } //(4.b) Make sure the comment ends in a null-byte. if(!err){ if(buffer[header.comment_len - 1] != '\0') { err = TRUE; } } free(buffer); } //(5) Read the pixels if(!err){ read = fread(tmp_im->data, sizeof(uint8_t), n_bytes, fp); //(5.a) Make sure you read *all* width*height pixels if(read != n_bytes) { err = TRUE; } } //(5.b) Make sure you've reached the end of the file. (i.e., there are no trailing bytes of data past the end of the pixel data.) if(!err){ uint8_t byte; read = fread(&byte, sizeof(uint8_t), 1, fp); if(read != 0) { err = TRUE; } } if(!err){ tmp_im->width = header.width; tmp_im->height = header.height; im = tmp_im; tmp_im = NULL; } Image_free(tmp_im); if(fp) { fclose(fp); } return im; }
int main(int argc, char *argv[]) { FILE *ifile, *ofile, *ofile2; const char *ofname, *ofname2; Image *img; unsigned char *edge_pix; PList *edge_pts; float threshold, radius; #ifdef MTRACE setenv("MALLOC_TRACE", "mtrace.out", 0); mtrace(); #endif // check args if (argc < 2) { fprintf(stderr, "Usage: %s IN.bmp [BLUR_RADIUS [THRESHOLD [OUT.bmp [POINTS.txt]]]]\n", argv[0]); return 1; } // open input image ifile = fopen(argv[1], "r"); if (!ifile) { fprintf(stderr, "Error opening '%s'.\n", argv[1]); return 1; } // read input image img = Image_createFromBMP(ifile); fclose(ifile); if (!img) { LOG("Image_createFromBMP failed"); return -1; } // read blur radius radius = (argc >= 3) ? atof(argv[2]) : -1.f; if (!isfinite(radius) || radius < 0.f) { radius = 5.f; fprintf(stderr, "Using default blur radius: %g\n", radius); } // read threshold threshold = (argc >= 4) ? atof(argv[3]) : -1.f; if (!isfinite(threshold) || threshold < 0.f) { threshold = 10.f; fprintf(stderr, "Using default threshold: %g\n", threshold); } // open output image ofname = (argc >= 5) ? argv[4] : "out.bmp"; ofile = fopen(ofname, "wb"); if (!ofile) fprintf(stderr, "Error opening '%s'.\n", ofname); // open output points file ofname2 = (argc >= 6) ? argv[5] : "pts.txt"; ofile2 = fopen(ofname2, "w"); if (!ofile2) fprintf(stderr, "Error opening '%s'.\n", ofname2); // create output structures edge_pix = (unsigned char *) malloc(img->w * img->h); if (!edge_pix) { LOG("alloc edge_pix failed"); return -1; } edge_pts = PList_create(); if (!edge_pts) LOG("alloc edge_pts failed"); // find edge pixels timer_start(); struct timeval tmp = start_time; // hack so other calls don't affect main timer detect_edges(img, radius, threshold, edge_pix, edge_pts); start_time = tmp; elapsed = 0.0; timer_end("detect_edges"); // write output image (edge pixels) if (ofile && edge_pix) { grayscale_writeBMP(edge_pix, img->w, img->h, ofile); fclose(ofile); } // write list of edge points if (ofile2 && edge_pts) { int i, n = edge_pts->len; for (i = 0; i < n; i++) { P p = edge_pts->pts[i]; fprintf(ofile2, "%04d,%04d\n", p.y, p.x); } fclose(ofile2); } // cleanup Image_free(img); free(edge_pix); PList_free(edge_pts); #ifdef MTRACE muntrace(); #endif return 0; }
static void free_image(void *image, void *user) { Deallocator *deallocator = user; Image_free(image, *deallocator); }
void IoImage_free(IoImage *self) { Image_free(DATA(self)->image); io_free(IoObject_dataPointer(self)); }
Image * cleanUp(Image * im, FILE * fptr) { if (fptr != NULL) fclose(fptr); Image_free(im); return NULL; }
Image * Image_load(const char * filename) { FILE * fp = NULL; ImageHeader header; size_t read; Image * tmp_im = NULL, * im = NULL; int err = FALSE; if(!err){ fp = fopen(filename, "rb"); if(!fp){ fprintf(stderr, "Failed to open file '%s'\n",filename); err = TRUE; } } if(!err){ read = fread(&header, sizeof(ImageHeader), 1, fp); if(read != 1){ fprintf(stderr, "Failed to read header from '%s'\n", filename); err = TRUE; } } if(!err){ if(!BMP_checkValid(&header)) { fprintf(stderr, "Invalid header in '%s'\n", filename); err = TRUE; } } if(!err){ tmp_im = malloc(sizeof(Image)); tmp_im->comment = NULL; tmp_im->data = NULL; if(tmp_im == NULL){ fprintf(stderr, "Failed to allocate im structure\n"); err = TRUE; } } if(!err){ tmp_im->width = header.width; tmp_im->height = header.height; tmp_im->comment = malloc(sizeof(char*) *header.comment_len); if(tmp_im->comment == NULL){ fprintf(stderr, "Fail to allocate comment\n"); err = TRUE; } } if(!err){ read = fread(tmp_im->comment, sizeof(char), header.comment_len, fp); if(read != header.comment_len || tmp_im->comment[header.comment_len-1] != '\0'){ fprintf(stderr, "Comment does not end with null byte\n"); err = TRUE; } } if(!err){ tmp_im-> data = malloc(sizeof(uint8_t) * header.width * header.height); if(tmp_im-> data == NULL){ fprintf(stderr, "Fail to allocate memory to data\n"); err = TRUE; } } if(!err) { read = fread(tmp_im-> data, sizeof(uint8_t), header.width * header.height,fp); if(read != header.width * header.height){ fprintf(stderr, "could not read all image data\n"); err = TRUE; } } if(!err){ if(fgetc(fp) != EOF){ fprintf(stderr, "Have not reach the end of file\n"); err = TRUE; } } if(!err){ im = tmp_im; tmp_im = NULL; } Image_free(tmp_im); if(fp != NULL) fclose(fp); if(err) return NULL; else return im; }
/** * Loads an ECE264 image file, returning an Image structure. * Will return NULL if there is any error. * * Hint: Please see the README for extensive hints */ Image * Image_load(const char * filename) //为什么是直接 Image? { FILE * fp = NULL; size_t read; // size_t is used to count the number ImageHeader header; Image * tmp_im = NULL, * im = NULL; int err = FALSE; if(!err) { // Open filename fp = fopen(filename, "rb"); if(!fp) { fprintf(stderr, "Failed to open file '%s'\n", filename); err = TRUE; } } if(!err) { // Read the header read = fread(&header, sizeof(ImageHeader), 1, fp); if(read != 1) { fprintf(stderr, "Failed to read header from '%s'\n", filename); err = TRUE; } } if(!err){ if(!checkValid(&header)) { fprintf(stderr, "Invalid header in '%s'\n", filename); err = TRUE; } } if(!err) { // Allocate Image struct tmp_im = malloc(sizeof(Image)); tmp_im->comment = NULL; tmp_im->data = NULL; if(tmp_im == NULL) { fprintf(stderr, "Failed to allocate im structure\n"); err = TRUE; } } if(!err) { // Init the Image struct tmp_im->width = header.width; tmp_im->height = header.height; tmp_im->comment = malloc(sizeof(char*) *header.comment_len); if(tmp_im->comment == NULL) { fprintf(stderr, "Failed to allocate comment\n"); err = TRUE; } } if(!err){ //make sure read the whole comment read = fread(tmp_im->comment, sizeof(char), header.comment_len, fp); if( read != header.comment_len || tmp_im->comment[header.comment_len-1] != '\0'){ //comment does not end with null byte fprintf(stderr, "comment does not end with null byte\n"); err = TRUE; } } if(!err){ //initial the tmp_im data(array) tmp_im-> data = malloc(sizeof(uint8_t) * header.width * header.height); if(tmp_im-> data == NULL){ fprintf(stderr, "Fail to allocate memory to data\n"); err = TRUE; } } if(!err){ // make sure read entire data(array) read = fread(tmp_im->data, sizeof(uint8_t), header.width * header.height, fp); if(read != header.width * header.height){ fprintf(stderr, "Could not read all image data\n"); err = TRUE; } } if(!err) { // We should be at the end of the file now if(fgetc(fp) != EOF){ fprintf(stderr, "Have not reach the end of file\n"); err = TRUE; } } if(!err) { // We're winners... im = tmp_im; // bmp will be returned tmp_im = NULL; // and not cleaned up } Image_free(tmp_im); if(fp){ fclose(fp); } if(err){ return NULL; }else{ return im; } }