@@ -21,12 +21,12 @@ bool g_originalTransparencyKey = true;
2121
2222XYPAIR g_permsList[16 ];
2323XYPAIR g_specList[16 ];
24- texdata_t * g_pageDatas = NULL ;
24+ texdata_t * g_texPageData = NULL ;
2525TexPage_t* g_texPages = NULL ;
2626extclutdata_t * g_extraPalettes = NULL ;
2727int g_numExtraPalettes = 0 ;
2828
29- // legacy format converter
29+ // 16 bit color to BGRA
3030TVec4D<ubyte> bgr5a1_ToRGBA8 (ushort color)
3131{
3232 ubyte b = (color & 0x1F ) * 8 ;
@@ -44,6 +44,77 @@ TVec4D<ubyte> bgr5a1_ToRGBA8(ushort color)
4444 return TVec4D<ubyte>(r,g,b,a);
4545}
4646
47+ // 16 bit color to RGBA
48+ TVec4D<ubyte> rgb5a1_ToRGBA8 (ushort color)
49+ {
50+ ubyte r = (color & 0x1F ) * 8 ;
51+ ubyte g = ((color >> 5 ) & 0x1F ) * 8 ;
52+ ubyte b = ((color >> 10 ) & 0x1F ) * 8 ;
53+
54+ ubyte a = (color >> 15 );
55+
56+ // restore source transparency key
57+ if (g_originalTransparencyKey && color == 0 )
58+ {
59+ return TVec4D<ubyte>(255 , 0 , 255 , 0 );
60+ }
61+
62+ return TVec4D<ubyte>(r, g, b, a);
63+ }
64+
65+ // -------------------------------------------------------------
66+ // Conversion of indexed palettized texture to 32bit RGBA
67+ // -------------------------------------------------------------
68+ void ConvertIndexedTextureToRGBA (int nPage, uint* dest_color_data, int detail, TEXCLUT* clut, bool outputBGR)
69+ {
70+ texdata_t * page = &g_texPageData[nPage];
71+
72+ if (!(detail < g_texPages[nPage].numDetails ))
73+ {
74+ MsgError (" Cannot apply palette to non-existent detail! Programmer error?\n " );
75+ return ;
76+ }
77+
78+ int ox = g_texPages[nPage].details [detail].x ;
79+ int oy = g_texPages[nPage].details [detail].y ;
80+ int w = g_texPages[nPage].details [detail].width ;
81+ int h = g_texPages[nPage].details [detail].height ;
82+
83+ if (w == 0 )
84+ w = 256 ;
85+
86+ if (h == 0 )
87+ h = 256 ;
88+
89+ char * textureName = g_textureNamesData + g_texPages[nPage].details [detail].nameoffset ;
90+ // MsgWarning("Applying detail %d '%s' (xywh: %d %d %d %d)\n", detail, textureName, ox, oy, w, h);
91+
92+ int tp_wx = ox + w;
93+ int tp_hy = oy + h;
94+
95+ for (int y = oy; y < tp_hy; y++)
96+ {
97+ for (int x = ox; x < tp_wx; x++)
98+ {
99+ ubyte clindex = page->data [y * 128 + x / 2 ];
100+
101+ if (0 != (x & 1 ))
102+ clindex >>= 4 ;
103+
104+ clindex &= 0xF ;
105+
106+ TVec4D<ubyte> color = outputBGR ?
107+ bgr5a1_ToRGBA8 (clut->colors [clindex]) :
108+ rgb5a1_ToRGBA8 (clut->colors [clindex]);
109+
110+ // flip texture by Y because of TGA
111+ int ypos = (TEXPAGE_SIZE_Y - y - 1 ) * TEXPAGE_SIZE_Y;
112+
113+ dest_color_data[ypos + x] = *(uint*)(&color);
114+ }
115+ }
116+ }
117+
47118// -------------------------------------------------------------------------------
48119
49120// unpacks texture, returns new source pointer
@@ -168,8 +239,8 @@ void LoadPermanentTPages(IVirtualStream* pFile)
168239 pFile->Seek ( g_levInfo.texdata_offset , VS_SEEK_SET );
169240
170241 // allocate page data
171- g_pageDatas = new texdata_t [g_numTexPages];
172- memset (g_pageDatas , 0 , sizeof (texdata_t )*g_numTexPages);
242+ g_texPageData = new texdata_t [g_numTexPages];
243+ memset (g_texPageData , 0 , sizeof (texdata_t )*g_numTexPages);
173244
174245 // -----------------------------------
175246
@@ -190,7 +261,7 @@ void LoadPermanentTPages(IVirtualStream* pFile)
190261 int tpage = g_permsList[i].x ;
191262
192263 // permanents are also compressed
193- LoadTPageAndCluts (pFile, &g_pageDatas [tpage], tpage, true );
264+ LoadTPageAndCluts (pFile, &g_texPageData [tpage], tpage, true );
194265
195266 pFile->Seek (curOfs + ((g_permsList[i].y + 2047 ) & -2048 ), VS_SEEK_SET);
196267 }
@@ -208,9 +279,9 @@ void LoadPermanentTPages(IVirtualStream* pFile)
208279 {
209280 long curOfs = pFile->Tell ();
210281 int tpage = g_specList[i].x ;
211-
282+
212283 // permanents are compressed
213- LoadTPageAndCluts (pFile, &g_pageDatas [tpage], tpage, true );
284+ LoadTPageAndCluts (pFile, &g_texPageData [tpage], tpage, true );
214285
215286 pFile->Seek (curOfs + ((g_specList[i].y + 2047 ) & -2048 ), VS_SEEK_SET);
216287 }
0 commit comments