|
|
16位模式下“555”的问题 |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
当我开始使用DirectX,我决定不再使用旧的256色模式,而是直接开始改用16位模式。有千万种的颜色可使用 是多么的令人愉快。 我买了一本很好的书去学习怎么使用C++和DirectX。我做的第一件事是写一个在屏幕上显示一些点的程序。当 它在另一台计算机运行时,我发现屏幕上所有的颜色都错了!所有的颜色都偏绿了。到底是哪里出错了?一个朋友 告诉我有些图形卡使用了不同的16位模式。 书的作者也提及了这个问题。在他的例子中, 他使用了#define定义RGB值为WORD。这个#define假定图形模式 为555 。这意味着在WORD中的每各通道都用5位剩余的1位不使用。其它的卡使用565模式。(我不知道是否还有其它 的模式,但我写的例程还未发现过问题。) 在高彩模式下,我们用一个WORD来表示一个像素。但由于显卡的差异, 通常会有555和565两种情况。 555是指颜色表示的RGB分量各占5位,如图:
N |
R |
R |
R |
R |
R |
G |
G |
G |
G |
G |
B |
B |
B |
B |
B | 565是指颜色表示的RB分量各占5位,G分量占6位,如图:
R |
R |
R |
R |
R |
G |
G |
G |
G |
G |
G |
B |
B |
B |
B |
B | 书中的#define看起来就象: #define _RGB16BIT (r, g, b) ( (b % 32) + ( (g % 32) << 5)+( (r % 32) << 10)) 如果忘了它,而使用恒值555将不能保证在所有的计算机上正常工作。幸运的是,有一个DX函数可以返回DWORD s 简要地说明了颜色模式(以及一些其它资料)。 这个函数的语法格式是:HRESULT GetpixelFormat(DDPIXELFORMAT x) 如果你想知道绿色用了几位, 你可以查看DWORD中绿色通道设置为几位。因此我写了下面的函数。 使用这个函数必须定义四个全局变量: DWORD redbits, greenbits, bluebits, rgbbits; bool MyGetPixelFormat() { // init the vars: redbits = greenbits = bluebits = rgbbits = 0; // the PixelFormat structure: DDPIXELFORMAT DDPixelFormat; DDPixelFormat.dwSize = sizeof(DDPIXELFORMAT); // don't forget this!
// fill the pixel-format structure: if (lpddsprimary->GetPixelFormat(&DDPixelFormat)!=DD_OK) return false; // * error * //
// fill the global vars with values from the struct: redbits = DDPixelFormat.dwRBitMask; greenbits = DDPixelFormat.dwGBitMask; bluebits = DDPixelFormat.dwBBitMask; rgbbits = DDPixelFormat.dwRGBBitCount;
// now let us see, how much bits each color-channel has got (5 or 6): int bit = 0; bool done = false; DWORD count = 1; // look for # of blue bits: while (!done) { if (bluebits & count) { bit++; count <<= 1; } else done = true; } bluebits = bit; bit = 0; done = false;
// look for # of green bits: while (!done) { if (greenbits & count) { bit++; count <<= 1; } else done = true; } greenbits = bit; bit = 0; done = false;
// look for # of red bits: while (!done) { if (redbits & count) { bit++; count <<= 1; } else done = true; } redbits = bit; /* Now the vars red-, green- and bluebits are filled simply with the number of the channel! Perfect.*/ return true; }
现在我们知道了图形模式(555, 565...)。接着我们需要用前面提到的 #define 做一个函数来工作。我写的这 个函数要用到参数int r, g, b返回一个USHORT。颜色值的范围为0..255。 USHORT _RGB16BIT (int r , int g, int b) { // the return value USHORT retval = 0;
// max value has to be either 32 or 64, // depending on the number of bits of the channel: if (redbits == 6) r >>= 2; else r >>= 3; if (greenbits == 6) g >>= 2; else g >>= 3; if (bluebits == 6) b >>= 2; else b >>= 3;
// as I said, I don't know how many modes there are. If you do, shorten // the function by removing unnessessary lines above. // And please tell me! ;-)
// now build a word of the three channels by shifting them by the // _correct_ number of bits: retval = (USHORT) (b + (g << bluebits) + (r << (bluebits + greenbits))); // return the color: return(retval); } 好了,朋友,就这样!你可以在初始化DDraw之后调用MyGetPixelFormat()函数。然后,你就可以用RGB16BIT (r, g, b)画点或从文件中读入RGB数据等等。 
|
|
相关文章:相关软件: |
|