To accomplish the task of encoding and decoding a 4-color indexed palette PNG image using a custom encoding scheme and the stb_image library, you will need to follow specific steps. This example will guide you through opening a PNG image with stb_image, encoding its color data into a hexadecimal format as per your substitution table, and then decoding it back to an image.
Before coding, ensure you have stb_image.h and stb_image_write.h for reading and writing PNG files, and include them in your project directory.
Here's a C program outline for your task:
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include "stb_image_write.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// Function prototypes
void encode_image(const char *input_file, const char *output_file);
void decode_image(const char *input_file, const char *output_file);
// Main function
int main(int argc, char **argv) {
if (argc != 4) {
printf("Usage: %s <encode/decode> <input_file> <output_file>\n", argv[0]);
return 1;
}
if (strcmp(argv[1], "encode") == 0) {
encode_image(argv[2], argv[3]);
} else if (strcmp(argv[1], "decode") == 0) {
decode_image(argv[2], argv[3]);
} else {
printf("Invalid command: %s. Use 'encode' or 'decode'.\n", argv[1]);
return 1;
}
return 0;
}
// Mapping color to string code
const char* get_color_code(const uint8_t *pixel) {
if (pixel[0] == 255 && pixel[1] == 255 && pixel[2] == 255) return "W";
if (pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 0) return "X";
if (pixel[0] == 255 && pixel[1] == 0 && pixel[2] == 0) return "R";
if (pixel[0] == 0 && pixel[1] == 255 && pixel[2] == 0) return "G";
return NULL;
}
// Encode two color code letters into a hex digit
char encode_pair(const char *pair) {
if (strcmp(pair, "WW") == 0) return '0';
if (strcmp(pair, "WX") == 0) return '1';
if (strcmp(pair, "WR") == 0) return '2';
if (strcmp(pair, "WG") == 0) return '3';
if (strcmp(pair, "XW") == 0) return '4';
if (strcmp(pair, "XX") == 0) return '5';
if (strcmp(pair, "XR") == 0) return '6';
if (strcmp(pair, "RW") == 0) return '7';
if (strcmp(pair, "RX") == 0) return '8';
if (strcmp(pair, "RR") == 0) return '9';
if (strcmp(pair, "RG") == 0) return 'A';
if (strcmp(pair, "GW") == 0) return 'B';
if (strcmp(pair, "GX") == 0) return 'C';
if (strcmp(pair, "GR") == 0) return 'D';
if (strcmp(pair, "GG") == 0) return 'F';
return '?';
}
// Function to encode image
void encode_image(const char *input_file, const char *output_file) {
int width, height, channels;
uint8_t *img = stbi_load(input_file, &width, &height, &channels, 0);
if (img == NULL) {
printf("Error loading image.\n");
return;
}
if (width != 320 || height != 240) {
printf("Input image must be 320x240.\n");
stbi_image_free(img);
return;
}
FILE *output = fopen(output_file, "w");
if (!output) {
printf("Error opening output file.\n");
stbi_image_free(img);
return;
}
for (int i = 0; i < width * height; i += 2) {
const char *code1 = get_color_code(&img[i * channels]);
const char *code2 = get_color_code(&img[(i + 1) * channels]);
if (!code1 || !code2) {
printf("Unexpected color in image.\n");
fclose(output);
stbi_image_free(img);
return;
}
char pair[3] = {code1[0], code2[0], '\0'};
char hex = encode_pair(pair);
fprintf(output, "%c", hex);
}
fclose(output);
stbi_image_free(img);
}
// Decode hex digit into two color letters
const char* decode_pair(char hex) {
switch (hex) {
case '0': return "WW";
case '1': return "WX";
case '2': return "WR";
case '3': return "WG";
case '4': return "XW";
case '5': return "XX";
case '6': return "XR";
case '7': return "RW";
case '8': return "RX";
case '9': return "RR";
case 'A': return "RG";
case 'B': return "GW";
case 'C': return "GX";
case 'D': return "GR";
case 'F': return "GG";
default: return NULL;
}
}
// Mapping color code to RGB values
void get_rgb_from_code(const char code, uint8_t *rgb) {
if (code == 'W') { rgb[0] = 255; rgb[1] = 255; rgb[2] = 255; }
else if (code == 'X') { rgb[0] = 0; rgb[1] = 0; rgb[2] = 0; }
else if (code == 'R') { rgb[0] = 255; rgb[1] = 0; rgb[2] = 0; }
else if (code == 'G') { rgb[0] = 0; rgb[1] = 255; rgb[2] = 0; }
}
// Function to decode image
void decode_image(const char *input_file, const char *output_file) {
FILE *input = fopen(input_file, "r");
if (!input) {
printf("Error opening input file.\n");
return;
}
uint8_t *decoded_image = malloc(320 * 240 * 3); // 3 channels for RGB
if (!decoded_image) {
printf("Error allocating memory.\n");
fclose(input);
return;
}
int index = 0;
char hex;
while (fscanf(input, "%c", &hex) != EOF && index < 320 * 240 * 3) {
const char *pair = decode_pair(hex);
if (!pair) {
printf("Unexpected hex character in input.\n");
free(decoded_image);
fclose(input);
return;
}
get_rgb_from_code(pair[0], &decoded_image[index * 3]);
get_rgb_from_code(pair[1], &decoded_image[(index + 1) * 3]);
index += 2;
}
fclose(input);
if (stbi_write_png(output_file, 320, 240, 3, decoded_image, 320 * 3) == 0) {
printf("Error writing to output PNG.\n");
}
free(decoded_image);
}
Key Programming Steps:
-
Encoding:
- Load the image using
stb_image.
- Ensure the image is 320x240.
- Map each pixel to its corresponding color code.
- Form pairs of codes, encode them using the specified table, and write the hexadecimal results to an output file.
-
Decoding:
- Read from a hex-encoded file.
- Decode each hex character into a pair of color codes.
- Translate these codes back into RGB values.
- Construct an image from these RGB values and save it using
stb_image_write.
Considerations:
- Ensure your input images contain only the defined colors.
- This code assumes the input file is correctly formatted and the output file paths are valid.
- This demonstration doesn't handle errors extensively. You may implement further robustness by checking for additional edge cases.
Make sure you have the stb headers in your include path and compile the program with required flags for your environment. You'll also need to link it with any necessary libraries required by stb_image.