M
M
Muriam2019-02-01 05:14:57
OOP
Muriam, 2019-02-01 05:14:57

How to figure out what's going on in this header file?

The theme of the project is working with bmp-images.
https://ru.wikipedia.org/wiki/BMP
C++ builder. The bmp.h header file declares: structures, a class (maybe it's a constructor?) and functions. Please explain how it all works to a newbie.
How was this header file included in the project, just #include "bmp.h" or through the c++builder-a menu?

#include <io.h>
#include <stdio.h>
#pragma pack(1)

struct FileHeader 
{
  WORD 	bfType; 
  DWORD   bfSize;
  WORD	bfReserved1;    //не используется
  WORD	bfReserved2;    //не используется
  DWORD   bfOffbits;      //смещение данных битового образа от заголовка в байтах 
};

struct MAPINFO 
{
  DWORD   Size;           //число байт, занимаемых структурой InfoHeader
  DWORD   Width;          //ширина битового образа в пикселях
  DWORD   Height;         //высота битового образа в пикселях 
  WORD    Planes;         //число битовых плоскостей устройства
  WORD    BitCount;       //число битов на пиксель
  DWORD   Compression;    //тип сжатия
  DWORD   SizeImage;      //размер картинки в байтах
  long    XPelsPerMeter;  //горизонтальное разрешение устройства, пиксель/м
  long    YPelPerMeter;   //вертикальное разрешение устройства, пиксель/м
  DWORD   ClrUsed;        //число используемых цветов
  DWORD   ClrImportant;   //число "важных" цветов
};

struct RGBquad 
{
    BYTE    rgbBlue;         //интенсивность голубого
    BYTE    rgbGreen;        //интенсивность зеленого
    BYTE    rgbRed;          //интенсивность красного    
    BYTE    rgbReserved;     //не используется
};

class CBmp
{
private:
  void *pBmp;
  long SizeFile;
public:
  CBmp();
  ~CBmp();
  void Open(char* fn);
  void Save(char* fn);
  FileHeader *GetFH();
  MAPINFO *GetMapInfo();
  RGBquad *GetMap();
};

void CBmp::Save(char* fn)
{
        FILE* File;
        File=fopen(fn,"wb");
        fwrite(pBmp,1,SizeFile,File);
        fclose(File);
}

RGBquad *CBmp::GetMap()
{
  RGBquad *rgb;
  rgb=(RGBquad *)((long)pBmp+sizeof(FileHeader)+sizeof(MAPINFO));
  return rgb;
}

MAPINFO *CBmp::GetMapInfo()
{
  MAPINFO *mi;
  mi=(MAPINFO*)((long)pBmp+sizeof(FileHeader));
  return mi;
}

FileHeader *CBmp::GetFH()
{
  FileHeader *fh;
  fh=(FileHeader *)pBmp;
  return  fh;
}

CBmp::CBmp()
{
  pBmp=0;
  SizeFile=0;
}

CBmp::~CBmp()
{
  delete [] pBmp;
}

void CBmp::Open(char* fn)
{
  FILE* File;
        int hFile;
        File=fopen(fn,"rb");
        hFile=_fileno(File);
        SizeFile=filelength(hFile);
        pBmp=(BYTE*)malloc(SizeFile);
        fread(pBmp,1,SizeFile,File);
        fclose(File);
}

CLARIFICATION:
I want to understand the code above to make in the same way the structure
from the question: How to create a header file?
in my project i need to make a 15 px thick border on a bmp image. The frame consists of pixels of random colors Working with graphics. C++builder. How to draw a border around a picture?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2019-02-01
@Muriam

Learn the concept of "compilation unit". Here, unfortunately, there are things that should be in the CPP file, and things that should be in the H file. We need data structures "one to one", without padding bytes.

struct FileHeader 
struct MAPINFO

.bmp format. Don't forget that the BMP format is written from the bottom line!
The Open function reads the picture "one to one", Save writes "one to one", GetMapInfo and GetFH give out some headers of our BMP.
It remains GetMap (), which, in theory, should produce a matrix of colors, but really only works for 32-bit BMP and does not encapsulate either the width-height of the matrix, or the fact that the BMP format is written from the bottom line.
For this code - three with a minus.
And now what your code does NOT support, but, in theory, should, in order to complete your task.
1. Creating a BMP of the desired size from scratch, rather than loading from a file.
2. Encapsulate a matrix of pixels. It is desirable that there be quick access to lines as buffers in memory, for ease of transferring information from the old BMP to the new one, 30x30 pixels larger.
3. If you have limited support for the BMP format, crash with an error if the version is unsupported (for example, the wrong number of colors).
Is it your task to establish support for BMP on your own ? And then in the Builder there is a TBitmap.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question