Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp.h"
- int main(int argc, char *argv[]) {
- // обеспечить правильное использование
- if (argc != 3) {
- printf("Usage: ./copy infile outfilen");
- return 1;
- }
- // запомнить имена файлов
- char* infile = argv[1];
- char* outfile = argv[2];
- // открыть входной файл
- FILE* inptr = fopen(infile, "r");
- if (inptr == NULL) {
- printf("Could not open %s.n", infile);
- return 2;
- }
- // открыть выходной файл
- FILE* outptr = fopen(outfile, "w");
- if (outptr == NULL) {
- fclose(inptr);
- fprintf(stderr, "Could not create %s.n", outfile);
- return 3;
- }
- // чтение файла infile типа BITMAPFILEHEADER
- BITMAPFILEHEADER bf;
- fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
- // чтение файла infile типа BITMAPINFOHEADER
- BITMAPINFOHEADER bi;
- fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
- // убедитесь, что infile (вероятно) 24-разрядный несжатый 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;
- }
- // запись outfile в BITMAPFILEHEADER
- fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
- // запись outfile в BITMAPINFOHEADER
- fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
- // определение отступов для сканирования строк
- int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
- // перебрать строки в infile
- for(int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++) {
- // итерация по пикселям в scanline
- for(int j = 0; j < bi.biWidth; j++) {
- // временное хранилище
- RGBTRIPLE triple;
- // считать RGB тройной от infile
- fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
- // Если RGB полностью красный то заменить на белый
- if(triple.rgbtRed == 0xff && triple.rgbtGreen == 0x00 && triple.rgbtBlue == 0x00) {
- triple.rgbtBlue = 0xff;
- triple.rgbtGreen = 0xff;
- triple.rgbtRed = 0xff;
- }
- // записать RGB тройной для outfile
- fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
- }
- // пропустить отступ, если он есть
- fseek(inptr, padding, SEEK_CUR);
- // затем добавьте его обратно (чтобы продемонстрировать, как)
- for (int k = 0; k < padding; k++) {
- fputc(0x00, outptr);
- }
- }
- // закрыть infile
- fclose(inptr);
- // закрыть outfile
- fclose(outptr);
- return 0;
- }
- #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;
Add Comment
Please, Sign In to add comment