Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const size_t cSizeofPixel = 3; //size of struct tPixel
typedef struct{
unsigned char blue;
unsigned char green;
unsigned char red;
} tPixel;
int main()
{
int padding=0;
char temp[4];
int row = 0;
int col = 0;
FILE *bmpIn = fopen("testimg1.bmp", "rb");
if (bmpIn == NULL)
puts("The file could not be opened.");
char signature[3] = { '\0' };
if (fread(signature, sizeof(char), 2, bmpIn) != 2)
puts("Error, signature bits could not be read");
if (strcmp(signature, "BM"))
puts("This is not a BMP file.");
else
puts("This is a BMP file.");
int32_t fileSize;
if (fread(&fileSize, sizeof(fileSize), 1, bmpIn) != 1)
puts("Error reading file size");
int16_t resv1;
if (fread(&resv1, sizeof(resv1), 1, bmpIn) != 1)
puts("Error reading resv1");
int16_t resv2;
if (fread(&resv2, sizeof(resv2), 1, bmpIn) != 1)
puts("Error reading resv2");
int32_t pixelOffset;
if (fread(&pixelOffset, sizeof(pixelOffset), 1, bmpIn) != 1)
puts("Error reading pixelOffset");
int32_t size;
if (fread(&size, sizeof(size), 1, bmpIn) != 1)
puts("Error reading size");
int32_t width;
if (fread(&width, sizeof(width), 1, bmpIn) != 1)
puts("Error reading width");
int32_t height;
if (fread(&height, sizeof(height), 1, bmpIn) != 1)
puts("Error reading height");
int16_t colorPlanes;
if (fread(&colorPlanes, sizeof(colorPlanes), 1, bmpIn) != 1)
puts("Error reading color Planes");
int16_t bitsPerPixel;
if (fread(&bitsPerPixel, sizeof(bitsPerPixel), 1, bmpIn) != 1)
puts("Error reading bits per pixel");
unsigned int Compression;
if (fread(&Compression, sizeof(Compression), 1, bmpIn) != 1)
puts("Error reading Compression");
unsigned int ImageSize;
if (fread(&ImageSize, sizeof(ImageSize), 1, bmpIn) != 1)
puts("Error reading ImageSize");
int xResolution;
if (fread(&xResolution, sizeof(xResolution), 1, bmpIn) != 1)
puts("Error reading xResolution");
int yResolution;
if (fread(&yResolution, sizeof(yResolution), 1, bmpIn) != 1)
puts("Error reading yResolution");
unsigned int Colors;
if (fread(&Colors, sizeof(Colors), 1, bmpIn) != 1)
puts("Error reading Colors");
unsigned int ImportantColors;
if (fread(&ImportantColors, sizeof(ImportantColors), 1, bmpIn) != 1)
puts("Error reading ImportantColors");
//Finish reading both headers #############################################################
printf("\nType: %s", signature );
printf("\nFile Size: %d", fileSize);
printf("\nReserved 1: %hd", resv1);
printf("\nReserved 2: %hd", resv2);
printf("\nOffset: %d", pixelOffset);
printf("\n\nInfo Header Size: %d", size);
printf("\nWidth: %d", width);
printf("\nHeight: %d", height);
printf("\nPlanes: %hd", colorPlanes);
printf("\nBits per pixel: %d", bitsPerPixel);
printf("\nCompression: %d", Compression);
printf("\nImageSize: %d", ImageSize);
printf("\nxResolution: %d", xResolution);
printf("\nyResolution: %d", yResolution);
printf("\nColors: %d", Colors);
printf("\nImportantColors: %d", ImportantColors);
printf("\n");
//Finish printing the information from both headers #######################################
padding = width % 4;
if (padding != 0) {
padding = (4-(3*width)%4)%4;
}
tPixel **pixels = (tPixel **)malloc(height * sizeof(tPixel *));
for (row = 0; row < height; ++row) {
pixels[row] = (tPixel *)malloc(width * sizeof(tPixel));
}
printf("\nReading Pixels Info!!!\n");
for (row = 0; row < height; ++row) {
for (col = 0; col < width; ++col) {
if (fread(&pixels[row][col], cSizeofPixel, 1, bmpIn) != 1) {
printf("\nError reading pixel [%d] ", row);
printf("[%d]\n", col);
}
}
if (padding != 0) {
if (fread(&temp, padding, 1, bmpIn) != 1)
printf("\nError reading padding in row %d \n", row);
}
}
for (row = 0; row < height; ++row)
free(pixels[row]);
free(pixels);
fclose(bmpIn);
FILE *bmpOut = fopen("new.bmp", "wb");
if (bmpOut == NULL) {
printf("\nCannot open file\n");
}
printf("\nWriting Header...\n");
//save to new bmp file
if (fwrite(signature, sizeof(char), 2, bmpOut) != 2)
puts("Error writing signature bits");
if (fwrite(&fileSize, sizeof(fileSize), 1, bmpOut) != 1)
puts("Error writing file size");
if (fwrite(&resv1, sizeof(resv1), 1, bmpOut) != 1)
puts("Error writing resv1");
if (fwrite(&resv2, sizeof(resv2), 1, bmpOut) != 1)
puts("Error writing resv2");
if (fwrite(&pixelOffset, sizeof(pixelOffset), 1, bmpOut) != 1)
puts("Error writing pixelOffset");
if (fwrite(&size, sizeof(size), 1, bmpOut) != 1)
puts("Error writing size");
if (fwrite(&width, sizeof(width), 1, bmpOut) != 1)
puts("Error writing width");
if (fwrite(&height, sizeof(height), 1, bmpOut) != 1)
puts("Error writing height");
if (fwrite(&colorPlanes, sizeof(colorPlanes), 1, bmpOut) != 1)
puts("Error writing color Planes");
if (fwrite(&bitsPerPixel, sizeof(bitsPerPixel), 1, bmpOut) != 1)
puts("Error writing bits per pixel");
if (fwrite(&Compression, sizeof(Compression), 1, bmpOut) != 1)
puts("Error writing Compression");
if (fwrite(&ImageSize, sizeof(ImageSize), 1, bmpOut) != 1)
puts("Error writing ImageSize");
if (fwrite(&xResolution, sizeof(xResolution), 1, bmpOut) != 1)
puts("Error writing xResolution");
if (fwrite(&yResolution, sizeof(yResolution), 1, bmpOut) != 1)
puts("Error writing yResolution");
if (fwrite(&Colors, sizeof(Colors), 1, bmpOut) != 1)
puts("Error writing Colors");
if (fwrite(&ImportantColors, sizeof(ImportantColors), 1, bmpOut) != 1)
puts("Error writing ImportantColors");
printf("\nWriting Pixels...\n");
for(row=0; row < height ; row++) {
for(col=0; col < width; col++) {
if (fwrite(&pixels[row][col], cSizeofPixel, 1, bmpOut) != 1) {
printf("\nError writing pixel [%d] ", row);
printf("[%d]\n", col);
}
}
if(padding != 0) {
fwrite(&temp, padding, 1, bmpOut);
}
}
printf("\nDone writing...\n");
fclose(bmpOut);
return(0);
}