本文整理汇总了C++中CByteImage::PixelAddress方法的典型用法代码示例。如果您正苦于以下问题:C++ CByteImage::PixelAddress方法的具体用法?C++ CByteImage::PixelAddress怎么用?C++ CByteImage::PixelAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CByteImage
的用法示例。
在下文中一共展示了CByteImage::PixelAddress方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ReadFileTGA
void ReadFileTGA(CByteImage& img, const char* filename)
{
// Open the file and read the header
FILE *stream = fopen(filename, "rb");
if (stream == 0)
throw CError("ReadFileTGA: could not open %s", filename);
CTargaHead h;
if (fread(&h, sizeof(CTargaHead), 1, stream) != 1)
throw CError("ReadFileTGA(%s): file is too short", filename);
// Throw away the image descriptor
if (h.idLength > 0)
{
char* tmp = new char[h.idLength];
int nread = fread(tmp, sizeof(uchar), h.idLength, stream);
delete tmp; // throw away this data
if (nread != h.idLength)
throw CError("ReadFileTGA(%s): file is too short", filename);
}
bool isRun = (h.imageType & 8) != 0;
bool reverseRows = (h.descriptor & TargaScreenOrigin) != 0;
int fileBytes = (h.pixelSize + 7) / 8;
// Read the colormap
uchar colormap[TargaCMapSize][TargaCMapBands];
int cMapSize = 0;
bool grayRamp = false;
if (h.colorMapType == 1)
{
cMapSize = (h.cMapLength[1] << 8) + h.cMapLength[0];
if (h.cMapBits != 24)
throw CError("ReadFileTGA(%s): only 24-bit colormap currently supported", filename);
int l = fileBytes * cMapSize;
if (l > TargaCMapSize * TargaCMapBands)
throw CError("ReadFileTGA(%s): colormap is too large", filename);
if (fread(colormap, sizeof(uchar), l, stream) != l)
throw CError("ReadFileTGA(%s): could not read the colormap", filename);
// Check if it's just a standard gray ramp
int i;
for (i = 0; i < cMapSize; i++) {
for (int j = 0; j < TargaCMapBands; j++)
if (colormap[i][j] != i)
break;
}
grayRamp = (i == cMapSize); // didn't break out too soon
}
bool isGray =
h.imageType == TargaRawBW || h.imageType == TargaRunBW ||
grayRamp &&
(h.imageType == TargaRawColormap || h.imageType == TargaRunColormap);
bool isRaw = h.imageType == TargaRawBW || h.imageType == TargaRawRGB ||
h.imageType == TargaRawRGB && isGray;
// Determine the image shape
CShape sh(h.width, h.height, (isGray) ? 1 : 4);
// Allocate the image if necessary
img.ReAllocate(sh, false);
// Construct a run-length code reader
CTargaRLC rlc(! isRaw);
// Read in the rows
for (int y = 0; y < sh.height; y++)
{
int yr = reverseRows ? sh.height-1-y : y;
uchar* ptr = (uchar *) img.PixelAddress(0, yr, 0);
if (fileBytes == sh.nBands && isRaw)
{
// Special case for raw image, same as destination
int n = sh.width*sh.nBands;
if (fread(ptr, sizeof(uchar), n, stream) != n)
throw CError("ReadFileTGA(%s): file is too short", filename);
}
else
{
// Read one pixel at a time
for (int x = 0; x < sh.width; x++, ptr += sh.nBands)
{
uchar* buf = rlc.getBytes(fileBytes, stream);
if (fileBytes == 1 && sh.nBands == 1)
{
ptr[0] = buf[0];
}
else if (fileBytes == 1 && sh.nBands == 4)
{
for (int i = 0; i < 3; i++)
ptr[i] = (isGray) ? buf[0] : colormap[buf[0]][i];
ptr[3] = 255; // full alpha;
}
else if ((fileBytes == 3 || fileBytes == 4) && sh.nBands == 4)
{
int i;
for (i = 0; i < fileBytes; i++)
ptr[i] = buf[i];
if (i == 3) // missing alpha channel
ptr[3] = 255; // full alpha;
}
else
//.........这里部分代码省略.........