Replacing the Pixels from one bmp file to another

Hey

Am interesting in copying a pixel from a bmp file located in this link https://imgur.com/la1frjx and move it into the bmp located in https://imgur.com/iwNqkfG. I have worked so hard and made the file to read the bmp files headers. Now the problem is that I don’t have any knowledge on how I can copy pixel from the first bmp to the second bmp. And am only supposed to use the availed libraries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
#include <stdio.h>
#include "windows.h"

int main()
{
    int i;
    FILE* Pic1 = fopen("pic1.bmp", "rb");
    FILE* Pic2 = fopen("pic2.bmp", "rb+");
    unsigned char infoPic1[54];
    fread(infoPic1, sizeof(unsigned char), 54, Pic1); // read the 54-byte header from Pic1

    // extract image height and width from Pic1
    int widthPic1 = *(int*)&infoPic1[18];
    int heightPic1 = *(int*)&infoPic1[22];

    int sizePic1 = 3 * widthPic1 * heightPic1;

    unsigned char infoPic2[54];
    fread(infoPic2, sizeof(unsigned char), 54, Pic2); // read the 54-byte header from Pic2

    // extract image height and width from Pic2
    int widthPic2 = *(int*)&infoPic2[18];
    int heightPic2 = *(int*)&infoPic2[22];

    int sizePic2 = 3 * widthPic2 * heightPic2;

    printf("Pic1 is %i*%i pixels and %ibyte\nPic2 is %i*%i and %ibytes\n\n", heightPic1, widthPic1, sizePic1, heightPic2, widthPic2, sizePic2);

    system("PAUSE");
    return 0;
}

https://www.theengineeringprojects.com/2019/11/introduction-to-namespaces-in-c-sharp.html
Last edited on
Line 14: There is no need for the casting. int widthPic1 = infoPic1[18]; suffice. The same applies to line 15/23/24.

After line 17 you can do this:
1
2
    unsigned char *data1 = new unsigned char[sizePic1];
    fread(infoPic1, sizeof(unsigned char), sizePic1, data1); // read the 54-byte header from Pic1 
Similar for Pic2.

Now you have an 1d array. To get the index from 2d coordinates you need to calculate:

int idx1 = (y * widthPic1 + x) * 3; // The same for Pic2 with widthPic2

This way you can access/copy the 3 values of the pixel relatively easy.
First step would be some reading.
https://en.wikipedia.org/wiki/BMP_file_format

But before that, they're PNG files.
undefined - Imgur.png: PNG image data, 325 x 485, 8-bit/color RGB, non-interlaced
iwNqkfG - Imgur.png: PNG image data, 1024 x 1033, 8-bit/color RGB, non-interlaced

> This way you can access/copy the 3 values of the pixel relatively easy.
This only works if the BMP file is in BI_RGB format, and the width is a multiple of 4.
This only works if the BMP file is in BI_RGB format, and the width is a multiple of 4.
For the real format there are most likely some adjustments necessary. This is just to give an idea.
See if this helps. Its GDI+ library (built into visual studio, ready to go).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   GpStatus gps{}; ///set up gdi+
   GdiplusStartupInput forizzle; 
   ULONG_PTR           token;   
   gps = GdiplusStartup(&token, &forizzle, nullptr);   
   ///load the image file.  
   Bitmap original(argv[1]);
   ///process image.    
   uint32_t w{ original.GetWidth()},h{ original.GetHeight()};
   Gdiplus::BitmapData data;
   Gdiplus::Rect sq(0, 0, w, h);
   ///useful code showing how to get the RGBA byte data in a usable format.  
   original.LockBits(&sq,
       Gdiplus::ImageLockModeWrite, PixelFormat32bppARGB, &data);
   uint8_t * cp = (uint8_t*)(data.Scan0);         
   
   for(int i = 0; i < w*h*4; i+=4) //go over all pixels,    
   {             
   //logic and code to change pixels.  
         cp[i] = ??;             
   } 


BMP files are complicated, there are several formats.
PNG files are beyond complicated, and you would not want to try to read one by hand at all.
Last edited on
Topic archived. No new replies allowed.