1 Attachment(s)
24 bit bmp mirror image in c
I am trying to make a mirror image for any 24 bit bmp image in c, but when I run the program it always gets distorted. This is suppose to be the nested for loop that replaces all right and left pixels with each other. Anyone see any faults with this logic? The program makes a new image.
insert
Code:
for (j = 0; j < image.height; j++)
{
for (i = 0; i < image.width+(image.padding); i++)
{
int mirrorrow = image.width - i -1;
if (i == image.width-1 && image.padding!=0)
{
for (t = 0;t < image.padding;t++)
new.pixels[(image.width * 3 + t) + (j*image.width * 3 + image.padding)] = image.pixels[(image.width * 3 + t) + (j*image.width * 3 + image.padding)];
}
else
{
int xPos = j * image.width * 3 + 3 * i;
int xEndPos = j * image.width * 3 + 3*mirrorrow;
unsigned char temp1 = image.pixels[xEndPos - 2];
unsigned char temp2 = image.pixels[xEndPos - 1];
unsigned char temp3 = image.pixels[xEndPos];
new.pixels[xPos] = temp1;
new.pixels[xPos + 1] = temp2;
new.pixels[xPos + 2] = temp3;
}
}
This is what I get when the program finishes, original image vs the new one that is suppose to be the mirrored original one.
Attachment 15789
Here is my original unedited source code for the program, it should only work for 24 bit bmp images.
insert
Code:
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include<string.h>
typedef struct {
int width;
int height;
int pixeloffset;
int padding;
unsigned char header[54];
unsigned char* pixels;
int size;
}BMP;
BMP mirrorimage(BMP image)
{
int um = image.height * image.width;
int i,j,t;
BMP novi;
novi.width = image.width;
novi.height = image.height;
novi.size = image.size;
novi.pixeloffset = image.pixeloffset;
novi.padding = image.padding;
for (i = 0; i < 54; i++)
{
novi.header[i] = image.header[i];
}
novi.pixels = (unsigned char*)malloc((image.padding*image.height)+image.width * image.height * 3 * sizeof(unsigned char));
for (j = 0; j < image.height; j++)
{
for (i = 0; i < image.width+(image.padding); i++)
{
int zrcalnakolona = image.width - i -1;
//zrcalnakolona = i;
if (i == image.width-1 && image.padding!=0)
{
for (t = 0;t < image.padding;t++)
novi.pixels[(image.width * 3 + t) + (j*image.width * 3 + image.padding)] = image.pixels[(image.width * 3 + t) + (j*image.width * 3 + image.padding)];
}
else
{
int xPos = j * image.width * 3 + 3 * i;
int xEndPos = j * image.width * 3 + 3*zrcalnakolona+image.width*2;
unsigned char temp1 = image.pixels[xEndPos - 2];
unsigned char temp2 = image.pixels[xEndPos - 1];
unsigned char temp3 = image.pixels[xEndPos];
novi.pixels[xPos] = temp1;
novi.pixels[xPos + 1] = temp2;
novi.pixels[xPos + 2] = temp3;
}
}
}
return novi;
}
BMP citanjeBMP()
{
BMP image;
int i;
FILE* slika = fopen("C:\\Temp\\test.bmp", "rb");
fread(image.header, sizeof(unsigned char), 54, slika);
image.width = *(int*)& image.header[18];
image.height = *(int*)& image.header[22];
image.pixeloffset = *(int*)& image.header[10];
image.padding = (image.width*3) % 4;
image.size = 3 * image.width * image.height;
image.pixels = (unsigned char*)malloc(image.padding*image.height + image.width * image.height * 3 * sizeof(unsigned char));
fread(image.pixels, sizeof(unsigned char), image.size, slika);
fclose(slika);
for (i = 0; i < image.size + (image.padding*image.height); i += 3)
{
if (i%image.width*3-1 == 0 && image.padding != 0 && i != 0)
{
i = i + image.padding-3;
}
else
{
unsigned char tmp = image.pixels[i];
image.pixels[i] = image.pixels[i + 2];
image.pixels[i + 2] = tmp;
}
}
return image;
}
void upisivanjeBMP(BMP image)
{
FILE* slika = fopen("C:\\Temp\\test.bmp", "rb");
FILE* novi = fopen("C:\\Temp\\testnovi2.bmp", "wb");
char c;
c = fgetc(slika);
while (c != EOF)
{
fputc(c, novi);
c = fgetc(slika);
}
fclose(slika);
fwrite(image.header, sizeof(unsigned char), 54, novi);
int i;
unsigned char tmp;
for (i = 0; i < image.size+(image.padding*image.height); i += 3)
{
if (i%image.width*3-1 == 0 && image.padding!=0 && i!=0)
{
i = i + image.padding-3;
}
else
{
tmp = image.pixels[i];
image.pixels[i] = image.pixels[i + 2];
image.pixels[i + 2] = tmp;
}
}
fwrite(image.pixels, sizeof(unsigned char), image.size+image.padding*image.height, novi);
fclose(novi);
}
int main(void)
{
FILE* slika;
int i = 0;
char check[2];
char slovo;
slika = fopen("C:\\Temp\\test.bmp", "rb");
if (slika == NULL)
{
fprintf(stderr, "\nNemoguce pristupiti podatku\n");
system("pause");
return -1;
}
rewind(slika);
do
{
slovo = ((char)fgetc(slika));
printf(" %c ", slovo);
check[i] = slovo;
i = i + 1;
} while (i < 2);
if (check[0] != 'B' || check[1] != 'M')
{
printf("Podatak nije bmp!");
system("pause");
return -1;
}
else
{
printf("Podatak je bmp!");
}
fclose(slika);
BMP image = citanjeBMP();
printf("\nVelicina slike je %d x %d", image.width, image.height);
system("pause");
BMP imageNovi = mirrorimage(image);
upisivanjeBMP(imageNovi);
return(0);
}