I am trying to copy all data of one .tga file and create new file with exactly same data. Basicly it should copy image. (but i need to use this way because of teacher...)
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
typedef unsigned char byte;
typedef struct {
byte id_length;
byte color_map_type;
byte image_type;
byte color_map[5];
byte x_origin[2];
byte y_origin[2];
byte width[2];
byte height[2];
byte depth;
byte descriptor;
} TGAHeader;
typedef struct {
byte red;
byte green;
byte blue;
} RGBPixel;
typedef struct {
TGAHeader header;
RGBPixel* pixels;
int width;
int height;
} Image;
bool image_load(Image* image, const char* path){
FILE *file = fopen(path, "rb");
memset(image, 0, sizeof(Image));
fread(&image->header, sizeof(image->header), 1, file);
assert(image->header.depth == 24);
assert(image->header.image_type == 2);
memcpy(&image->width, image->header.width, sizeof(image->header.width));
memcpy(&image->height, image->header.height, sizeof(image->header.height));
image->pixels = (RGBPixel*) malloc(sizeof(RGBPixel) * image->width * image->height);
fread(image->pixels, sizeof(RGBPixel) * image->width * image->height, 1, file);
fclose(file);
return true;
}
void image_free(Image* image) {
free(image->pixels);
}
int main(int argc, char* argv[]){
Image image;
if(!image_load(&image, argv[1])){
printf("Could not load image");
return 1;
}
FILE* fp;
fp = fopen(argv[2], "wb");
fwrite(&image, 1, sizeof(image), fp);
fclose(fp);
image_free(&image);
return 0;
}
this image works with slight damage done to the image
but this image after copying is just bunch of random pixels
Slight damage done to image is acceptable but I need the image good enough so I can still say "it is A"...
Notes:
I have converted images to png so I can show them here.
I am using C on Linux.