-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.c
128 lines (105 loc) · 3.46 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
128
// Resizes a BMP file by the factor inputted on command line
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <cs50.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 filenames
char *infile = argv[2];
char *outfile = argv[3];
int n = atoi(argv[1]);
// 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;
}
// Variables for new width and height
int inWidth = bi.biWidth;
int inHeight = bi.biHeight;
int outWidth = inWidth * n;
int outHeight = inHeight * n;
// Determine padding for scanlines and header files
int inPadding = (4 - (inWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int outPadding = (4 - (outWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// Change headers information
bi.biHeight = outHeight;
bi.biWidth = outWidth;
bi.biSizeImage = ((sizeof(RGBTRIPLE) * outWidth) + outPadding) * abs(outHeight);
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);
// Allocate a memory to store one scanline
RGBTRIPLE scanline[outWidth * sizeof(RGBTRIPLE)];
// Iterate over infile's scanlines
for (int i = 0, biHeight = abs(inHeight); i < biHeight; i++)
{
// Iterate over pixels in scanline
for (int j = 0; j < inWidth; j++)
{
// Temporary storage
RGBTRIPLE triple;
// Read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// Create a new scanline in a temporary array
for (int k = 0; k < n; k++)
{
scanline[(j * n) + k] = triple;
}
}
// Skip over padding, if any
fseek(inptr, inPadding, SEEK_CUR);
// Write the current scanline n times
for (int j = 0; j < n; j++)
{
// write a new scanline once
fwrite(scanline, sizeof(RGBTRIPLE), outWidth, outptr);
// Write new padding
for (int k = 0; k < outPadding; k++)
{
fputc(0x00, outptr);
}
}
}
// Close infile
fclose(inptr);
// Close outfile
fclose(outptr);
// Success
return 0;
}