// Modified version for PP01 #include #include int main(int argc, char *argv[]) { if (argc != 4) { printf("Usage: %s \n", argv[0]); return 1; } char *input_file = argv[1]; char *output_file = argv[2]; int width = atoi(argv[3]); int height; // Open the input file FILE *in = fopen(input_file, "rb"); if (!in) { printf("Error: Failed to open input file\n"); return 1; } // Calculate the height based on the file size and width fseek(in, 0L, SEEK_END); long file_size = ftell(in); fseek(in, 0L, SEEK_SET); height = file_size / width; // Open the output file FILE *out = fopen(output_file, "wb"); if (!out) { printf("Error: Failed to open output file\n"); fclose(in); return 1; } // Color iterations unsigned char mask = 2; for (int c = 0; c < 3; c++ ) { // Loop over each pixel in the input file, convert to RGB, and write to output file for (int y = 0; y < height; y++) { for (int x = 0; x < (width/8); x++) { unsigned char outb = 0; for(int z = 0; z < 8; z++ ) { unsigned char pixel[1]; fread(pixel, 1, 1, in); outb <<= 1; if ( (pixel[0] & mask) == mask ) outb++; } fprintf(out, "%c", outb); } } if ( mask == 2 ) { mask = 4; } else { mask = 1; } fseek(in,0,SEEK_SET); } // Close files and exit fclose(in); fclose(out); return 0; }