📄 tga.cpp
字号:
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
/*-------------------SPV Card DATA type defination-----------------------*/
#define SPVHUGE
#define SPVLONG int
typedef unsigned SPVLONG USLONG;
typedef unsigned SPVLONG SPVHUGE * LPUSLONG;
typedef signed SPVLONG SLONG;
typedef signed SPVLONG SPVHUGE * LPSLONG;
typedef void SPVHUGE * LPVOIDHUGE;
typedef unsigned short USINT;
typedef unsigned short SPVHUGE * LPUSINT;
typedef signed short SINT;
typedef signed short SPVHUGE * LPSINT;
typedef unsigned char USCHAR;
typedef unsigned char SPVHUGE * LPUSCHAR;
typedef signed char SCHAR;
typedef signed char SPVHUGE * LPSCHAR;
#define VIDEOWIDTH 720
#define VIDEOHEIGHT 576
#define BUF_SIZE 1024*4
#define ON 0
#define OFF 1
#define YES 0
#define NO 1
#define OK 0
#define FAIL 1
#define _32BIT 32
#define _16BIT 16
#define _24BIT 24
#define VARY_COLOR_PIXEL 0
#define MONO_COLOR_PIXEL 1
#define _PX 1
#define _NX -1
#define _PY 1
#define _NY -1
#define SUCCESS 0
#define FORMAT_ERROR 0XFF
char * DataBuf = NULL;
int BufPoint = 0;
#pragma pack (1)
typedef struct tagTGA_HEAD
{
char Id;
char Cmt;
char It;
short Cms1;
short Cms2;
char Cms3;
short XOrigin;
short YOrigin;
short TgaXSize;
short TgaYSize;
char PixelDepth;
char ImgDescriptor;
}TGA_HEAD;
#pragma pack(16)
void ReadCelFIle(HFILE handle, TGA_HEAD* head, char* matxbuf, void * pp,
short xsize, short ysize, short scrnx, short protflag,
long offset, short mflag, short pixelflag, short visibleflag);
void ReadTgaCompressLine(HFILE handle, void * data1, short xsize,
short pixelsize, short* total);
void WriteLine( TGA_HEAD* head, void* data, void * tdata,
char* matxbuf, short* xtable,
short xsize, short protflag, short matxflag,
short xdirection,short pixelflag, short visibleflag);
void TransData24To16(void* data1, void * data, short size);
void TransData24To32(void* data1, void * data, short size);
void TransData32To16(void* data1, void * data, short size);
void TransData16To32(void* data1, void * data, short size);
void Trans16To32(unsigned short* color16, unsigned long* color32p);
void Trans32To16(unsigned long* color32, unsigned short* color16p);
void GetALLScrTable(short xsize, short ysize, short scrx, short scry,
short* xtab, short* ytab );
unsigned int LoadTgaFile_to_Buffer( char* filename, unsigned char * over_buf);
unsigned int LoadTgaFiletoBuffer( char* filename, unsigned int * over_buf);
unsigned int LoadTgaFile( char* filename, short protflag,
short visibleflag);
LPVOID winalloc(long size);
void winfree(LPVOID ptr);
unsigned int Vloadtgafile( char* filename, unsigned int* pp,
short protflag, short visibleflag);
unsigned int ReadTgaFileToBuffer( char* filename, unsigned int * over_buf);
unsigned int Vloadtgafile( char* filename, unsigned int* pp,
short protflag, short visibleflag)
{
TGA_HEAD head;
short xsize, ysize;
HFILE handle;
void *ppp;
int size;
handle=_lopen(filename, OF_READ);
if(handle==-1)
{
_lclose(handle);
return(0);
}
_llseek(handle, 0, SEEK_SET);
_lread(handle, (char*)&head, sizeof(TGA_HEAD));
if(head.It!=2 && head.It!=10)
{
_lclose(handle);
return(FORMAT_ERROR);
}
xsize=head.TgaXSize;
ysize=head.TgaYSize;
size=18L;
ppp=pp+head.YOrigin*VIDEOWIDTH+head.XOrigin;
if(head.Id>0) size+=(long)head.Id;
if(head.Cmt==1) size+=((long)head.Cms2*((long)head.Cms3/8));
ReadCelFIle(handle, &head, NULL, ppp, xsize, ysize,
VIDEOWIDTH, protflag, size, NO, _32BIT, visibleflag);
_lclose(handle);
return(SUCCESS);
}
unsigned int LoadTgaFiletoBuffer( char* filename, unsigned int * over_buf)
{
TGA_HEAD head;
HFILE handle;
short xsize, ysize,x, y;
long size;
void *ppp;
LPUSLONG pp;
pp = over_buf;
if(pp == NULL)
{
MessageBeep(0);
MessageBox(NULL, "No enogh Memory","Error", MB_OK);
return(FAIL);
}
handle=_lopen(filename, O_BINARY|O_RDONLY);
if(handle==-1)
{
_lclose(handle);
return (FAIL);
}
_llseek(handle, 0, SEEK_SET);
_lread(handle, (char*)&head, sizeof(TGA_HEAD));
if(head.It!=2 && head.It!=10)
{
_lclose(handle);
return(FORMAT_ERROR);
}
size=18L;
if(head.Id>0) size+=(long)head.Id;
if(head.Cmt==1) size+=(long)head.Cms2*((long)head.Cms3/8);
xsize=head.TgaXSize;
ysize=head.TgaYSize;
x=(VIDEOWIDTH-xsize)/2;
y=(VIDEOHEIGHT-ysize)/2;
ppp=(void *)(pp+y*VIDEOWIDTH+x);
ReadCelFIle(handle, &head, NULL, ppp, xsize, ysize,
VIDEOWIDTH, NO, size, NO, _32BIT, NO);
_lclose(handle);
return(SUCCESS);
}
unsigned int LoadTgaFile_to_Buffer( char* filename, unsigned char * over_buf)
{
TGA_HEAD head;
HFILE handle;
short xsize, ysize,x, y;
long size;
int i,j;
void *ppp;
LPUSLONG pp;
pp=(LPUSLONG)winalloc(0x400000L);
if(pp == NULL)
{
MessageBeep(0);
MessageBox(NULL, "No enogh Memory","Error", MB_OK);
return(FAIL);
}
handle=_lopen(filename, O_BINARY|O_RDONLY);
if(handle==-1)
{
_lclose(handle);
return(0);
}
_llseek(handle, 0, SEEK_SET);
_lread(handle, (char*)&head, sizeof(TGA_HEAD));
if(head.It!=2 && head.It!=10)
{
_lclose(handle);
return(FORMAT_ERROR);
}
size=18L;
if(head.Id>0) size+=(long)head.Id;
if(head.Cmt==1) size+=(long)head.Cms2*((long)head.Cms3/8);
xsize=head.TgaXSize;
ysize=head.TgaYSize;
x=(VIDEOWIDTH-xsize)/2;
y=(VIDEOHEIGHT-ysize)/2;
ppp=(void *)(pp+y*VIDEOWIDTH+x);
ReadCelFIle(handle, &head, NULL, ppp, xsize, ysize,
VIDEOWIDTH, NO, size, NO, _32BIT, NO);
_lclose(handle);
for(i=0;i<VIDEOHEIGHT;i++)
for(j=0;j<VIDEOWIDTH; j++)
{
over_buf[(long)i*(long)VIDEOWIDTH+(long)j] = (unsigned char)
( ( (unsigned long)pp[(long)i*(long)VIDEOWIDTH+(long)j] )&
0x000000ffL );
}
winfree(pp);
return(SUCCESS);
}
void ReadCelFIle(HFILE handle, TGA_HEAD* head, char* matxbuf, void * pp,
short xsize, short ysize, short scrnx, short protflag,
long offset, short mflag, short pixelflag, short visibleflag)
{
int para, para1;
long wsize;
char * tmp_pp;
unsigned char *data, *tdata, * data1;
char *matx;
short *xtable, *ytable, xdirection, kk, j, linetotal;
int i, yinc, matxxsize;
para=pixelflag/8;
para1= head->PixelDepth/8;
data=(unsigned char *)winalloc(head->TgaXSize*para);
if(head->TgaXSize>256) data1=(unsigned char *)winalloc(head->TgaXSize*para1*2);
else data1=(unsigned char *)winalloc(256*para1*2);
xtable=(short*)winalloc(xsize*2);
ytable=(short*)winalloc(ysize*2);
GetALLScrTable(head->TgaXSize, head->TgaYSize, xsize, ysize, xtable, ytable);
matx=matxbuf;
if(head->TgaXSize%8==0) matxxsize=head->TgaXSize/8;
else matxxsize=head->TgaXSize/8+1;
tmp_pp = (char *)pp;
if((head->ImgDescriptor&0x20)==0)
{
tdata=(unsigned char *)(&tmp_pp[((unsigned long)ysize-1L)*
(unsigned long)para*
(unsigned long)scrnx
]);
yinc=-para*scrnx;
matxxsize=-matxxsize;
}
else
{
tdata=(unsigned char *)tmp_pp;
yinc=para*scrnx;
}
if((head->ImgDescriptor&0x10)==0) xdirection=_PX;
else xdirection=_NX;
kk=-1;
j=0;
_llseek(handle, offset, SEEK_SET);
linetotal=0;
DataBuf=(char*)winalloc(BUF_SIZE);
BufPoint=-1;
for(i=0; i<ysize; i++)
{
switch(head->It)
{
case 10:
if(kk!=ytable[i])
{
while(j<=ytable[i])
{
ReadTgaCompressLine( handle, data1,
head->TgaXSize,
head->PixelDepth,
&linetotal);
j++;
}
}
break;
case 2:
if(kk!=ytable[i])
{
_llseek(handle, offset+(unsigned long)(
(unsigned long)head->TgaXSize*
(unsigned long)ytable[i]*
(unsigned long)para1 ),
SEEK_SET);
wsize=_lread(handle, data1, head->TgaXSize*para1);
if(wsize != head->TgaXSize*para1)
MessageBeep(0);
}
break;
default:
break;
}
if(mflag==YES)
{
if(matxxsize>0)
matx=matxbuf+ytable[i]*matxxsize;
else
matx=matxbuf+((ysize-1)-ytable[i])*(-matxxsize);
}
if(pixelflag==head->PixelDepth)
{
if(kk!=ytable[i]) kk=ytable[i];
WriteLine(head, data1, tdata, matx, xtable, xsize, protflag,
mflag, xdirection, pixelflag, visibleflag);
}
else
{
if(kk!=ytable[i])
{
kk=ytable[i];
if(head->PixelDepth==_16BIT && pixelflag==_32BIT)
TransData16To32(data1, data, head->TgaXSize);
if(head->PixelDepth==_32BIT && pixelflag==_16BIT)
TransData32To16(data1, data, head->TgaXSize);
if(head->PixelDepth==_24BIT && pixelflag==_16BIT)
TransData24To16(data1, data, head->TgaXSize);
if(head->PixelDepth==_24BIT && pixelflag==_32BIT)
TransData24To32(data1, data, head->TgaXSize);
}
WriteLine(head, data, tdata, matx, xtable, xsize, protflag,
mflag, xdirection, pixelflag, visibleflag);
}
tdata+=yinc;
}
winfree(DataBuf);
DataBuf=NULL;
BufPoint=-1;
winfree(data);
winfree(data1);
winfree(xtable);
winfree(ytable);
return;
}
void ReadTgaCompressLine(HFILE handle, void * data1, short xsize,
short pixelsize, short* total)
{
unsigned int i, j, k, tt;
unsigned char cc1, cc;
unsigned int para, para1, len;
char temp[4], *data;
para=pixelsize/8;
i=0;
if(*total>xsize)
{
*total-=xsize;
memcpy((char*)data1, &((char*)data1)[xsize*para], *total*para);
if(*total>=xsize) return;
i=*total;
}
data=(char*)data1;
if(BufPoint<0)
{
_lread(handle, DataBuf, BUF_SIZE);
BufPoint=0;
}
do
{
cc=(unsigned char)DataBuf[BufPoint];
cc1=(cc&0x7f);
BufPoint++;
if(BufPoint>=BUF_SIZE)
{
_lread(handle, DataBuf, BUF_SIZE);
BufPoint=0;
}
len=1+(int)(cc1);
if((cc&0x80)!=0)
{
for(j=0; j<para; j++)
{
temp[j]=DataBuf[BufPoint];
BufPoint++;
if(BufPoint>=BUF_SIZE)
{
_lread(handle, DataBuf, BUF_SIZE);
BufPoint=0;
}
}
for(j=0; j<len; j++)
{
para1=(i+j)*para;
for(k=0; k<para; k++) data[para1+k]=temp[k];
}
}
else
{
para1=len*para;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -