Skip to content

Commit

Permalink
Fixed crash when trying to replace image on some TEXB
Browse files Browse the repository at this point in the history
Fixed image destruction on some TEXB
  • Loading branch information
MikuAuahDark committed Nov 27, 2016
1 parent b0d073f commit 9ad393a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ sln/*
!sln/Itsudemo.sln
!sln/Itsudemo.vcxproj
!sln/Itsudemo.vcxproj.filters
libs/
obj/
10 changes: 6 additions & 4 deletions Info.rc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#define ITSUDEMO_VERSION "1.0.6"

#ifdef RC_INVOKED

1 VERSIONINFO
FILEVERSION 1,0,5,0
PRODUCTVERSION 1,0,5,0
FILEVERSION 1,0,6,0
PRODUCTVERSION 1,0,6,0
FILEFLAGSMASK 0x17L
FILEOS 0x40004L
FILETYPE 0x1L
Expand All @@ -14,12 +16,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "Dark Energy Processor Corporation"
VALUE "FileDescription", "TEXB Manipulation Tool"
VALUE "FileVersion", "1.0.5.0"
VALUE "FileVersion", "1.0.6.0"
VALUE "InternalName", "Itsudemo.exe"
VALUE "LegalCopyright", "Copyright � 2037 Dark Energy Processor Corporation"
VALUE "OriginalFilename", "Itsudemo.exe"
VALUE "ProductName", "Itsudemo"
VALUE "ProductVersion", "1.0.5.0"
VALUE "ProductVersion", "1.0.6.0"
END
END
BLOCK "VarFileInfo"
Expand Down
65 changes: 65 additions & 0 deletions TEXB_stream_format.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--------------------------------------------------------------------------
TEXB File format

TEXB file format describe a texture.

Texture can embbed multiple "images" which are not only rectangular but
is defined as a triangle soup.

2011/12/06 V0.1 : First Draft
2011/12/09 V0.15: Added information about "Type" field, compressed texture is not complete still.
2012/01/12 V0.2 : Updated information about C String format for ressource name
2012/01/26 V0.3 : Added member width, height into image asset
--------------------------------------------------------------------------
[TEXB](u32)
Size (u32)
String Length (u16)
Length include 0 and padding alignment
C String (0 included, + padding align 2)
T + full resource path name
example : Tpackage.package2.filename.texb

Width (u16)
Height (u16)
Type (u16)
Bit 0..2 : [0] ALPHA | [1] LUMA | [2] LUMALPHA | [3] RGB | [4] RGBA
Bit 3 : [0] False, [1] True --> Compressed
Bit 4 : [0] False, [1] True --> Mipmap
Bit 5 : [0] False, [1] True --> Double Buffered
Bit 6..7 : Pixel Format
0 : 565 RGB
1 : 5551 RGBA
2 : 4444 RGBA
3 : Byte

TODO RP : Add Bit definition for compressed texture format type (ETC1 variation, etc...)
Can reuse pixel format bits if necessary

TotalVertexCount (u16)
TotalIndexCount (u16)
ImageCount (u16)

(multiply by Image Count)
[TIMG] (u32)
Size (u16)
String Length (u16)
Length include 0 and padding alignment
C String (0 included, + padding align 2)
I + full resource path name
Ipackage.package2.filenameB.imag
VertexCount (u8)
IndexCount (u8)
Width (u16)
Height (u16)
CenterX (u16)
CenterY (u16)

(multiply by VertexCount)
X (u32 fixed 16.16)
Y (u32 fixed 16.16)

(multiply by IndexCount)
Index (u8)

[... Texture Bitmap Data...]
------------------------------------------------------------------------
3 changes: 2 additions & 1 deletion src/Itsudemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "TEXB.h"
#include "CompilerName.h"
#include "../Info.rc"

#include <iostream>
#include <string>
Expand Down Expand Up @@ -204,7 +205,7 @@ int main(int argc,char* argv[])
std::cerr << " Itsudemo. TEXB Manipulation tool\n\n Copyright (c) 2037 Dark Energy Processor Corporation\n" << std::endl;
return 0;
}
std::string VersionString("1.0.5\nCopyright (c) 2037 Dark Energy Processor Corporation\nCompiled with ");
std::string VersionString(ITSUDEMO_VERSION "\nCopyright (c) 2037 Dark Energy Processor Corporation\nCompiled with ");
VersionString.append(CompilerName());
#ifdef _DEBUG
VersionString.append(" (debug)");
Expand Down
28 changes: 13 additions & 15 deletions src/TEXBLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ TextureBank* TextureBank::FromMemory(uint8_t* _mem,size_t _n)
else
{
// Upps, I don't know the kind of the compression.
FailAndExit: // Visual Studio & GCC supports labels
FailAndExit:
for(std::vector<TextureImage*>::iterator k=imageListTemp.begin();k!=imageListTemp.end();k++)
LIBTEXB_FREE((*k));
LIBTEXB_FREE(texb);
Expand All @@ -253,7 +253,6 @@ TextureBank* TextureBank::FromMemory(uint8_t* _mem,size_t _n)
texb->ImageList_Id=imageListTemp;
LIBTEXB_FREE(rawData);

//for(std::vector<TextureImage*>::iterator i=imageListTemp.begin();i!=imageListTemp.end();i++)
for(uint32_t i=0;i<texb->ImageList_Id.size();i++)
{
TextureImage* timg=texb->ImageList_Id[i];
Expand All @@ -278,25 +277,24 @@ TextureBank* TextureBank::FromMemory(uint8_t* _mem,size_t _n)
// Check UV
for(uint32_t j=0;j<4;j++)
{
if(t[j].U>1.0) t[j].U=1;
if(t[j].V>1.0) t[j].V=1;
if(t[j].U>1.0) t[j].U = 1.0;
if(t[j].V>1.0) t[j].V = 1.0;
}

// Check width & height mismatch.
if(v[2].X-v[0].X>timg->Width)
timg->Width=v[2].X-v[0].X;
if(v[2].Y-v[0].Y>timg->Height)
timg->Height=v[2].Y-v[0].Y;
uint32_t min_x = std::min(v[0].X, std::min(v[1].X, std::min(v[2].X, v[3].X)));
uint32_t min_y = std::min(v[0].Y, std::min(v[1].Y, std::min(v[2].Y, v[3].Y)));
uint32_t max_x = std::max(v[0].X, std::max(v[1].X, std::max(v[2].X, v[3].X)));
uint32_t max_y = std::max(v[0].Y, std::max(v[1].Y, std::max(v[2].Y, v[3].Y)));

rawBmp=LIBTEXB_ALLOC(uint32_t,timg->Width*timg->Height);
rawBmp=LIBTEXB_ALLOC(uint32_t, timg->Width * timg->Height);

memset(rawBmp,0,timg->Width*timg->Height*4);
for(uint32_t y=v[0].Y;y<v[2].Y;y++)
memset(rawBmp, 0, timg->Width * timg->Height * 4);
for(uint32_t y=min_y; y < max_y; y++)
{
for(uint32_t x=v[0].X;x<v[2].X;x++)
for(uint32_t x = min_x; x < max_x; x++)
{
UVPoint uv=xy2uv(x,y,v[0],v[1],v[2],v[3],t[0],t[1],t[2],t[3]);
rawBmp[x+y*v[2].X]=texbBmp[uint32_t(uv.U*tWidth+0.5)+uint32_t(uv.V*tHeight+0.5)*tWidth];
UVPoint uv=xy2uv(x, y, v[0], v[1], v[2], v[3], t[0], t[1], t[2], t[3]);
rawBmp[x + y * timg->Width] = texbBmp[uint32_t(uv.U * tWidth + 0.5) + uint32_t(uv.V * tHeight + 0.5) * tWidth];
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/TEXBModify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,17 @@ void TextureBank::ReflectChanges()
{Vrtx[10]/65536.0,Vrtx[11]/65536.0},
{Vrtx[14]/65536.0,Vrtx[15]/65536.0}
};
uint32_t min_x = std::min(v[0].X, std::min(v[1].X, std::min(v[2].X, v[3].X)));
uint32_t min_y = std::min(v[0].Y, std::min(v[1].Y, std::min(v[2].Y, v[3].Y)));
uint32_t max_x = std::max(v[0].X, std::max(v[1].X, std::max(v[2].X, v[3].X)));
uint32_t max_y = std::max(v[0].Y, std::max(v[1].Y, std::max(v[2].Y, v[3].Y)));

for(uint32_t y=0;y<timg->Height;y++)
for(uint32_t y = min_y; y < max_y; y++)
{
for(uint32_t x=0;x<timg->Width;x++)
for(uint32_t x = min_x; x < max_x; x++)
{
UVPoint uv=xy2uv(x,y,v[0],v[1],v[2],v[3],t[0],t[1],t[2],t[3]);
texbBmp[uint32_t(uv.U*Width+0.5)+uint32_t(uv.V*Height+0.5)*Width]=rawBmp[x+y*timg->Width];
UVPoint uv=xy2uv(x, y, v[0], v[1], v[2], v[3], t[0], t[1], t[2], t[3]);
texbBmp[uint32_t(uv.U * Width + 0.5) + uint32_t(uv.V * Height + 0.5) * Width] = rawBmp[x + y * timg->Width];
}
}
}
Expand Down

0 comments on commit 9ad393a

Please sign in to comment.