added ws202425 courses
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
colorize:
|
||||
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow -o colorize colorize.c helpers.c
|
||||
@@ -0,0 +1,75 @@
|
||||
// BMP-related data types based on Microsoft's own
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Common Data Types
|
||||
*
|
||||
* The data types in this section are essentially aliases for C/C++
|
||||
* primitive data types.
|
||||
*
|
||||
* Adapted from http://msdn.microsoft.com/en-us/library/cc230309.aspx.
|
||||
* See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
|
||||
*/
|
||||
typedef uint8_t BYTE;
|
||||
typedef uint32_t DWORD;
|
||||
typedef int32_t LONG;
|
||||
typedef uint16_t WORD;
|
||||
|
||||
/**
|
||||
* BITMAPFILEHEADER
|
||||
*
|
||||
* The BITMAPFILEHEADER structure contains information about the type, size,
|
||||
* and layout of a file that contains a DIB [device-independent bitmap].
|
||||
*
|
||||
* Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
WORD bfType;
|
||||
DWORD bfSize;
|
||||
WORD bfReserved1;
|
||||
WORD bfReserved2;
|
||||
DWORD bfOffBits;
|
||||
} __attribute__((__packed__))
|
||||
BITMAPFILEHEADER;
|
||||
|
||||
/**
|
||||
* BITMAPINFOHEADER
|
||||
*
|
||||
* The BITMAPINFOHEADER structure contains information about the
|
||||
* dimensions and color format of a DIB [device-independent bitmap].
|
||||
*
|
||||
* Adapted from http://msdn.microsoft.com/en-us/library/dd183376(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;
|
||||
|
||||
/**
|
||||
* RGBTRIPLE
|
||||
*
|
||||
* This structure describes a color consisting of relative intensities of
|
||||
* red, green, and blue.
|
||||
*
|
||||
* Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
BYTE rgbtBlue;
|
||||
BYTE rgbtGreen;
|
||||
BYTE rgbtRed;
|
||||
} __attribute__((__packed__))
|
||||
RGBTRIPLE;
|
||||
@@ -0,0 +1,111 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "helpers.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// ensure proper usage
|
||||
if (argc != 3)
|
||||
{
|
||||
printf("Usage: colorize infile outfile\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// remember filenames
|
||||
char *infile = argv[1];
|
||||
char *outfile = argv[2];
|
||||
|
||||
// open input file
|
||||
FILE *inptr = fopen(infile, "r");
|
||||
if (inptr == NULL)
|
||||
{
|
||||
printf("Could not open %s.\n", infile);
|
||||
return 4;
|
||||
}
|
||||
|
||||
// open output file
|
||||
FILE *outptr = fopen(outfile, "w");
|
||||
if (outptr == NULL)
|
||||
{
|
||||
fclose(inptr);
|
||||
printf("Could not create %s.\n", outfile);
|
||||
return 5;
|
||||
}
|
||||
|
||||
// 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);
|
||||
printf("Unsupported file format.\n");
|
||||
return 6;
|
||||
}
|
||||
|
||||
int height = abs(bi.biHeight);
|
||||
int width = bi.biWidth;
|
||||
|
||||
// allocate memory for image
|
||||
RGBTRIPLE (*image)[width] = calloc(height, width * sizeof(RGBTRIPLE));
|
||||
if (image == NULL)
|
||||
{
|
||||
printf("Not enough memory to store image.\n");
|
||||
fclose(outptr);
|
||||
fclose(inptr);
|
||||
return 7;
|
||||
}
|
||||
|
||||
// determine padding for scanlines
|
||||
int padding = (4 - (width * sizeof(RGBTRIPLE)) % 4) % 4;
|
||||
|
||||
// iterate over infile's scanlines
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
// read row into pixel array
|
||||
fread(image[i], sizeof(RGBTRIPLE), width, inptr);
|
||||
|
||||
// skip over padding
|
||||
fseek(inptr, padding, SEEK_CUR);
|
||||
}
|
||||
|
||||
colorize(height, width, image);
|
||||
|
||||
// write outfile's BITMAPFILEHEADER
|
||||
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
|
||||
|
||||
// write outfile's BITMAPINFOHEADER
|
||||
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
|
||||
|
||||
// write new pixels to outfile
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
// write row to outfile
|
||||
fwrite(image[i], sizeof(RGBTRIPLE), width, outptr);
|
||||
|
||||
// write padding at end of row
|
||||
for (int k = 0; k < padding; k++)
|
||||
{
|
||||
fputc(0x00, outptr);
|
||||
}
|
||||
}
|
||||
|
||||
// free memory for image
|
||||
free(image);
|
||||
|
||||
// close infile
|
||||
fclose(inptr);
|
||||
|
||||
// close outfile
|
||||
fclose(outptr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "helpers.h"
|
||||
|
||||
void colorize(int height, int width, RGBTRIPLE image[height][width])
|
||||
{
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
for (int j = 0; j < width; j++)
|
||||
{
|
||||
if (image[i][j].rgbtRed == 0x00 && image[i][j].rgbtGreen == 0x00 && image[i][j].rgbtBlue == 0x00)
|
||||
{
|
||||
image[i][j].rgbtBlue = 0xf0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "bmp.h"
|
||||
|
||||
// colorize image
|
||||
void colorize(int height, int width, RGBTRIPLE image[height][width]);
|
||||
|
||||
Reference in New Issue
Block a user