Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
3.1 kB
2
Indexable
Never
int main(int argc, char *argv[]) {
    if (argc != 4) {
        fprintf(stderr, "Usage: %s <input_png> <output_png> <message>\n", argv[0]);
        return 1;
    }

    const char *input_filename = argv[1];
    const char *output_filename = argv[2];
    const char *message = argv[3];

    png_bytep *image = NULL;
    int width, height;

    // Read the input PNG image
    read_png_file(input_filename, &image, &width, &height);

    // Embed the message in the image
    embed_message(image, width, height, message);

    // Write the output PNG image
    write_png_file(output_filename, image, width, height);

    // Free memory
    for (int y = 0; y < height; y++) {
        free(image[y]);
    }
    free(image);

    printf("Message embedded and saved to %s\n", output_filename);
    return 0;
}

// Function to extract a message from an image using DCT
void extract_message(png_bytep *image, int width, int height, char *output_message, int max_length) {
    int msg_index = 0;
    int bit_value;
    double block[BLOCK_SIZE][BLOCK_SIZE];
    
    for (int y = 0; y < height && msg_index < max_length * 8; y += BLOCK_SIZE) {
        for (int x = 0; x < width && msg_index < max_length * 8; x += BLOCK_SIZE) {
            // Load an 8x8 block
            for (int i = 0; i < BLOCK_SIZE; i++) {
                for (int j = 0; j < BLOCK_SIZE; j++) {
                    if (y + i < height && x + j < width) {
                        block[i][j] = (double)image[y + i][4 * (x + j)];
                    } else {
                        block[i][j] = 0.0;
                    }
                }
            }

            // Perform DCT on the block
            dct(block);

            // Extract the bit from DCT coefficients
            bit_value = block[1][1] > 0 ? 1 : 0;
            output_message[msg_index / 8] |= (bit_value << (7 - (msg_index % 8)));
            msg_index++;
        }
    }

    // Null-terminate the extracted message
    output_message[msg_index / 8] = '\0';
}

// Function to read the embedded message
void read_embedded_message(const char *input_filename, int message_length) {
    png_bytep *image = NULL;
    int width, height;
    char *extracted_message = malloc((message_length + 1) * sizeof(char));
    memset(extracted_message, 0, (message_length + 1) * sizeof(char));

    // Read the input PNG image
    read_png_file(input_filename, &image, &width, &height);

    // Extract the message from the image
    extract_message(image, width, height, extracted_message, message_length);

    // Free memory
    for (int y = 0; y < height; y++) {
        free(image[y]);
    }
    free(image);

    printf("Extracted Message: %s\n", extracted_message);
    free(extracted_message);
}

int main_extract(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <input_png> <message_length>\n", argv[0]);
        return 1;
    }

    const char *input_filename = argv[1];
    int message_length = atoi(argv[2]);

    // Extract the message from the image
    read_embedded_message(input_filename, message_length);

    return 0;
}
Leave a Comment