I thought the reason during the compilation process, but probably not. The program removes the red color from the original 24 bit image bmp. In a Linux environment, the file is compiled and works. In a Windows environment, it compiles, but does not work. C language, MinGW compiler, Winsows OS [Version 10.0.17134.48]

Help solve the problem.

Thanks in advance to all who took the time.

Log from gdb, he did not help me.

enter image description here

Log cmd

enter image description here

Modified bmp.h

enter image description here

#include <stdio.h> #include <stdlib.h> #include "bmp.h" int main(int argc, char *argv[]) { // обеспечить правильное использование if (argc != 3) { printf("Usage: ./copy infile outfile\n"); return 1; } // запомнить имена файлов char* infile = argv[1]; char* outfile = argv[2]; // открыть входной файл FILE* inptr = fopen(infile, "rb"); if (inptr == NULL) { printf("Could not open %s.\n", infile); return 2; } // открыть выходной файл FILE* outptr = fopen(outfile, "wb"); 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); } } fclose(inptr); fclose(outptr); return 0; } 

Bmp.h code

 #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; 
  • Well ottrassuyte under Windows. - nick_n_a
  • clue.bmp your clue.bmp really a 24-bit uncompressed BMP 4.0 ? Maybe your program works just the same. - zed
  • Yeah, because the code has a check. Maybe the file is "spoiled" :) - KoVadim
  • The file meets the requirements. I don’t even know what to watch, I just don’t understand how it might not work correctly on Windows if it works correctly on Linux. - Mink
  • one
    text information is better to attach as text: a) easier to read; b) can be copied; c) the search works. You can correct the question text by clicking below to edit the question text - aleksandr barakin

2 answers 2

In Mingw, the packing of the structure does not work (see # 275 pack-struct option is broken ).

Add a flag when compiling -mno-ms-bitfields and the packaging will work as expected.

The size of the BITMAPFILEHEADER structure must be 14 bytes - you can use this size for control:

 if ( sizeof(BITMAPFILEHEADER) != 14 ) { printf("pragma pack failed!"); return 1; } 
  • Super finally, thank you very much. - Mink

There is an idea that the standard types of wines are redefined in BMP.h, through typedef, this affects the dimension just in the operations performed. And as an option, try to collect for wines without __attribute__ __packed__ , also affects the dimension.

  • Unfortunately the result is the same. updated in question. - Mink
  • Then an option: make a test copy with BMP.h, where in the hex to drop the block of the BMP file, and carry out one of the operations. Compare output to lnux and wines. Hence, to start the changes, the problem probably lies in BMP.h, under MinGw there are some pitfalls, everything is not 100% compatible there. - NewView
  • @Mink, did fseek () try to call? - 0andriy
  • @ 0andriy, there is a problem with the size of structures and types, the output from the verification of the BMP file header from the author is "Unsupported file format", which indicates a discrepancy of changes in the structure, taking into account that the picture file is her :) - NewView