/* tgarw.c - read and write tga format */ #include #include //Read-only struct ro_image_info { unsigned short rowcnt; unsigned short colcnt; unsigned char * image; }; main(int ac, char *av[]) { struct ro_image_info roinfo; struct init_info * thread_init; if ( ac != 3 ){ printf("usage: %s input output \n", av[0]); exit(1); } //Theory // 1 - load the image to be diffused (Assume 8bit ) // 2 - perform diffusion on the in-memory image // 3 - write the output file //Load the image. //tga file with header (NOTE: only partial validation of the TGA header is performed) unsigned char * image; unsigned char tgaheader[18]; FILE * infile; infile = fopen(av[1], "r"); fread(tgaheader, 18, 1, infile); memcpy(&roinfo.rowcnt, tgaheader+14, sizeof(short)); memcpy(&roinfo.colcnt, tgaheader+12, sizeof(short)); unsigned char pixbits; //Bits per pixel? memcpy(&pixbits, tgaheader+16, 1); if(pixbits != 8 || tgaheader[2] != 3) { printf("Image is not 8bit uncompressed black and white!\n"); exit(1); } int imagesize = roinfo.rowcnt*roinfo.colcnt; //Malloc space for the image image = (unsigned char *) malloc(imagesize); roinfo.image = image; printf("Read in %i bytes(%ix%i)\n", imagesize, roinfo.colcnt, roinfo.rowcnt); //Read it in fread(image, imagesize, 1, infile); fclose(infile); /* Floyd-Steinberg would be here */ //Done - write output; FILE * outfile; outfile = fopen(av[2], "w"); fwrite(tgaheader, 18, 1, outfile); //Header shouldn't change fwrite(image, imagesize, 1, outfile); fclose(outfile); printf("Done!\n"); }