bitmap plz

the code doesnt work plz i dont know where is the wrong.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/ tring.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

#define BYTE    unsigned char 
#define WORD    unsigned short
#define DWORD   unsigned long  
#define NULL	0
#define RGB(r,g,b)    ( ((DWORD)(BYTE)r)|((DWORD)((BYTE)g)<<8)|((DWORD)((BYTE)b)<<16) )
#define GetRValue(RGBColor)    (BYTE) (RGBColor)
#define GetGValue(RGBColor)    (BYTE) (((DWORD)RGBColor) >> 8)
#define GetBValue(RGBColor)    (BYTE) (((DWORD)RGBColor) >> 16)

#pragma pack(2)


struct BITMAPFILEHEADER
{
    WORD    bfType;
    DWORD   bfSize;
    WORD    bfReserved1;
    WORD    bfReserved2;
    DWORD   bfOffBits;
};

struct BITMAPINFOHEADER
{
    DWORD    biSize;
    DWORD    biWidth;
    DWORD    biHeight;
    WORD    biPlanes;
    WORD    biBitCount;
    DWORD    biCompression;
    DWORD    biSizeImage;
    DWORD    biXPelsPerMeter;
    DWORD    biYPelsPerMeter;
    DWORD    biClrUsed;
    DWORD    biClrImportant;
};



class CBitmap
{
    DWORD*    Colors;
    DWORD    Width;
    DWORD    Height;
	

public:
    
    DWORD GetWidth(){return Width;};
    DWORD GetHeight(){return Height;};

    CBitmap()
    {
        Colors = NULL;
        Width = Height = 0;
    }
    ~CBitmap()
    {
        if(Colors)delete[] Colors;
    }

    DWORD& Access(int x, int y)
    {
        return Colors[x*Height+y];
    }





bool Load(const char* FileName)
    {
        BITMAPFILEHEADER    BitmapFileHeader;
        BITMAPINFOHEADER    BitmapInfoHeader;

        ifstream infile(FileName, ios::binary);
        infile.read((char*)&BitmapFileHeader, sizeof(BitmapFileHeader));
        infile.read((char*)&BitmapInfoHeader, sizeof(BitmapInfoHeader));

        if(BitmapFileHeader.bfType != 0x4d42 
            || BitmapInfoHeader.biClrUsed!=0
            || BitmapInfoHeader.biBitCount!=24 
            || BitmapInfoHeader.biCompression!=0 
            || BitmapInfoHeader.biPlanes!=1)return false;

        Width = BitmapInfoHeader.biWidth;
        Height = BitmapInfoHeader.biHeight;

        BYTE* ColorsData = new BYTE[3*Width*Height];
        Colors = new DWORD[Width*Height];
        
        infile.read((char*)ColorsData, 3*Width*Height);

        DWORD    Offset = 0;

        for(int y=Height-1;y>=0;y--)
        {
            for(int x=0;x<(int)Width;x++, Offset+=3)
            {
                BYTE B = ColorsData[Offset + 0];
                BYTE G = ColorsData[Offset + 1];
                BYTE R = ColorsData[Offset + 2];
                Access(x,y) = RGB(R,G,B);
            }
        }

        delete[] ColorsData;

        infile.close();
        return true;
    }

    bool Save(const char* FileName)
    {
        if(Colors==NULL)return false;

        BITMAPFILEHEADER    BitmapFileHeader = {
            0x4d42, //Bmp Mark
            sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * Width * Height, //File Size
            0,    //Not Used
            0,    //Not Used
            sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)    //Colors Offset in File
        };
        
        BITMAPINFOHEADER    BitmapInfoHeader = {
            sizeof(BITMAPINFOHEADER),    //Structure Size
            Width,
            Height,
            1,        //Number of Plans
            24,        //Bits Per colors 
            0,        //Compression Ration
            0,        //Original Image Size (When Compressed)
            2835,    //Number of Pixel Per Meter (Vertical)
            2835,    //Number of Pixel Per Meter (Horizontal)
            0,        //Number of Used Colors (Indexed Mode)
            0,        //Number of Important Colors (Indexed Mode)
        };

        BYTE* ColorsData = new BYTE[3*Width*Height];

        DWORD    Offset = 0;

        for(int y=Height-1;y>=0;y--)
        {
            for(int x=0;x<(int)Width;x++, Offset+=3)
            {
                DWORD    Color = Access(x,y);
                ColorsData[Offset + 0] = GetBValue(Color);
                ColorsData[Offset + 1] = GetGValue(Color);
                ColorsData[Offset + 2] = GetRValue(Color);
            }
        }


        ofstream outfile(FileName, ios::binary);
        outfile.write((char*)&BitmapFileHeader, sizeof(BitmapFileHeader));
        outfile.write((char*)&BitmapInfoHeader, sizeof(BitmapInfoHeader));
        outfile.write((char*)ColorsData, 3*Width*Height);
        outfile.close();

        delete[] ColorsData;
        return false;
    }

void Create(int W, int H, DWORD BackColor)
       {
               if(Colors)
                    delete[] Colors;
                
                Width = W;
                Height = H;
                 
                if(Width*Height==0)
                {
                      Colors = NULL; 
                       return;
                 }
 
                 Colors = new DWORD[Width*Height];
                  
                 for(DWORD y=0;y<Height;y++)
                       for(DWORD x=0;x<Width;x++)
                               Access(x,y) = BackColor;
         }
 

void RedScale (CBitmap& Result)
{
	         DWORD R;
         for(int y=0;y <Height;y++)
        {
            for(int x=0;x<(int)Width;x++)
            {
			DWORD Color = Access(x,y);
              R= GetRValue(Color) ;
              Result.Access(x,y) = RGB(R,0,0);   
            }
        }

}
};





int _tmain(int argc, _TCHAR* argv[])
{
CBitmap image,Result;

image.Load(" Menna.bmp ");
Result.Create ( image.GetWidth(), image.GetHeight(), RGB  (0, 0, 0 ) );
image.RedScale(Result); 
Result.Save ( "Menna_2.bmp" );

return 0;

}

In line 219 above, you shouldn't have the spaces in the string.
thnxs for helping buy this is not the problem
Last edited on
What error messages are you getting?
thnxs for helping buy this is not the problem


That's strange. It works for me.
closed account (z05DSL3A)
Turn the bitmap read for me, well it did once I made sure the bitmap file was a 24bit one. ;0)

Only changes to the code
1
2
3
4
5
...
    image.Load("c:\\test.bmp");
...
    Result.Save ("c:\\test2.bmp");
...


EDIT:
One small point, as the CBitmap Height and Width are DWORD your for loop indexes should also be DWORD. Well looking at it, you are generally a bit sloppy in matching your data types.
1
2
3
4
5
6
7
8
9
10
11
12
13
void RedScale (CBitmap& Result)
    {
        DWORD R;
        for(DWORD  y = 0; y < Height; ++y)
        {
            for(DWORD  x = 0; x < Width; ++x)
            {
                DWORD Color = Access(x,y);
                R= GetRValue(Color) ;
                Result.Access(x,y) = RGB(R,0,0);   
            }
        }
    }

Last edited on
Topic archived. No new replies allowed.