//Read a binary file
//Bitmap file header contain the information about == type == , == size == , == layout == --->14 bytes
//Bitmap information header contain the information about == dim == , == type of compresion==, ==coulor format== --->40 bytes
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int width, height, image_size, text_size, buff_bit_size, my_text[100];
unsigned char Mat[100000][3];
char TextToBinary[100 * 8];
void Read_BMP_FILE()
{
//Deschiderea fisierului image.bmp si citirea
FILE* image = fopen("image.bmp", "rb");
fseek(image, 18, SEEK_SET); //Mutam cursorul de la inceputul fisierului SEEK_SET peste 18 octeti pozitie citind astfel dim
fread(&width, sizeof(int), 1, image);
fread(&height, sizeof(int), 1, image);
//printf("%i x %i", width, height);
fseek(image, 54, SEEK_SET);
image_size = width * height; //width * height * 3 bytes for pixel
//BGR->RGB
for (int i = 0; i < image_size; ++i) {
Mat[i][2] = getc(image);
Mat[i][1] = getc(image);
Mat[i][0] = getc(image);
}
fclose(image);
}
void Read_Message_Text()
{
FILE* text = fopen("text.txt", "r");
fread(my_text, sizeof(char), 100, text);
text_size = strlen(my_text);
fclose(text);
}
void ConvertTextToBinary(char *str)
{
char binars[9];
int cnt = 0;
char output[9];
while (*str) {
itoa(*str, output, 2);
for (int i = 0; i < 7; ++i)
TextToBinary[buff_bit_size++] = output[i];
//puts(output);
++str;
}
}
void ComputeEachPixel()
{
for (int i = 0, j = 0; i < buff_bit_size; i += 3, ++j) {
if (TextToBinary[i] == '1') {
if (Mat[j][0] % 2 == 0) Mat[j][0] += 1;
}
else
{
if (Mat[j][0] % 2) Mat[j][0] -= 1;
}
if (TextToBinary[i + 1] == '1') {
if (Mat[j][1] % 2 == 0) Mat[j][1] += 1;
}
else
{
if (Mat[j][1] % 2) Mat[j][1] -= 1;
}
if (TextToBinary[i + 2] == '1') {
if (Mat[j][2] % 2 == 0) Mat[j][2] += 1;
}
else
{
if (Mat[j][2] % 2) Mat[j][2] -= 1;
}
}
}
int main()
{
//Codul maxim a unui caracter este 'Z' = 90 <= 2^7 ===> maxim 7 biti pentru fiecare caracter
Read_BMP_FILE();
Read_Message_Text();
ConvertTextToBinary(my_text);
ComputeEachPixel();
getch();
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.