|
| 1 | +#include "image_io.h" |
| 2 | +#include "bmp.h" |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +// Read BMP file |
| 7 | +int read_bmp(const char *filename, ImageData *img) { |
| 8 | + FILE *inptr = fopen(filename, "rb"); |
| 9 | + if (inptr == NULL) { |
| 10 | + return 1; |
| 11 | + } |
| 12 | + BITMAPFILEHEADER bf; |
| 13 | + if (fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr) != 1) { |
| 14 | + fclose(inptr); |
| 15 | + return 1; |
| 16 | + } |
| 17 | + BITMAPINFOHEADER bi; |
| 18 | + if (fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr) != 1) { |
| 19 | + fclose(inptr); |
| 20 | + return 1; |
| 21 | + } |
| 22 | + if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || bi.biBitCount != 24 || bi.biCompression != 0) { |
| 23 | + fclose(inptr); |
| 24 | + return 1; |
| 25 | + } |
| 26 | + img->height = abs(bi.biHeight); |
| 27 | + img->width = bi.biWidth; |
| 28 | + img->format = IMAGE_FORMAT_BMP; |
| 29 | + |
| 30 | + img->pixels = (RGBTRIPLE **)malloc(img->height * sizeof(RGBTRIPLE *)); |
| 31 | + if (img->pixels == NULL) { |
| 32 | + fclose(inptr); |
| 33 | + return 1; |
| 34 | + } |
| 35 | + RGBTRIPLE *pixel_data = (RGBTRIPLE *)calloc(img->height * img->width, sizeof(RGBTRIPLE)); |
| 36 | + if (pixel_data == NULL) { |
| 37 | + free(img->pixels); |
| 38 | + fclose(inptr); |
| 39 | + return 1; |
| 40 | + } |
| 41 | + for (int i = 0; i < img->height; i++) { |
| 42 | + img->pixels[i] = pixel_data + i * img->width; |
| 43 | + } |
| 44 | + int padding = (4 - (img->width * sizeof(RGBTRIPLE)) % 4) % 4; |
| 45 | + |
| 46 | + // Read pixels |
| 47 | + for (int i = 0; i < img->height; i++) { |
| 48 | + if (fread(img->pixels[i], sizeof(RGBTRIPLE), img->width, inptr) != (size_t)img->width) { |
| 49 | + free_image(img); |
| 50 | + fclose(inptr); |
| 51 | + return 1; |
| 52 | + } |
| 53 | + fseek(inptr, padding, SEEK_CUR); |
| 54 | + } |
| 55 | + |
| 56 | + fclose(inptr); |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +// Write BMP file |
| 61 | +int write_bmp(const char *filename, ImageData *img) { |
| 62 | + FILE *outptr = fopen(filename, "wb"); |
| 63 | + if (outptr == NULL) { |
| 64 | + return 1; |
| 65 | + } |
| 66 | + int padding = (4 - (img->width * sizeof(RGBTRIPLE)) % 4) % 4; |
| 67 | + int row_size = img->width * sizeof(RGBTRIPLE) + padding; |
| 68 | + int image_size = row_size * img->height; |
| 69 | + |
| 70 | + BITMAPFILEHEADER bf; |
| 71 | + bf.bfType = 0x4d42; |
| 72 | + bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + image_size; |
| 73 | + bf.bfReserved1 = 0; |
| 74 | + bf.bfReserved2 = 0; |
| 75 | + bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); |
| 76 | + |
| 77 | + BITMAPINFOHEADER bi; |
| 78 | + bi.biSize = sizeof(BITMAPINFOHEADER); |
| 79 | + bi.biWidth = img->width; |
| 80 | + bi.biHeight = img->height; |
| 81 | + bi.biPlanes = 1; |
| 82 | + bi.biBitCount = 24; |
| 83 | + bi.biCompression = 0; |
| 84 | + bi.biSizeImage = image_size; |
| 85 | + bi.biXPelsPerMeter = 0; |
| 86 | + bi.biYPelsPerMeter = 0; |
| 87 | + bi.biClrUsed = 0; |
| 88 | + bi.biClrImportant = 0; |
| 89 | + |
| 90 | + if (fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr) != 1 || fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr) != 1) { |
| 91 | + fclose(outptr); |
| 92 | + return 1; |
| 93 | + } |
| 94 | + |
| 95 | + for (int i = 0; i < img->height; i++) { |
| 96 | + if (fwrite(img->pixels[i], sizeof(RGBTRIPLE), img->width, outptr) != (size_t)img->width) { |
| 97 | + fclose(outptr); |
| 98 | + return 1; |
| 99 | + } |
| 100 | + for (int k = 0; k < padding; k++) { |
| 101 | + fputc(0x00, outptr); |
| 102 | + } |
| 103 | + } |
| 104 | + fclose(outptr); |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
0 commit comments