Quicker way to get screenshot to color array?

At the moment, this program has the function 'rgb_extended l1see()' which returns a struct containing the height and width of the screen, along with an array of RGB color values for each pixel. Problematically, the meathod I am currently using takes over 400 milliseconds to do at times, and I was wondering if anyone can tell me how to shorten the time it takes, or at least, not make it create and write to a file, just to close it and open it back up again and read what it just wrote. (The first part I got from a website, the second I wrote myself.)

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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

#include <windows.h>
#include <iostream>
#include <winable.h>
#include <fstream>
#include <sstream>
#include <SDL.h>

using namespace std;

struct bmp_header
{
char magicnumber[2];
char size[4];
char unused1[2];
char unused2[2];
char pixoffset[4];
char numheaderbytes[4];
char width[4];
char height[4];
char panels[2];
char bpp[2];
char format[4];
char datasize[4];
char widthres[4];
char heightres[4];
char numcolorpalett[4];
char importantcolors[4];
};

struct simple_rgb
{
       unsigned char r,g,b;
       string rgbstr(){return istr((unsigned int)r)+":"+istr((unsigned int)g)+":"+istr((unsigned int)b);};
};

struct extended_rgb
{
       simple_rgb *pixels;
       int width,height;
       void alloc(){pixels = new simple_rgb[width*height];};
};

unsigned int char4toint(unsigned char a,unsigned char b,unsigned char c,unsigned char d)
{
    unsigned int ia,ib,ic,id;
    ia = (unsigned int)a;
    ib = (unsigned int)b;
    ic = (unsigned int)c;
    id = (unsigned int)d; 
    unsigned int ret = 0;
    ret += ia;
    ret += 256*ib;
    ret += 256*256*ic;
    ret += 256*256*256*id;
    return ret;
}

void output(string out)
{
       fstream err("general.log",ios::out|ios::app);
       err<<SDL_GetTicks()<<"ms : "<<out<<"\n";
       err.close();
}

extended_rgb *l1see()
{
     // Create a normal DC and a memory DC for the entire screen. The 
	// normal DC provides a "snapshot" of the screen contents. The 
	// memory DC keeps a copy of this "snapshot" in the associated 
	// bitmap. 
 
	HDC hdcScreen = CreateDC("DISPLAY", NULL, NULL, NULL); 
	HDC hdcCompatible = CreateCompatibleDC(hdcScreen); 
 
	// Create a compatible bitmap for hdcScreen. 
 
	HBITMAP hbmScreen = CreateCompatibleBitmap(hdcScreen, 
                     GetDeviceCaps(hdcScreen, HORZRES), 
                     GetDeviceCaps(hdcScreen, VERTRES)); 
 
	if (hbmScreen == 0) 
	{
		output("hbmscreen");
		return NULL;
    }
 
	// Select the bitmaps into the compatible DC. 
 
	if (!SelectObject(hdcCompatible, hbmScreen))
    { 
		output("Bitmap selection");
		return NULL;
    }
 
		 //Copy color data for the entire display into a 
		 //bitmap that is selected into a compatible DC. 
 			if (!BitBlt(hdcCompatible, 
				   0,0, 
				   GetDeviceCaps(hdcScreen, HORZRES), GetDeviceCaps(hdcScreen, VERTRES), 
				   hdcScreen, 
				   0,0, 
				   SRCCOPY)) 
				   {
 			output("screen blt failed");
 			return NULL;
        }
 			
    BITMAP bmp; 
    PBITMAPINFO pbmi; 
    WORD    cClrBits; 

    // Retrieve the bitmap color format, width, and height. 
    if (!GetObject(hbmScreen, sizeof(BITMAP), (LPSTR)&bmp)) 
    {
 		output("getobject");
 		return NULL;
     }
 		
    // Convert the color format to a count of bits. 
    cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
    if (cClrBits == 1) 
        cClrBits = 1; 
    else if (cClrBits <= 4) 
        cClrBits = 4; 
    else if (cClrBits <= 8) 
        cClrBits = 8; 
    else if (cClrBits <= 16) 
        cClrBits = 16; 
    else if (cClrBits <= 24) 
        cClrBits = 24; 
    else cClrBits = 32; 

    // Allocate memory for the BITMAPINFO structure. (This structure 
    // contains a BITMAPINFOHEADER structure and an array of RGBQUAD 
    // data structures.) 

     if (cClrBits != 24) 
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                    sizeof(BITMAPINFOHEADER) + 
                    sizeof(RGBQUAD) * (1<< cClrBits)); 

     // There is no RGBQUAD array for the 24-bit-per-pixel format. 

     else 
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                    sizeof(BITMAPINFOHEADER)); 

    // Initialize the fields in the BITMAPINFO structure. 

    pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    pbmi->bmiHeader.biWidth = bmp.bmWidth; 
    pbmi->bmiHeader.biHeight = bmp.bmHeight; 
    pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
    pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
    if (cClrBits < 24) 
        pbmi->bmiHeader.biClrUsed = (1<<cClrBits); 

    // If the bitmap is not compressed, set the BI_RGB flag. 
    pbmi->bmiHeader.biCompression = BI_RGB; 

    // Compute the number of bytes in the array of color 
    // indices and store the result in biSizeImage. 
    // For Windows NT, the width must be DWORD aligned unless 
    // the bitmap is RLE compressed. This example shows this. 
    // For Windows 95/98/Me, the width must be WORD aligned unless the 
    // bitmap is RLE compressed.
    pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
                                  * pbmi->bmiHeader.biHeight; 
    // Set biClrImportant to 0, indicating that all of the 
    // device colors are important. 
     pbmi->bmiHeader.biClrImportant = 0; 
     			
     HANDLE hf;                 // file handle 
    BITMAPFILEHEADER hdr;       // bitmap file-header 
    PBITMAPINFOHEADER pbih;     // bitmap info-header 
    LPBYTE lpBits;              // memory pointer 
    DWORD dwTotal;              // total count of bytes 
    DWORD cb;                   // incremental count of bytes 
    BYTE *hp;                   // byte pointer 
    DWORD dwTmp; 

    pbih = (PBITMAPINFOHEADER) pbmi; 
    lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);

    if (!lpBits) 
    {
		 output("global alloc");
		 return NULL;
      }
		 
    // Retrieve the color table (RGBQUAD array) and the bits 
    // (array of palette indices) from the DIB. 
    if (!GetDIBits(hdcScreen, hbmScreen, 0, (WORD) pbih->biHeight, lpBits, pbmi, 
        DIB_RGB_COLORS)) 
        {
    output("getdibits");
    return NULL;
}
    
    // Create the .BMP file. 
    hf = CreateFile("test.bmp", 
                   GENERIC_READ | GENERIC_WRITE, 
                   (DWORD) 0, 
                    NULL, 
                   CREATE_ALWAYS, 
                   FILE_ATTRIBUTE_NORMAL, 
                   (HANDLE) NULL); 
    if (hf == INVALID_HANDLE_VALUE) 
 {
		output("create file");
		return NULL;
    }
    hdr.bfType = 0x4d42;        // 0x42 = "B" 0x4d = "M" 
    // Compute the size of the entire file. 
    hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + 
                 pbih->biSize + pbih->biClrUsed 
                 * sizeof(RGBQUAD) + pbih->biSizeImage); 
    hdr.bfReserved1 = 0; 
    hdr.bfReserved2 = 0; 

    // Compute the offset to the array of color indices. 
    hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + 
                    pbih->biSize + pbih->biClrUsed 
                    * sizeof (RGBQUAD); 

    // Copy the BITMAPFILEHEADER into the .BMP file. 
    if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), 
        (LPDWORD) &dwTmp,  NULL)) 
        {
    output("write file");
    return NULL;
}

    // Copy the BITMAPINFOHEADER and RGBQUAD array into the file. 
    if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) 
                  + pbih->biClrUsed * sizeof (RGBQUAD), 
                  (LPDWORD) &dwTmp, ( NULL))) 
                  {
 		output("write file");return NULL;}
    // Copy the array of color indices into the .BMP file. 
    dwTotal = cb = pbih->biSizeImage; 
    hp = lpBits;
    if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)) {
   		   output("write file");return NULL;}
    // Close the .BMP file. 
     if (!CloseHandle(hf)) {
 		   output("close file");return NULL;}


End part one, second post will contain part two.
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
    dots tmp;
    tmp.hp = (char*)hp;
    tmp.width = bmp.bmWidth;
    tmp.height = bmp.bmHeight;


    // Free memory. 
    GlobalFree((HGLOBAL)lpBits);
    
    extended_rgb *ret;
    
    fstream file("test.bmp",ios::in|ios::binary);
    if(file.is_open())
    {
                        
                        bmp_header mainheader;
      file.read(mainheader.magicnumber,2);//2
      if(mainheader.magicnumber[0] == 66 && mainheader.magicnumber[1] == 77)
      {
          
          file.read(mainheader.size,4);       //6           
          file.read(mainheader.unused1,2);//8
          file.read(mainheader.unused2,2);//10
          file.read(mainheader.pixoffset,4);//14
          file.read(mainheader.numheaderbytes,4);//18
          file.read(mainheader.width,4);//22
          file.read(mainheader.height,4);//26
          file.read(mainheader.panels,2);//28
          file.read(mainheader.bpp,2);//30
          file.read(mainheader.format,4);//34
          file.read(mainheader.datasize,4);//38
          file.read(mainheader.widthres,4);//42
          file.read(mainheader.heightres,4);//46
          file.read(mainheader.numcolorpalett,4);//50
          file.read(mainheader.importantcolors,4);//54
          
          unsigned int imgwidth = char4toint(mainheader.width[0],mainheader.width[1],mainheader.width[2],mainheader.width[3]);
          unsigned int imgheight = char4toint(mainheader.height[0],mainheader.height[1],mainheader.height[2],mainheader.height[3]);
      
          if(mainheader.bpp[0] != 32)
          {
               output("Bad bitmap type. ");
               cout<<(unsigned int)mainheader.bpp[0]<<"\n";
               return NULL;
          }
          
          
          ret->width = imgwidth;
          ret->height = imgheight;
          ret->alloc();
           
          for(int i = 0; i<imgheight; i++)
          {
               
          for(int z = 0; z<imgwidth; z++)
          {
                  char rgbs[4];
                  file.read(rgbs,4);
                  
                  unsigned char rgb[3];
                  rgb[0] = rgbs[0];
                  rgb[1] = rgbs[1];
                  rgb[2] = rgbs[2];
                  
                  ret->pixels[((imgheight-i)*imgwidth)+(imgwidth-z)].r = rgb[2];
                  ret->pixels[(i*imgwidth)+z].g = rgb[1];
                  ret->pixels[(i*imgwidth)+z].b = rgb[0];
          }
          
          
                  /*int howmany = 0;
                  
                  if(idb(imgwidth*3,4))
                  howmany = 0;    
                  if(idb((imgwidth*3)+1,4))
                  howmany = 1;
                  if(idb((imgwidth*3)+2,4))
                  howmany = 2;
                  if(idb((imgwidth*3)+3,4))
                  howmany = 3;
                          
                  char buf[howmany];
                  file.read(buf,howmany); */
          
          }
          }      

                        
    }
    else { output("could not open visual memory"); return NULL; }
    
    file.close();
    
    return ret;
}


And that's the base of it.
Any help?
Topic archived. No new replies allowed.