Skip to content

Commit

Permalink
Update TEXConv.
Browse files Browse the repository at this point in the history
  • Loading branch information
tx00100xt committed Dec 26, 2023
1 parent 319c03e commit 13f0bbf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
28 changes: 19 additions & 9 deletions SamTFE/Sources/TEXConv/TEXConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// Engine/Base/ErrorReporting.o
HWND _hwndMain = NULL;

#ifdef PLATFORM_UNIX
// retrives memory offset of a specified mip-map or a size of all mip-maps (IN PIXELS!)
// (zero offset means first, i.e. largest mip-map)
static PIX _GetMipmapOffset(INDEX iMipLevel, PIX pixWidth, PIX pixHeight)
extern PIX GetMipmapOffset(INDEX iMipLevel, PIX pixWidth, PIX pixHeight);
// adds 8-bit opaque alpha channel to 24-bit bitmap (in place supported)
extern void AddAlphaChannel(UBYTE *pubSrcBitmap, ULONG *pulDstBitmap, PIX pixSize, UBYTE *pubAlphaBitmap);
// removes 8-bit alpha channel from 32-bit bitmap (in place supported)
extern void RemoveAlphaChannel(ULONG *pulSrcBitmap, UBYTE *pubDstBitmap, PIX pixSize);
#else
// retrives memory offset of a specified mip-map or a size of all mip-maps (IN PIXELS!)
// (zero offset means first, i.e. largest mip-map)
static PIX GetMipmapOffset(INDEX iMipLevel, PIX pixWidth, PIX pixHeight)
{
PIX pixTexSize = 0;
PIX pixMipSize = pixWidth*pixHeight;
Expand All @@ -51,7 +60,7 @@ static PIX _GetMipmapOffset(INDEX iMipLevel, PIX pixWidth, PIX pixHeight)
}

// adds 8-bit opaque alpha channel to 24-bit bitmap (in place supported)
static void _AddAlphaChannel(UBYTE *pubSrcBitmap, ULONG *pulDstBitmap, PIX pixSize, UBYTE *pubAlphaBitmap)
static void AddAlphaChannel(UBYTE *pubSrcBitmap, ULONG *pulDstBitmap, PIX pixSize, UBYTE *pubAlphaBitmap)
{
UBYTE ubR, ubG, ubB, ubA = 255;
// loop backwards thru all bitmap pixels
Expand All @@ -66,7 +75,7 @@ static void _AddAlphaChannel(UBYTE *pubSrcBitmap, ULONG *pulDstBitmap, PIX pixSi
}

// removes 8-bit alpha channel from 32-bit bitmap (in place supported)
static void _RemoveAlphaChannel(ULONG *pulSrcBitmap, UBYTE *pubDstBitmap, PIX pixSize)
static void RemoveAlphaChannel(ULONG *pulSrcBitmap, UBYTE *pubDstBitmap, PIX pixSize)
{
UBYTE ubR, ubG, ubB;
// loop thru all bitmap pixels
Expand All @@ -77,8 +86,9 @@ static void _RemoveAlphaChannel(ULONG *pulSrcBitmap, UBYTE *pubDstBitmap, PIX pi
pubDstBitmap[iPix * 3 + 2] = ubB;
}
}
#endif
// Convert old texture format (3) to format (4)
static void _Convert( CTextureData *pTD)
static void Convert( CTextureData *pTD)
{
// skip effect textures
if( pTD->td_ptegEffect != NULL) return;
Expand All @@ -87,7 +97,7 @@ static void _Convert( CTextureData *pTD)
PIX pixWidth = pTD->GetPixWidth();
PIX pixHeight = pTD->GetPixHeight();
PIX pixMipSize = pixWidth * pixHeight;
PIX pixFrameSize = _GetMipmapOffset( 15, pixWidth, pixHeight);
PIX pixFrameSize = GetMipmapOffset( 15, pixWidth, pixHeight);
// allocate memory for new texture
ULONG *pulFramesNew = (ULONG*)AllocMemory( pixFrameSize*pTD->td_ctFrames *BYTES_PER_TEXEL);
UWORD *puwFramesOld = (UWORD*)pTD->td_pulFrames;
Expand Down Expand Up @@ -229,7 +239,7 @@ void SubMain( int argc, char *argv[])
pTD->td_ulFlags |= ulFlags;
bAlphaChannel = pTD->td_ulFlags&TEX_ALPHACHANNEL;
// determine frame size
if( iVersion==4) pTD->td_slFrameSize = _GetMipmapOffset( 15, pTD->GetPixWidth(), pTD->GetPixHeight())
if( iVersion==4) pTD->td_slFrameSize = GetMipmapOffset( 15, pTD->GetPixWidth(), pTD->GetPixHeight())
* BYTES_PER_TEXEL;
} // TDAT

Expand Down Expand Up @@ -266,7 +276,7 @@ void SubMain( int argc, char *argv[])
// read texture without alpha channel from file
TEXFile.Read_t( pulCurrentFrame, pixFrameSizeOnDisk *3);
// add opaque alpha channel
_AddAlphaChannel( (UBYTE*)pulCurrentFrame, pulCurrentFrame, pixFrameSizeOnDisk);
AddAlphaChannel( (UBYTE*)pulCurrentFrame, pulCurrentFrame, pixFrameSizeOnDisk);
}
}
}
Expand All @@ -276,7 +286,7 @@ void SubMain( int argc, char *argv[])
while( !TEXFile.AtEOF());

// if texture is in old format, convert it to current format
if( iVersion==3) _Convert(pTD);
if( iVersion==3) Convert(pTD);
printf("Width: %d Height: %d\n", pTD->GetPixHeight(), pTD->GetPixWidth());

// Croteam internal image format
Expand All @@ -302,7 +312,7 @@ void SubMain( int argc, char *argv[])
if( pTD->td_ulFlags&TEX_ALPHACHANNEL) {
memcpy( ii.ii_Picture, pulFrame, slMipSize);
} else {
_RemoveAlphaChannel( pulFrame, ii.ii_Picture, pixMipSize);
RemoveAlphaChannel( pulFrame, ii.ii_Picture, pixMipSize);
}

// save tga
Expand Down
28 changes: 19 additions & 9 deletions SamTSE/Sources/TEXConv/TEXConv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// Engine/Base/ErrorReporting.o
HWND _hwndMain = NULL;

#ifdef PLATFORM_UNIX
// retrives memory offset of a specified mip-map or a size of all mip-maps (IN PIXELS!)
// (zero offset means first, i.e. largest mip-map)
static PIX _GetMipmapOffset(INDEX iMipLevel, PIX pixWidth, PIX pixHeight)
extern PIX GetMipmapOffset(INDEX iMipLevel, PIX pixWidth, PIX pixHeight);
// adds 8-bit opaque alpha channel to 24-bit bitmap (in place supported)
extern void AddAlphaChannel(UBYTE *pubSrcBitmap, ULONG *pulDstBitmap, PIX pixSize, UBYTE *pubAlphaBitmap);
// removes 8-bit alpha channel from 32-bit bitmap (in place supported)
extern void RemoveAlphaChannel(ULONG *pulSrcBitmap, UBYTE *pubDstBitmap, PIX pixSize);
#else
// retrives memory offset of a specified mip-map or a size of all mip-maps (IN PIXELS!)
// (zero offset means first, i.e. largest mip-map)
static PIX GetMipmapOffset(INDEX iMipLevel, PIX pixWidth, PIX pixHeight)
{
PIX pixTexSize = 0;
PIX pixMipSize = pixWidth*pixHeight;
Expand All @@ -51,7 +60,7 @@ static PIX _GetMipmapOffset(INDEX iMipLevel, PIX pixWidth, PIX pixHeight)
}

// adds 8-bit opaque alpha channel to 24-bit bitmap (in place supported)
static void _AddAlphaChannel(UBYTE *pubSrcBitmap, ULONG *pulDstBitmap, PIX pixSize, UBYTE *pubAlphaBitmap)
static void AddAlphaChannel(UBYTE *pubSrcBitmap, ULONG *pulDstBitmap, PIX pixSize, UBYTE *pubAlphaBitmap)
{
UBYTE ubR, ubG, ubB, ubA = 255;
// loop backwards thru all bitmap pixels
Expand All @@ -66,7 +75,7 @@ static void _AddAlphaChannel(UBYTE *pubSrcBitmap, ULONG *pulDstBitmap, PIX pixSi
}

// removes 8-bit alpha channel from 32-bit bitmap (in place supported)
static void _RemoveAlphaChannel(ULONG *pulSrcBitmap, UBYTE *pubDstBitmap, PIX pixSize)
static void RemoveAlphaChannel(ULONG *pulSrcBitmap, UBYTE *pubDstBitmap, PIX pixSize)
{
UBYTE ubR, ubG, ubB;
// loop thru all bitmap pixels
Expand All @@ -77,8 +86,9 @@ static void _RemoveAlphaChannel(ULONG *pulSrcBitmap, UBYTE *pubDstBitmap, PIX pi
pubDstBitmap[iPix * 3 + 2] = ubB;
}
}
#endif
// Convert old texture format (3) to format (4)
static void _Convert( CTextureData *pTD)
static void Convert( CTextureData *pTD)
{
// skip effect textures
if( pTD->td_ptegEffect != NULL) return;
Expand All @@ -87,7 +97,7 @@ static void _Convert( CTextureData *pTD)
PIX pixWidth = pTD->GetPixWidth();
PIX pixHeight = pTD->GetPixHeight();
PIX pixMipSize = pixWidth * pixHeight;
PIX pixFrameSize = _GetMipmapOffset( 15, pixWidth, pixHeight);
PIX pixFrameSize = GetMipmapOffset( 15, pixWidth, pixHeight);
// allocate memory for new texture
ULONG *pulFramesNew = (ULONG*)AllocMemory( pixFrameSize*pTD->td_ctFrames *BYTES_PER_TEXEL);
UWORD *puwFramesOld = (UWORD*)pTD->td_pulFrames;
Expand Down Expand Up @@ -229,7 +239,7 @@ void SubMain( int argc, char *argv[])
pTD->td_ulFlags |= ulFlags;
bAlphaChannel = pTD->td_ulFlags&TEX_ALPHACHANNEL;
// determine frame size
if( iVersion==4) pTD->td_slFrameSize = _GetMipmapOffset( 15, pTD->GetPixWidth(), pTD->GetPixHeight())
if( iVersion==4) pTD->td_slFrameSize = GetMipmapOffset( 15, pTD->GetPixWidth(), pTD->GetPixHeight())
* BYTES_PER_TEXEL;
} // TDAT

Expand Down Expand Up @@ -266,7 +276,7 @@ void SubMain( int argc, char *argv[])
// read texture without alpha channel from file
TEXFile.Read_t( pulCurrentFrame, pixFrameSizeOnDisk *3);
// add opaque alpha channel
_AddAlphaChannel( (UBYTE*)pulCurrentFrame, pulCurrentFrame, pixFrameSizeOnDisk);
AddAlphaChannel( (UBYTE*)pulCurrentFrame, pulCurrentFrame, pixFrameSizeOnDisk);
}
}
}
Expand All @@ -276,7 +286,7 @@ void SubMain( int argc, char *argv[])
while( !TEXFile.AtEOF());

// if texture is in old format, convert it to current format
if( iVersion==3) _Convert(pTD);
if( iVersion==3) Convert(pTD);
printf("Width: %d Height: %d\n", pTD->GetPixHeight(), pTD->GetPixWidth());

// Croteam internal image format
Expand All @@ -302,7 +312,7 @@ void SubMain( int argc, char *argv[])
if( pTD->td_ulFlags&TEX_ALPHACHANNEL) {
memcpy( ii.ii_Picture, pulFrame, slMipSize);
} else {
_RemoveAlphaChannel( pulFrame, ii.ii_Picture, pixMipSize);
RemoveAlphaChannel( pulFrame, ii.ii_Picture, pixMipSize);
}

// save tga
Expand Down

0 comments on commit 13f0bbf

Please sign in to comment.