write c program for encode/decode 4 color indexed pallette image.png with resolution 320x240, colors encoded W WHITE,X BLACK,R RED,G GREEN,to hex binary file and vice versa,
use libstb below replace table two pixel to hex,
color table RGB
black 0,0,0
white 255,255,255
red 255,0,0
green 0,255,00
for example WXWG as hex encoded as 0x13
replacement table
color hex
WW 0
WX 1
WR 2
WG 3
WX 4
XX 5
XR 6
RW 7
RX 8
RR 9
RG A
GW B
GX C
GR D
GG F
Assistant
To create a C program that encodes and decodes a 4-color indexed palette image using the stb_image and stb_image_write libraries, we first need to define the encoding scheme, use the library functions for image handling, and perform the conversion between pixel pairs and hex values based on the provided replacement table.
Here's a basic implementation of such a program:
c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include "stb_image_write.h"
#define WIDTH 320
#define HEIGHT 240
// Replacement table based on the given pattern
uint8_t replacement_table[4][4] = {
{0x00, 0x01, 0x02, 0x03}, // WW, WX, WR, WG
{0x04, 0x05, 0x06, 0x18}, // WX, XX, XR, XG
{0x18, 0x07, 0x09, 0x0A}, // RX, RW, RR, RG
{0x0B, 0x0C, 0x0D, 0x0F} // GW, GX, GR, GG
};
// Color definitions
enum {
WHITE,
BLACK,
RED,
GREEN
};
uint8_t identify_color(uint8_t r, uint8_t g, uint8_t b) {
if (r == 255 && g == 255 && b == 255) return WHITE;
if (r == 0 && g == 0 && b == 0) return BLACK;
if (r == 255 && g == 0 && b == 0) return RED;
if (r == 0 && g == 255 && b == 0) return GREEN;
return -1;
}
void encode_image_to_hex(const uint8_t* data, FILE* outfile) {
for (int i = 0; i < WIDTH * HEIGHT; i += 2) {
uint8_t first = identify_color(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]);
uint8_t second = identify_color(data[(i + 1) * 3], data[(i + 1) * 3 + 1], data[(i + 1) * 3 + 2]);
uint8_t hex_value = replacement_table[first][second];
fwrite(&hex_value, 1, sizeof(hex_value), outfile);
}
}
void decode_hex_to_image(const char* hex_filename, uint8_t* out_data) {
FILE* infile = fopen(hex_filename, "rb");
if (!infile) {
fprintf(stderr, "Could not open the file for reading.\n");
exit(EXIT_FAILURE);
}
uint8_t colors[4][3] = {
{255, 255, 255}, // WHITE
{0, 0, 0}, // BLACK
{255, 0, 0}, // RED
{0, 255, 0} // GREEN
};
uint8_t hex_value;
int idx = 0;
while (fread(&hex_value, 1, 1, infile)) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (replacement_table[i][j] == hex_value) {
out_data[idx * 3] = colors[i][0];
out_data[idx * 3 + 1] = colors[i][1];
out_data[idx * 3 + 2] = colors[i][2];
idx++;
out_data[idx * 3] = colors[j][0];
out_data[idx * 3 + 1] = colors[j][1];
out_data[idx * 3 + 2] = colors[j][2];
idx++;
}
}
}
}
fclose(infile);
}
int main() {
int width, height, channels;
uint8_t* image = stbi_load("input.png", &width, &height, &channels, 0);
if (!image) {
fprintf(stderr, "Could not load image.\n");
return EXIT_FAILURE;
}
if (width != WIDTH || height != HEIGHT) {
fprintf(stderr, "Image dimensions do not match 320x240.\n");
stbi_image_free(image);
return EXIT_FAILURE;
}
FILE* outfile = fopen("output.hex", "wb");
if (!outfile) {
fprintf(stderr, "Could not open the file for writing.\n");
stbi_image_free(image);
return EXIT_FAILURE;
}
encode_image_to_hex(image, outfile);
fclose(outfile);
stbi_image_free(image);
// Decode and generate image from hex (for test and verification purposes)
uint8_t* decoded_image = malloc(WIDTH * HEIGHT * 3);
decode_hex_to_image("output.hex", decoded_image);
stbi_write_png("output_decoded.png", WIDTH, HEIGHT, 3, decoded_image, WIDTH * 3);
free(decoded_image);
return EXIT_SUCCESS;
}
Explanation:
Library Integration: The stb_image libraries are included to load and save images.
Color Identification: The function identify_color() is defined to identify the color from RGB values using predefined patterns.
Encoding:
Image data is read pixel-wise (RGB format).
Two pixels are paired and converted to a hex value using replacement_table.
The output is written as a binary file containing these hex values.
Decoding:
The hex file is read, and corresponding colors are converted back to pixel RGB values using reverse lookup on replacement_table.
The output is saved as a new PNG file for validation.
Compilation and Execution:
Save the program to a .c file. Make sure stb_image.h and stb_image_write.h are available in the same directory.
Compile using:
sh
gcc -o encode_decode image_encode_decode.c -lm
Execute the program:
sh
./encode_decode
The program reads input.png, encodes and saves it as output.hex, and then decodes it back to output_decoded.png. Ensure input.png is a suitable image that matches the given specifications.