Skip to navigation
Encode ASCII to png black and white and decode back
01.02.26
### Requirements Recap - Each column in the PNG image should represent one ASCII character. - Each character is represented by 8 pixels (1 pixel for each bit). - Black pixels represent a binary `1`, and white pixels represent a binary `0`. - The height of the PNG image should be fixed at 8 pixels, and the width should be equal to the number of characters in the input text. ### Corrected C Program Here's the corrected C program that implements this functionality: ```c #include
#include
#include
#include
void create_binary_png_strip(const char *output_file, const char *text) { // Calculate the number of characters size_t text_length = strlen(text); int width = text_length; // Each character corresponds to one column int height = 8; // Fixed height of 8 pixels // Allocate memory for the image png_bytep *row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height); for (int y = 0; y < height; y++) { row_pointers[y] = (png_byte *)malloc(width * 4); // 4 bytes per pixel (RGBA) } // Fill the image with binary data for (size_t char_index = 0; char_index < text_length; char_index++) { unsigned char byte = text[char_index]; // Get the ASCII value of the character for (int bit_index = 0; bit_index < 8; bit_index++) { // Determine the color based on the binary value int pixel_y = 7 - bit_index; // Invert the bit index to match the height if (byte & (1 << bit_index)) { // Check if the bit is set // Set pixel to black (1) row_pointers[pixel_y][char_index * 4 + 0] = 0; // R (black) row_pointers[pixel_y][char_index * 4 + 1] = 0; // G (black) row_pointers[pixel_y][char_index * 4 + 2] = 0; // B (black) } else { // Set pixel to white (0) row_pointers[pixel_y][char_index * 4 + 0] = 255; // R (white) row_pointers[pixel_y][char_index * 4 + 1] = 255; // G (white) row_pointers[pixel_y][char_index * 4 + 2] = 255; // B (white) } // Set alpha to fully opaque row_pointers[pixel_y][char_index * 4 + 3] = 255; // A (fully opaque) } } // Write the image to a PNG file FILE *fp = fopen(output_file, "wb"); if (!fp) { fprintf(stderr, "Error opening file %s\n", output_file); return; } png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_infop info = png_create_info_struct(png); if (setjmp(png_jmpbuf(png))) { fprintf(stderr, "Error during PNG creation\n"); fclose(fp); return; } png_init_io(png, fp); png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png, info); png_write_image(png, row_pointers); png_write_end(png, NULL); // Cleanup fclose(fp); for (int y = 0; y < height; y++) { free(row_pointers[y]); } free(row_pointers); png_destroy_write_struct(&png, &info); } int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s
\n", argv[0]); return 1; } create_binary_png_strip(argv[1], argv[2]); return 0; } ``` ### Explanation of the Corrected Program 1. **Input Handling**: The program takes the output PNG filename and the text to encode as command-line arguments. 2. **Image Dimensions**: The width of the PNG image is determined by the length of the input text, and the height is fixed at 8 pixels. 3. **Pixel Filling**: - Each ASCII character is processed to determine its binary representation. - Each bit of the character is represented by a pixel in the column: - A bit value of `1` (black) sets the pixel to black. - A bit value of `0` (white) sets the pixel to white. - The `pixel_y` variable is used to ensure that the least significant bit is at the bottom of the column (the 8th pixel) and the most significant bit is at the top (the 1st pixel). 4. **PNG Writing**: The program uses `libpng` to write the generated image to a PNG file. ### Compilation To compile the program, use the following command: ```bash gcc -o binary_strip binary_strip.c -lpng ``` ### Usage Run the program with: ```bash ./binary_strip output.png "Your text here" ``` ### Notes - Ensure that the `libpng` library is properly installed and linked during compilation. - The output PNG will have a height of 8 pixels and a width equal to the number of characters in the input text. - Each column in the PNG image will represent one ASCII character in binary format. # Encode from ASCII to PNG ``` #include
#include
#include
#include
void create_binary_png_strip(const char *output_file, const char *text) { // Calculate the number of characters size_t text_length = strlen(text); int width = text_length; // Each character corresponds to one column int height = 8; // Fixed height of 8 pixels // Allocate memory for the image png_bytep *row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height); for (int y = 0; y < height; y++) { row_pointers[y] = (png_byte *)malloc(width * 4); // 4 bytes per pixel (RGBA) } // Fill the image with binary data for (size_t char_index = 0; char_index < text_length; char_index++) { unsigned char byte = text[char_index]; // Get the ASCII value of the character for (int bit_index = 0; bit_index < 8; bit_index++) { // Determine the color based on the binary value int pixel_y = 7 - bit_index; // Invert the bit index to match the height if (byte & (1 << bit_index)) { // Check if the bit is set // Set pixel to black (1) row_pointers[pixel_y][char_index * 4 + 0] = 0; // R (black) row_pointers[pixel_y][char_index * 4 + 1] = 0; // G (black) row_pointers[pixel_y][char_index * 4 + 2] = 0; // B (black) } else { // Set pixel to white (0) row_pointers[pixel_y][char_index * 4 + 0] = 255; // R (white) row_pointers[pixel_y][char_index * 4 + 1] = 255; // G (white) row_pointers[pixel_y][char_index * 4 + 2] = 255; // B (white) } // Set alpha to fully opaque row_pointers[pixel_y][char_index * 4 + 3] = 255; // A (fully opaque) } } // Write the image to a PNG file FILE *fp = fopen(output_file, "wb"); if (!fp) { fprintf(stderr, "Error opening file %s\n", output_file); return; } png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_infop info = png_create_info_struct(png); if (setjmp(png_jmpbuf(png))) { fprintf(stderr, "Error during PNG creation\n"); fclose(fp); return; } png_init_io(png, fp); png_set_IHDR(png, info, width, height, 8, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png, info); png_write_image(png, row_pointers); png_write_end(png, NULL); // Cleanup fclose(fp); for (int y = 0; y < height; y++) { free(row_pointers[y]); } free(row_pointers); png_destroy_write_struct(&png, &info); } int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s
\n", argv[0]); return 1; } create_binary_png_strip(argv[1], argv[2]); return 0; } ``` To create a C program that decodes the ASCII text from the PNG image generated by the previous program, we will read the PNG file, extract the pixel data, and convert the binary representation back into ASCII characters. ### C Program to Decode ASCII Text from PNG Here's the C program that accomplishes this task: ```c #include
#include
#include
#include
void decode_png_to_text(const char *input_file) { FILE *fp = fopen(input_file, "rb"); if (!fp) { fprintf(stderr, "Error opening file %s\n", input_file); return; } png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_infop info = png_create_info_struct(png); if (setjmp(png_jmpbuf(png))) { fprintf(stderr, "Error during PNG reading\n"); fclose(fp); return; } png_init_io(png, fp); png_read_info(png, info); int width = png_get_image_width(png, info); int height = png_get_image_height(png, info); png_byte bit_depth = png_get_bit_depth(png, info); png_byte color_type = png_get_color_type(png, info); // Ensure the image is in RGBA format if (color_type != PNG_COLOR_TYPE_RGBA) { png_set_expand(png); png_set_palette_to_rgb(png); png_set_tRNS_to_alpha(png); png_set_filler(png, 0xFF, PNG_FILLER_AFTER); color_type = PNG_COLOR_TYPE_RGBA; } png_read_update_info(png, info); png_bytep *row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height); for (int y = 0; y < height; y++) { row_pointers[y] = (png_byte *)malloc(png_get_rowbytes(png, info)); } png_read_image(png, row_pointers); fclose(fp); // Decode the binary data from the PNG char *decoded_text = (char *)malloc(width + 1); // +1 for null terminator for (int x = 0; x < width; x++) { unsigned char byte = 0; for (int y = 0; y < height; y++) { // Check if the pixel is black (1) or white (0) if (row_pointers[y][x * 4] == 0) { // Check the red channel for black byte |= (1 << (7 - y)); // Set the corresponding bit } } decoded_text[x] = byte; // Store the ASCII character } decoded_text[width] = '\0'; // Null-terminate the string // Print the decoded text printf("Decoded text: %s\n", decoded_text); // Cleanup for (int y = 0; y < height; y++) { free(row_pointers[y]); } free(row_pointers); free(decoded_text); png_destroy_read_struct(&png, &info, NULL); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s
\n", argv[0]); return 1; } decode_png_to_text(argv[1]); return 0; } ``` ### Explanation of the Program 1. **PNG Reading**: The program opens the specified PNG file and reads its contents using the `libpng` library. 2. **Pixel Data Extraction**: It extracts the pixel data from the image, ensuring that it is in RGBA format. 3. **Binary Decoding**: - For each column (representing an ASCII character), it reads the pixel values. - It checks the red channel of each pixel to determine if it is black (1) or white (0). - It constructs the corresponding ASCII character by setting the appropriate bits based on the pixel values. 4. **Output**: The decoded ASCII text is printed to the console. ### Compilation To compile the program, use the following command: ```bash gcc -o decode_png decode_png.c -lpng ``` ### Usage Run the program with: ```bash ./decode_png input.png ``` ### Notes - Ensure that the `libpng` library is properly installed and linked during compilation. - The program assumes that the input PNG image is formatted correctly, with each column representing one ASCII character in binary form. - The output will display the decoded ASCII text.
Reply
Anonymous
Information Epoch 1770133142
Allow the user to tailor the environment.
Home
Notebook
Contact us