Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
resize.c
// BMP-related data types based on Microsoft's own #include <stdint.h> // aliases for C/C++ primitive data types // https://msdn.microsoft.com/en-us/library/cc230309.aspx typedef uint8_t BYTE; typedef uint32_t DWORD; typedef int32_t LONG; typedef uint16_t WORD; // information about the type, size, and layout of a file // https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx typedef struct { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; } __attribute__((__packed__)) BITMAPFILEHEADER; // information about the dimensions and color format // https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx typedef struct { DWORD biSize; LONG biWidth; LONG biHeight; WORD biPlanes; WORD biBitCount; DWORD biCompression; DWORD biSizeImage; LONG biXPelsPerMeter; LONG biYPelsPerMeter; DWORD biClrUsed; DWORD biClrImportant; } __attribute__((__packed__)) BITMAPINFOHEADER; // relative intensities of red, green, and blue // https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx typedef struct { BYTE rgbtBlue; BYTE rgbtGreen; BYTE rgbtRed; } __attribute__((__packed__)) RGBTRIPLE; /** * copy.c * * Computer Science 50 * Problem Set 4 * * Copies a BMP piece by piece, just because. */ #include <stdio.h> #include <stdlib.h> #include "bmp.h" int main(int argc, char* argv[]) { // ensure proper usage if (argc != 4) { printf("Usage: ./resize n infile outfile\n"); return 1; } // remember filenames int n = atoi(argv[1]); char* infile = argv[2]; char* outfile = argv[3]; //check the value of n if(n <= 0 || n > 100) { printf("Try to enter between 0 and 100."); return 2; } // open input file FILE* inptr = fopen(infile, "r"); if (inptr == NULL) { printf("Could not open %s.\n", infile); return 3; } // open output file FILE* outptr = fopen(outfile, "w"); if (outptr == NULL) { fclose(inptr); fprintf(stderr, "Could not create %s.\n", outfile); return 4; } // 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 5; } // determine padding for scanlines int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // update header info for the out file BITMAPFILEHEADER bf_resize = bf; BITMAPINFOHEADER bi_resize = bi; bi_resize.biWidth = bi.biWidth * n; bi_resize.biHeight = bi.biHeight * n; // determine new padding after update header info int padding_resize = (4 - (bi_resize.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; // resize the image size bi_resize.biSizeImage = (bi_resize.biWidth * sizeof(RGBTRIPLE) + padding_resize) * abs(bi_resize.biHeight); bf_resize.bfSize = bi_resize.biSizeImage + 54; // write outfile's BITMAPFILEHEADER fwrite(&bf_resize, sizeof(BITMAPFILEHEADER), 1, outptr); // write outfile's BITMAPINFOHEADER fwrite(&bi_resize, sizeof(BITMAPINFOHEADER), 1, outptr); // iterate over infile's scanlines for (int i = 0, biHeight = abs(bi_resize.biHeight); i < biHeight; i++) { // resize vertical for(int repeatVertical = 0; repeatVertical < n; repeatVertical++) { // set the inptr position if(repeatVertical != 0) fseek(inptr, -(bi.biWidth * 3 + padding), SEEK_CUR); // read each scanline, pixel by pixel for (int j = 0; j < bi_resize.biWidth; j++) { // temporary storage RGBTRIPLE triple; // read RGB triple from infile fread(&triple, sizeof(RGBTRIPLE), 1, inptr); // resize horizontal for(int repeatHorizontal = 0; repeatHorizontal < n; repeatHorizontal++) { // write RGB triple to outfile fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); } } } // skip over padding, if any fseek(inptr, padding, SEEK_CUR); // then add it back (to demonstrate how) for (int k = 0; k < padding_resize; k++) { fputc(0x00, outptr); } } // close infile fclose(inptr); // close outfile fclose(outptr); // that's all folks return 0; }
run
|
edit
|
history
|
help
0
marquee text in C
Herout (67) - 15
Printf ejemplos varios
prog0
Code
13 12 2014 20:25
Herout (67) - 2
Paso parámetros a punteros
CV09-2.1
Herout (67) - 4