📄 texture.cpp
字号:
#include "Stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "texture.h"
#include "windowsx.h"
CTexture::CTexture()
{
rbuf=NULL;
gbuf=NULL;
bbuf=NULL;
abuf=NULL;
base=NULL;
image=NULL;
}
CTexture::~CTexture()
{
ImageClose();
delete []rbuf; rbuf=NULL;
delete []gbuf; gbuf=NULL;
delete []bbuf; bbuf=NULL;
delete []abuf; abuf=NULL;
if( base ) GlobalFreePtr(base); base=NULL;
}
void CTexture::bwtorgba(unsigned char *b,unsigned char *l,int n)
{
while(n--){
l[0] = *b;
l[1] = *b;
l[2] = *b;
l[3] = 0xff;
l += 4; b++;
}
}
void CTexture::latorgba(unsigned char *b, unsigned char *a,unsigned char *l,int n)
{
while(n--) {
l[0] = *b;
l[1] = *b;
l[2] = *b;
l[3] = *a;
l += 4; b++; a++;
}
}
void CTexture::rgbtorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *l,int n)
{
while(n--) {
l[0] = r[0];
l[1] = g[0];
l[2] = b[0];
l[3] = 0xff;
l += 4; r++; g++; b++;
}
}
void CTexture::rgbtorgb(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *l,int n)
{
while(n--) {
l[0] = r[0];
l[1] = g[0];
l[2] = b[0];
l += 3; r++; g++; b++;
}
}
void CTexture::rgbatorgba(unsigned char *r,unsigned char *g,unsigned char *b,unsigned char *a,unsigned char *l,int n)
{
while(n--) {
l[0] = r[0];
l[1] = g[0];
l[2] = b[0];
l[3] = a[0];
l += 4; r++; g++; b++; a++;
}
}
void CTexture::ConvertShort(unsigned short *array, long length)
{
unsigned b1, b2;
unsigned char *ptr;
ptr = (unsigned char *)array;
while (length--) {
b1 = *ptr++;
b2 = *ptr++;
*array++ = (b1 << 8) | (b2);
}
}
void CTexture::ConvertLong(unsigned *array, long length)
{
unsigned b1, b2, b3, b4;
unsigned char *ptr;
ptr = (unsigned char *)array;
while (length--) {
b1 = *ptr++;
b2 = *ptr++;
b3 = *ptr++;
b4 = *ptr++;
*array++ = (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
}
}
ImageRec *CTexture::ImageOpen(const char *fileName)
{
union
{
int testWord;
char testByte[4];
} endianTest;
ImageRec *image;
int swapFlag;
int x;
endianTest.testWord = 1;
if (endianTest.testByte[0] == 1)
swapFlag = 1;
else
swapFlag = 0;
image = new ImageRec;
if (image == NULL)
{
MessageBox(NULL,"Out of memory!", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
if ((image->file = fopen(fileName, "rb")) == NULL)
{
char msgBuff[300];
sprintf(msgBuff,"can not open file:%s",fileName);
MessageBox(NULL,msgBuff, "Error", MB_OK | MB_ICONERROR);
return NULL;
exit(1);
}
fread(image, 1, 12, image->file);
if (swapFlag)
ConvertShort(&image->imagic, 6);
image->tmp = new unsigned char[image->xsize*256];
if(!image->tmp)
{
MessageBox(NULL,"Out of memory!", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
if ((image->type & 0xFF00) == 0x0100)
{
x = image->ysize * image->zsize * sizeof(unsigned);
image->rowStart = (unsigned *)malloc(x);
image->rowSize = (int *)malloc(x);
if (image->rowStart == NULL || image->rowSize == NULL)
{
MessageBox(NULL,"Out of memory!", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
image->rleEnd = 512 + (2 * x);
fseek(image->file, 512, SEEK_SET);
fread(image->rowStart, 1, x, image->file);
fread(image->rowSize, 1, x, image->file);
if (swapFlag)
{
ConvertLong(image->rowStart, x/sizeof(unsigned));
ConvertLong((unsigned *)image->rowSize, x/sizeof(int));
}
}
return image;
}
void CTexture::ImageClose()
{
if( image )
{
if( image->file ) fclose(image->file);
if( image && image->tmp ) { delete []image->tmp; image->tmp=NULL; }
delete image; image=NULL;
}
}
void CTexture::ImageGetRow(unsigned char *buf, int y, int z)
{
ASSERT( z<image->zsize );
unsigned char *iPtr, *oPtr, pixel;
int count;
int pp=0;
if ((image->type & 0xFF00) == 0x0100)
{
fseek(image->file, image->rowStart[y+z*image->ysize], SEEK_SET);
fread(image->tmp, 1, (unsigned int)image->rowSize[y+z*image->ysize],image->file);
iPtr = image->tmp;
oPtr = buf;
while (1)
{
pixel = *iPtr++;
count = (int)(pixel & 0x7F);
if (!count) return;
if (pixel & 0x80)
{
while (count--)
{
pp++;
if( pp>image->xsize ) return;
ASSERT( pp<=image->xsize );
*oPtr++ = *iPtr++;
}
}
else
{
pixel = *iPtr++;
while (count--)
{
pp++;
if( pp>image->xsize ) return;
ASSERT( pp<=image->xsize );
*oPtr++ = pixel;
}
}
}
}
else
{
fseek(image->file, 512+(y*image->xsize)+(z*image->xsize*image->ysize),
SEEK_SET);
fread(buf, 1, image->xsize, image->file);
}
}
unsigned char* CTexture::read_texture(char *name, int *width, int *height, int *components)
{
ImageClose();
delete []rbuf; rbuf=NULL;
delete []gbuf; gbuf=NULL;
delete []bbuf; bbuf=NULL;
delete []abuf; abuf=NULL;
if( base ) GlobalFreePtr(base); base=NULL;
unsigned char *lptr;
int y;
image = ImageOpen(name);
if(!image) return NULL;
(*width)=image->xsize;
(*height)=image->ysize;
(*components)=image->zsize;
if(image->zsize==3)
base=(unsigned char*)GlobalAllocPtr(GMEM_MOVEABLE,image->xsize*image->ysize*3*sizeof(unsigned char));
else
{
(*components)=4;
base=(unsigned char*)GlobalAllocPtr(GMEM_MOVEABLE,image->xsize*image->ysize*4*sizeof(unsigned char));
}
rbuf = new unsigned char[image->xsize];
gbuf = new unsigned char[image->xsize];
bbuf = new unsigned char[image->xsize];
abuf = new unsigned char[image->xsize];
if(!base || !rbuf || !gbuf || !bbuf || !abuf)
{
MessageBox(NULL,"out of memory" ,"Error", MB_OK | MB_ICONERROR);
return NULL;
}
lptr = base;
for(y=0; y<image->ysize; y++)
{
if(image->zsize>4)
{
MessageBox(NULL,"component is not greater than 4" ,"Error", MB_OK | MB_ICONERROR);
exit(1);
}
else if(image->zsize==4)
{
ImageGetRow(rbuf,y,0);
ImageGetRow(gbuf,y,1);
ImageGetRow(bbuf,y,2);
ImageGetRow(abuf,y,3);
rgbatorgba(rbuf,gbuf,bbuf,abuf,(unsigned char *)lptr,image->xsize);
lptr += image->xsize*4;
}
else if(image->zsize==3)
{
ImageGetRow(rbuf,y,0);
ImageGetRow(gbuf,y,1);
ImageGetRow(bbuf,y,2);
rgbtorgb(rbuf,gbuf,bbuf,(unsigned char *)lptr,image->xsize);
lptr += image->xsize*3;
}
else if(image->zsize==2)
{
ImageGetRow(rbuf,y,0);
ImageGetRow(abuf,y,1);
latorgba(rbuf,abuf,(unsigned char *)lptr,image->xsize);
lptr += image->xsize*4;
}
else
{
ImageGetRow(rbuf,y,0);
bwtorgba(rbuf,(unsigned char *)lptr,image->xsize);
lptr += image->xsize*4;
}
}
return (unsigned char*) base;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -