|
| 1 | +//+------------------------------------------------------------------+ |
| 2 | +//| FileBMP.mqh | |
| 3 | +//| Copyright 2020, MetaQuotes Software Corp. | |
| 4 | +//| https://www.mql5.com | |
| 5 | +//+------------------------------------------------------------------+ |
| 6 | +#include <Object.mqh> |
| 7 | + |
| 8 | +//+------------------------------------------------------------------+ |
| 9 | +//| Bitmap headers | |
| 10 | +//+------------------------------------------------------------------+ |
| 11 | +struct BITMAPFILEHEADER |
| 12 | + { |
| 13 | + ushort bfType; |
| 14 | + uint bfSize; |
| 15 | + ushort bfReserved1; |
| 16 | + ushort bfReserved2; |
| 17 | + uint bfOffBits; |
| 18 | + }; |
| 19 | +struct BITMAPINFOHEADER |
| 20 | + { |
| 21 | + uint biSize; |
| 22 | + int biWidth; |
| 23 | + int biHeight; |
| 24 | + ushort biPlanes; |
| 25 | + ushort biBitCount; |
| 26 | + uint biCompression; |
| 27 | + uint biSizeImage; |
| 28 | + int biXPelsPerMeter; |
| 29 | + int biYPelsPerMeter; |
| 30 | + uint biClrUsed; |
| 31 | + uint biClrImportant; |
| 32 | + }; |
| 33 | +#define BM 0x4D42 |
| 34 | +//+------------------------------------------------------------------+ |
| 35 | +//| Class CFileBMP | |
| 36 | +//| Purpose: Special class to read and write bmp file | |
| 37 | +//| Derives from class CObject. | |
| 38 | +//+------------------------------------------------------------------+ |
| 39 | +class CFileBMP : public CObject |
| 40 | + { |
| 41 | +protected: |
| 42 | + int m_handle; |
| 43 | + BITMAPFILEHEADER m_file_header; |
| 44 | + BITMAPINFOHEADER m_info_header; |
| 45 | + |
| 46 | +public: |
| 47 | + CFileBMP(void); |
| 48 | + ~CFileBMP(void); |
| 49 | + int OpenWrite(const string file_name,bool common_flag=false); |
| 50 | + int OpenRead(const string file_name,bool common_flag=false); |
| 51 | + int Write32BitsArray(uint& uint_array[],const int width,const int height); |
| 52 | + int Read32BitsArray(uint& uint_array[],int& width,int& height); |
| 53 | + void Close(void); |
| 54 | + }; |
| 55 | +//+------------------------------------------------------------------+ |
| 56 | +//| Constructor | |
| 57 | +//+------------------------------------------------------------------+ |
| 58 | +CFileBMP::CFileBMP(void) : m_handle(INVALID_HANDLE) |
| 59 | + { |
| 60 | + ZeroMemory(m_file_header); |
| 61 | + ZeroMemory(m_info_header); |
| 62 | + } |
| 63 | +//+------------------------------------------------------------------+ |
| 64 | +//| Destructor | |
| 65 | +//+------------------------------------------------------------------+ |
| 66 | +CFileBMP::~CFileBMP(void) |
| 67 | + { |
| 68 | + Close(); |
| 69 | + } |
| 70 | +//+------------------------------------------------------------------+ |
| 71 | +//| Open the file | |
| 72 | +//+------------------------------------------------------------------+ |
| 73 | +int CFileBMP::OpenWrite(const string file_name,bool common_flag) |
| 74 | + { |
| 75 | + Close(); |
| 76 | +//--- action |
| 77 | + int open_flags=FILE_BIN|FILE_WRITE|FILE_SHARE_READ|FILE_SHARE_WRITE; |
| 78 | + if(common_flag) |
| 79 | + open_flags|=FILE_COMMON; |
| 80 | +//--- open |
| 81 | + m_handle=FileOpen(file_name,open_flags); |
| 82 | +//--- result |
| 83 | + return(m_handle); |
| 84 | + } |
| 85 | +//+------------------------------------------------------------------+ |
| 86 | +//| Open the file | |
| 87 | +//+------------------------------------------------------------------+ |
| 88 | +int CFileBMP::OpenRead(const string file_name,bool common_flag) |
| 89 | + { |
| 90 | + Close(); |
| 91 | +//--- action |
| 92 | + int open_flags=FILE_BIN|FILE_READ|FILE_SHARE_READ|FILE_SHARE_WRITE; |
| 93 | + if(common_flag) |
| 94 | + open_flags|=FILE_COMMON; |
| 95 | +//--- open |
| 96 | + m_handle=FileOpen(file_name,open_flags); |
| 97 | +//--- check bmp headers |
| 98 | + if(m_handle!=INVALID_HANDLE) |
| 99 | + { |
| 100 | + uint fileheader_size=FileReadStruct(m_handle,m_file_header); |
| 101 | + uint infoheader_size=FileReadStruct(m_handle,m_info_header); |
| 102 | + //--- it should be a simple 32-bit bmp |
| 103 | + if(fileheader_size!=sizeof(m_file_header) || |
| 104 | + infoheader_size!=sizeof(m_info_header) || |
| 105 | + m_file_header.bfType!=BM || |
| 106 | + m_file_header.bfOffBits!=sizeof(m_file_header)+sizeof(m_info_header) || |
| 107 | + m_info_header.biBitCount!=32 || |
| 108 | + m_info_header.biClrUsed!=0) |
| 109 | + Close(); |
| 110 | + } |
| 111 | +//--- result |
| 112 | + return(m_handle); |
| 113 | + } |
| 114 | +//+------------------------------------------------------------------+ |
| 115 | +//| Write the file | |
| 116 | +//+------------------------------------------------------------------+ |
| 117 | +int CFileBMP::Write32BitsArray(uint& uint_array[],const int width,const int height) |
| 118 | + { |
| 119 | + if(m_handle==INVALID_HANDLE) |
| 120 | + return(-1); |
| 121 | +//--- check size |
| 122 | + int size=width*height; |
| 123 | + if(size==0) |
| 124 | + return(0); |
| 125 | + if(size<0) |
| 126 | + size=-size; |
| 127 | + if(ArraySize(uint_array)<size) |
| 128 | + return(-2); |
| 129 | +//--- prepare headers |
| 130 | + ZeroMemory(m_file_header); |
| 131 | + ZeroMemory(m_info_header); |
| 132 | + m_file_header.bfType=BM; |
| 133 | + m_file_header.bfSize=sizeof(m_file_header)+sizeof(m_info_header)+size*sizeof(uint); |
| 134 | + m_file_header.bfOffBits=sizeof(m_file_header)+sizeof(m_info_header); |
| 135 | + m_info_header.biSize=sizeof(m_info_header); |
| 136 | + m_info_header.biWidth=width; |
| 137 | + m_info_header.biHeight=height; |
| 138 | + m_info_header.biPlanes=1; |
| 139 | + m_info_header.biBitCount=32; |
| 140 | + m_info_header.biSizeImage=size*32; |
| 141 | +//--- write bmp-file |
| 142 | + FileSeek(m_handle,0,SEEK_SET); |
| 143 | + FileWriteStruct(m_handle,m_file_header); |
| 144 | + FileWriteStruct(m_handle,m_info_header); |
| 145 | + uint written=FileWriteArray(m_handle,uint_array,0,size); |
| 146 | +//--- bytes written |
| 147 | + return((int)written*32); |
| 148 | + } |
| 149 | +//+------------------------------------------------------------------+ |
| 150 | +//| Read the file | |
| 151 | +//+------------------------------------------------------------------+ |
| 152 | +int CFileBMP::Read32BitsArray(uint& uint_array[],int& width,int& height) |
| 153 | + { |
| 154 | + if(m_handle==INVALID_HANDLE) |
| 155 | + return(-1); |
| 156 | +//--- store dimensions from header |
| 157 | + width=m_info_header.biWidth; |
| 158 | + height=m_info_header.biHeight; |
| 159 | +//--- check size |
| 160 | + int size=width*height; |
| 161 | + if(size==0) |
| 162 | + return(0); |
| 163 | + if(size<0) |
| 164 | + size=-size; |
| 165 | +//--- read bmp-file |
| 166 | + FileSeek(m_handle,sizeof(m_file_header)+sizeof(m_info_header),SEEK_SET); |
| 167 | + uint read=FileReadArray(m_handle,uint_array,0,size); |
| 168 | +//--- bytes read |
| 169 | + return((int)read*32); |
| 170 | + } |
| 171 | +//+------------------------------------------------------------------+ |
| 172 | +//| Close the file | |
| 173 | +//+------------------------------------------------------------------+ |
| 174 | +void CFileBMP::Close(void) |
| 175 | + { |
| 176 | +//--- check handle |
| 177 | + if(m_handle!=INVALID_HANDLE) |
| 178 | + { |
| 179 | + FileClose(m_handle); |
| 180 | + m_handle=INVALID_HANDLE; |
| 181 | + } |
| 182 | + } |
| 183 | +//+------------------------------------------------------------------+ |
0 commit comments