Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include "bmp.h"
- int main(int argc, char* argv[])
- {
- if (argc != 4)
- {
- printf("Usage: ./resize f infile outfile \n");
- return 1;
- }
- // remember filenames
- char *infile = argv[2];
- char *outfile = argv[3];
- float key = atof(argv[1]);
- // open input file
- FILE *inptr = fopen(infile, "r");
- if (inptr == NULL)
- {
- fprintf(stderr, "Could not open %s.\n", infile);
- return 1;
- }
- // open output file
- FILE *outptr = fopen(outfile, "w");
- if (outptr == NULL)
- {
- fclose(inptr);
- fprintf(stderr, "Could not create %s.\n", outfile);
- return 1;
- }
- // 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;
- }
- int hH = abs(bi.biHeight), hW = bi.biWidth;
- bi.biHeight *= round(key);
- bi.biWidth *= round(key);
- int padding = (bi.biWidth * sizeof(RGBTRIPLE)) % 4;
- bf.bfSize = 54 + abs(bi.biHeight)* sizeof(RGBTRIPLE) *(bi.biWidth + padding);
- //printf("size: %i | width: %i height: %i | padding: %i\n", bf.bfSize, bi.biWidth, bi.biHeight, padding);
- // write outfile's BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // write outfile's BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // iterate over infile's scanlines
- for (int i = 0, ptr_pos = 54, size_pad = (hW * sizeof(RGBTRIPLE)) % 4, rkey = round(key), biH = abs(bi.biHeight), igl = 0 ; i < hH; i++)
- {
- for (int j = 0; j < hW; j++)
- {
- RGBTRIPLE triple;
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- for (int z = 0; z < rkey; z++)
- {
- for (int y = 0; y < rkey; y++)
- {
- //printf("out: %ld in: %ld pixel: %i%i%i || ", ftell(outptr), ftell(inptr), (int)triple.rgbtRed,(int)triple.rgbtGreen,(int)triple.rgbtBlue);
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- //printf("\n");
- fseek(outptr,(sizeof(RGBTRIPLE)* bi.biWidth) - (key) * sizeof(RGBTRIPLE), SEEK_CUR);
- }
- fseek(outptr, sizeof(RGBTRIPLE)* (key - bi.biWidth* key) , SEEK_CUR);
- // printf("nline : %f pos: %ld\n", sizeof(RGBTRIPLE)* (key - bi.biWidth* key), ftell(outptr));
- }
- fseek(outptr,sizeof(RGBTRIPLE), SEEK_CUR);
- // then add it back (to demonstrate how)
- for (int k = 0; k < padding; k++)
- {
- RGBTRIPLE triple;
- printf("out: %ld || in: %ld \n", ftell(outptr), ftell(inptr));
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- fseek(outptr, 54 + sizeof(RGBTRIPLE)* ( bi.biWidth* key) * (i + 1) , SEEK_SET);
- }
- // close infile
- fclose(inptr);
- // close outfile
- fclose(outptr);
- // success
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement