Browse Source

Added code and resources for exporting TIM image files directly

master
Nico de Poel 3 years ago
parent
commit
628955d7a0
  1. 7
      PS1BSP.vcxproj
  2. 17
      PS1BSP.vcxproj.filters
  3. BIN
      palette.lmp
  4. 99
      tim.cpp
  5. 98
      tim.h

7
PS1BSP.vcxproj

@ -144,6 +144,7 @@
<ClCompile Include="lighting.cpp" /> <ClCompile Include="lighting.cpp" />
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />
<ClCompile Include="texture.cpp" /> <ClCompile Include="texture.cpp" />
<ClCompile Include="tim.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="bsp.h" /> <ClInclude Include="bsp.h" />
@ -158,6 +159,12 @@
<ClInclude Include="rectpack\insert_and_split.h" /> <ClInclude Include="rectpack\insert_and_split.h" />
<ClInclude Include="rectpack\rect_structs.h" /> <ClInclude Include="rectpack\rect_structs.h" />
<ClInclude Include="texture.h" /> <ClInclude Include="texture.h" />
<ClInclude Include="tim.h" />
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="palette.lmp">
<FileType>Document</FileType>
</CopyFileToFolders>
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

17
PS1BSP.vcxproj.filters

@ -21,11 +21,14 @@
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="lighting.cpp">
<Filter>Header Files</Filter>
<ClCompile Include="tim.cpp">
<Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="texture.cpp"> <ClCompile Include="texture.cpp">
<Filter>Header Files</Filter>
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="lighting.cpp">
<Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -65,5 +68,13 @@
<ClInclude Include="texture.h"> <ClInclude Include="texture.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="tim.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="palette.lmp">
<Filter>Resource Files</Filter>
</CopyFileToFolders>
</ItemGroup> </ItemGroup>
</Project> </Project>

BIN
palette.lmp

99
tim.cpp

@ -0,0 +1,99 @@
#include "tim.h"
int tim::ExportFile(const char* fileName, tim::PARAM *param) {
FILE *fp;
fopen_s(&fp, fileName, "wb");
if (!fp) {
return(-1);
}
tim::HEADER fileHead={{0}};
tim::CLUT_HEAD clutHead={0};
tim::IMG_HEAD imgHead={0};
// Prepare header
fileHead.id.id = 0x10;
fileHead.id.ver = 0;
fileHead.flags.pmode = param->format;
// Prepare CLUT data block if image is 8-bit or less and that the pointer to the CLUT data is not NULL
if ((param->format <= 1) && (param->clutData != NULL)) {
fileHead.flags.clut = 1;
clutHead.cw = param->clutWidth;
clutHead.ch = param->clutHeight;
clutHead.cx = param->clutXoffs;
clutHead.cy = param->clutYoffs;
clutHead.len = ((2*param->clutWidth)*param->clutHeight);
clutHead.len += sizeof(clutHead);
}
// Prepare image data block
imgHead.w = param->imgWidth;
imgHead.h = param->imgHeight;
imgHead.x = param->imgXoffs;
imgHead.y = param->imgYoffs;
// Calculate final size of image based on its color depth
switch(param->format) {
case 0: // 4-bit with 16-color CLUT
imgHead.len = param->imgWidth/2;
imgHead.w /= 4;
break;
case 1: // 8-bit with 256-color CLUT
imgHead.len = param->imgWidth;
imgHead.w /= 2;
break;
case 2: // 16-bit RGB5I1
imgHead.len = param->imgWidth*2;
break;
case 3: // 24-bit RGB8
imgHead.len = (param->imgWidth*3);
imgHead.w = (u_short)ceil((double)imgHead.w*1.5f);
break;
}
// Calculate size of image data block
imgHead.len *= param->imgHeight;
imgHead.len += sizeof(imgHead);
// Write the header
fwrite(&fileHead, 1, sizeof(fileHead), fp);
// Write the CLUT data block
if ((param->format <= 1) && (param->clutData != NULL)) {
fwrite(&clutHead, 1, sizeof(clutHead), fp);
fwrite(param->clutData, 1, clutHead.len-sizeof(clutHead), fp);
}
// Write the image data block
fwrite(&imgHead, 1, sizeof(imgHead), fp);
fwrite(param->imgData, 1, imgHead.len-sizeof(imgHead), fp);
// Close and return
fclose(fp);
return(0);
}
void tim::FreeParam(tim::PARAM *param) {
if (param->imgData != NULL) {
free(param->imgData);
param->imgData = NULL;
}
if (param->clutData != NULL) {
free(param->clutData);
param->clutData = NULL;
}
}

98
tim.h

@ -0,0 +1,98 @@
#ifndef _TIM_H
#define _TIM_H
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <math.h>
#define TIM_OUTPUT_CLUT4 0
#define TIM_OUTPUT_CLUT8 1
#define TIM_OUTPUT_RGB5 2
#define TIM_OUTPUT_RGB24 3
namespace tim {
// TIM header struct
typedef struct {
// ID sub-struct
struct HEADER_ID {
u_int id:8; // Always 0x10
u_int ver:8; // Always 0
u_int pad:16; // Useless padding
} id;
// Flags sub-struct
struct HEADER_FLAGS {
u_int pmode:3; // Pixel mode (0: 4-bit, 1: 8-bit, 2:16-bit, 3:24-bit)
u_int clut:1;
u_int pad:24;
} flags;
} HEADER;
// CLUT header struct
typedef struct {
u_int len;
u_short cx,cy;
u_short cw,ch;
} CLUT_HEAD;
// Image data block header
typedef struct {
u_int len;
u_short x,y;
u_short w,h;
} IMG_HEAD;
typedef struct {
// 0: 4-bit CLUT, 1: 8-bit CLUT, 2:16-bit, 3:24-bit
int format;
// Image data params
void *imgData;
u_short imgWidth,imgHeight;
u_short imgXoffs,imgYoffs;
// CLUT data params
void *clutData;
u_short clutWidth,clutHeight;
u_short clutXoffs,clutYoffs;
} PARAM;
// RGB5A1 pixel format struct
typedef struct {
u_short r:5;
u_short g:5;
u_short b:5;
u_short i:1;
} PIX_RGB5;
typedef struct {
u_char r;
u_char g;
u_char b;
} PIX_RGB24;
/*! tim::ExportFile()
*
* /param[in] fileName - Name of TIM file.
* /param[in] *param - tim::PARAM object of TIM export parameters.
*
* /returns Zero if the TIM file was written successfully, otherwise an error occured.
*
*/
int ExportFile(const char* fileName, tim::PARAM *param);
void FreeParam(tim::PARAM *param);
};
#endif // _TIM_H
Loading…
Cancel
Save