forked from mareksuscak/cs50
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.c
127 lines (103 loc) Β· 3.5 KB
/
resize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copies a BMP file
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "bmp.h"
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 4)
{
fprintf(stderr, "Usage: resize n infile outfile\n");
return 1;
}
// remember size multiplier
float n = atof(argv[1]);
// remember filenames
char *infile = argv[2];
char *outfile = argv[3];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
// open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(inptr);
fprintf(stderr, "Could not create %s.\n", outfile);
return 3;
}
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
bi.biBitCount != 24 || bi.biCompression != 0)
{
fclose(outptr);
fclose(inptr);
fprintf(stderr, "Unsupported file format.\n");
return 4;
}
// determine new dimensions
int oldWidth = bi.biWidth;
int oldHeight = bi.biHeight;
int newWidth = floor(oldWidth * n);
int newHeight = floor(oldHeight * n);
// determine padding for scanlines
int inPadding = (4 - (oldWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int outPadding = (4 - (newWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// reconfigure headers
bi.biHeight = newHeight;
bi.biWidth = newWidth;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * newWidth) + outPadding) * abs(newHeight);
bf.bfSize = bi.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// determine ratio
double widthRatio = (double) oldWidth / (double) newWidth;
double heightRatio = (double) oldHeight / (double) newHeight;
// allocate a memory to store one scanline
RGBTRIPLE scanline[oldWidth * sizeof(RGBTRIPLE)];
int cachedScanline = -1;
// for all rows in the new image
for (int i = 0, biHeight = abs(newHeight); i < biHeight; i++)
{
// compute the Y coordinate of the corresponding row in the old image
int row = i * heightRatio;
// read the corresponding scanline from the old image unless it's cached
if (cachedScanline != row)
{
fseek(inptr, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (((sizeof(RGBTRIPLE) * oldWidth) + inPadding) * row), SEEK_SET);
fread(scanline, sizeof(RGBTRIPLE), oldWidth, inptr);
cachedScanline = row;
}
// for all columns in the new image
for (int j = 0; j < newWidth; j++)
{
// compute the X coordinate of the corresponding column in the old image
int column = j * widthRatio;
fwrite(&scanline[column], sizeof(RGBTRIPLE), 1, outptr);
}
// write new padding
for (int j = 0; j < outPadding; j++)
{
fputc(0x00, outptr);
}
}
// close infile
fclose(inptr);
// close outfile
fclose(outptr);
// success
return 0;
}