lvcpp
lvcpp

Reputation: 169

Open and write BMP file in C under Linux, or porting existing Windows app to Linux [Close]

Task: read bmp file (byte by byte) and write it to another bmp file.

How to port this short app from Windows to Linux in C ?

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h> 
int main()
{       
     BITMAPFILEHEADER bfh_l;
     BITMAPINFOHEADER bih_l;
     RGBTRIPLE rgb_l;

     FILE * f1, * f2;
     f1 = fopen("1.bmp", "r+b");
     f2 = fopen("3.bmp", "w+b");
     int i, j;
     size_t t;
     fread(&bfh_l,sizeof(bfh_l),1,f1);
     fwrite(&bfh_l, sizeof(bfh_l), 1, f2);
     fread(&bih_l,sizeof(bih_l),1,f1);
     fwrite(&bih_l, sizeof(bih_l), 1, f2);

     size_t padding = 0;
     if ((bih_l.biWidth * 3) % 4)
        padding = 4 - (bih_l.biWidth * 3) % 4;
     for( i=0;i< bih_l.biHeight;i++)
     {
         for ( j = 0; j < bih_l.biWidth; j++)
         {
             fread(&rgb_l, sizeof(rgb_l),1, f1);
             fwrite(&rgb_l, sizeof(rgb_l), 1, f2);
         }
         for ( t = 0; t < padding; ++t) 
         {
             fread(&rgb_l, sizeof(rgb_l),1, f1);
             fwrite(&rgb_l, sizeof(rgb_l), 1, f2);
         }
     }
     fclose(f1);
     fclose(f2);
     return 0;
}

I'm looking for a simple and if possible cross-platform solution.

Upvotes: 0

Views: 720

Answers (1)

user0815
user0815

Reputation: 1406

The only Windows specific thing in your code is that you use 3 structures defined in Windows.h. You could simply define them by yourself as there is no magic behind it. The definitions can be found here:

To convert the wrapper types (e.g. DWORD) to native C types you can use this list.

Upvotes: 5

Related Questions