|
@@ -24,75 +24,88 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
|
|
+#include <fstream>
|
|
|
|
|
+#include <iostream>
|
|
|
|
|
+
|
|
|
#include "TGACompiler.h"
|
|
#include "TGACompiler.h"
|
|
|
-#include "DiskFile.h"
|
|
|
|
|
#include "PixelFormat.h"
|
|
#include "PixelFormat.h"
|
|
|
-#include "Resource.h"
|
|
|
|
|
-#include "Log.h"
|
|
|
|
|
|
|
+#include "TextureFormat.h"
|
|
|
|
|
|
|
|
namespace crown
|
|
namespace crown
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
-TGACompiler::TGACompiler(const char* root_path, const char* dest_path) :
|
|
|
|
|
- Compiler(root_path, dest_path, TEXTURE_TYPE),
|
|
|
|
|
- m_image_format(PF_UNKNOWN),
|
|
|
|
|
- m_image_channels(0),
|
|
|
|
|
- m_image_size(0),
|
|
|
|
|
- m_image_data(NULL)
|
|
|
|
|
|
|
+TGACompiler::TGACompiler() :
|
|
|
|
|
+ m_texture_data_size(0),
|
|
|
|
|
+ m_texture_data(NULL)
|
|
|
{
|
|
{
|
|
|
- memset(&m_tga_header, 0, sizeof(TGAHeader));
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
TGACompiler::~TGACompiler()
|
|
TGACompiler::~TGACompiler()
|
|
|
{
|
|
{
|
|
|
- cleanup_impl();
|
|
|
|
|
|
|
+ if (m_texture_data)
|
|
|
|
|
+ {
|
|
|
|
|
+ delete[] m_texture_data;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
-size_t TGACompiler::read_header_impl(DiskFile* in_file)
|
|
|
|
|
|
|
+size_t TGACompiler::compile_impl(const char* resource_path)
|
|
|
{
|
|
{
|
|
|
|
|
+ std::fstream in_file;
|
|
|
|
|
+ in_file.open(resource_path, std::fstream::in | std::fstream::binary);
|
|
|
|
|
+
|
|
|
|
|
+ if (!in_file.is_open())
|
|
|
|
|
+ {
|
|
|
|
|
+ std::cout << "Unable to open file: " << resource_path << std::endl;
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// Read the header
|
|
// Read the header
|
|
|
- in_file->read(&m_tga_header, sizeof(TGAHeader));
|
|
|
|
|
|
|
+ if (!in_file.read((char*)(char*)&m_tga_header, sizeof(TGAHeader)))
|
|
|
|
|
+ {
|
|
|
|
|
+ std::cout << "Unable to read the TGA header." << std::endl;
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
// Skip TGA ID
|
|
// Skip TGA ID
|
|
|
- in_file->skip(m_tga_header.id_length);
|
|
|
|
|
|
|
+ in_file.seekg(m_tga_header.id_length);
|
|
|
|
|
|
|
|
- return sizeof(TGAHeader) + m_tga_header.id_length;
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ // Compute color channels
|
|
|
|
|
+ m_tga_channels = m_tga_header.pixel_depth / 8;
|
|
|
|
|
+ m_tga_size = m_tga_header.width * m_tga_header.height;
|
|
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
|
|
-size_t TGACompiler::read_resource_impl(DiskFile* in_file)
|
|
|
|
|
-{
|
|
|
|
|
- // Compute color channels
|
|
|
|
|
- m_image_channels = m_tga_header.pixel_depth / 8;
|
|
|
|
|
-
|
|
|
|
|
- // Compute image size
|
|
|
|
|
- m_image_size = m_tga_header.width * m_tga_header.height;
|
|
|
|
|
|
|
+ m_texture_header.version = TEXTURE_VERSION;
|
|
|
|
|
+ m_texture_header.width = m_tga_header.width;
|
|
|
|
|
+ m_texture_header.height = m_tga_header.height;
|
|
|
|
|
|
|
|
// Select the appropriate pixel format and allocate
|
|
// Select the appropriate pixel format and allocate
|
|
|
// resource data based on tga size and channels
|
|
// resource data based on tga size and channels
|
|
|
- switch (m_image_channels)
|
|
|
|
|
|
|
+ switch (m_tga_channels)
|
|
|
{
|
|
{
|
|
|
case 2:
|
|
case 2:
|
|
|
case 3:
|
|
case 3:
|
|
|
{
|
|
{
|
|
|
- m_image_format = PF_RGB_8;
|
|
|
|
|
- m_image_data = (uint8_t*)default_allocator().allocate(m_image_size * 3 * sizeof(uint8_t));
|
|
|
|
|
|
|
+ m_texture_header.format = PF_RGB_8;
|
|
|
|
|
+
|
|
|
|
|
+ m_texture_data_size = m_tga_size * 3;
|
|
|
|
|
+ m_texture_data = new uint8_t[m_texture_data_size];
|
|
|
|
|
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
case 4:
|
|
case 4:
|
|
|
{
|
|
{
|
|
|
- m_image_format = PF_RGBA_8;
|
|
|
|
|
- m_image_data = (uint8_t*)default_allocator().allocate(m_image_size * m_image_channels * sizeof(uint8_t));
|
|
|
|
|
|
|
+ m_texture_header.format = PF_RGBA_8;
|
|
|
|
|
+
|
|
|
|
|
+ m_texture_data_size = m_tga_size * m_tga_channels;
|
|
|
|
|
+ m_texture_data = new uint8_t[m_texture_data_size];
|
|
|
|
|
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
default:
|
|
default:
|
|
|
{
|
|
{
|
|
|
- Log::e("Unable to determine TGA channels.");
|
|
|
|
|
|
|
+ std::cout << "Unable to determine TGA channels." << std::endl;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -102,7 +115,7 @@ size_t TGACompiler::read_resource_impl(DiskFile* in_file)
|
|
|
{
|
|
{
|
|
|
case 0:
|
|
case 0:
|
|
|
{
|
|
{
|
|
|
- Log::e("Fatal: The resource does not contain image data.");
|
|
|
|
|
|
|
+ std::cout << "The file does not contain image data: " << resource_path << std::endl;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
case 2:
|
|
case 2:
|
|
@@ -119,73 +132,54 @@ size_t TGACompiler::read_resource_impl(DiskFile* in_file)
|
|
|
|
|
|
|
|
default:
|
|
default:
|
|
|
{
|
|
{
|
|
|
- Log::e("Fatal: Image type not supported.");
|
|
|
|
|
|
|
+ std::cout << "Image type not supported." << std::endl;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Return the total resource size
|
|
// Return the total resource size
|
|
|
- return m_image_size * m_image_channels + sizeof(PixelFormat) + sizeof(uint16_t) + sizeof(uint16_t);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
|
|
-void TGACompiler::write_header_impl(DiskFile* out_file)
|
|
|
|
|
-{
|
|
|
|
|
- // Write the texture header
|
|
|
|
|
- out_file->write(&m_image_format, sizeof(PixelFormat));
|
|
|
|
|
- out_file->write(&m_tga_header.width, sizeof(uint16_t));
|
|
|
|
|
- out_file->write(&m_tga_header.height, sizeof(uint16_t));
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
|
|
-void TGACompiler::write_resource_impl(DiskFile* out_file)
|
|
|
|
|
-{
|
|
|
|
|
- // Write out the data
|
|
|
|
|
- out_file->write(m_image_data, m_image_size * m_image_channels);
|
|
|
|
|
|
|
+ return sizeof(TextureHeader) + m_texture_data_size;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
-void TGACompiler::cleanup_impl()
|
|
|
|
|
|
|
+void TGACompiler::write_impl(std::fstream& out_file)
|
|
|
{
|
|
{
|
|
|
- if (m_image_data)
|
|
|
|
|
- {
|
|
|
|
|
- default_allocator().deallocate(m_image_data);
|
|
|
|
|
- m_image_data = NULL;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ out_file.write((char*)&m_texture_header, sizeof(TextureHeader));
|
|
|
|
|
+ out_file.write((char*)m_texture_data, m_texture_data_size);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
-void TGACompiler::load_uncompressed(DiskFile* in_file)
|
|
|
|
|
|
|
+void TGACompiler::load_uncompressed(std::fstream& in_file)
|
|
|
{
|
|
{
|
|
|
uint64_t size = m_tga_header.width * m_tga_header.height;
|
|
uint64_t size = m_tga_header.width * m_tga_header.height;
|
|
|
|
|
|
|
|
- if (m_image_channels == 2)
|
|
|
|
|
|
|
+ if (m_tga_channels == 2)
|
|
|
{
|
|
{
|
|
|
int32_t j = 0;
|
|
int32_t j = 0;
|
|
|
|
|
|
|
|
- for (uint64_t i = 0; i < size * m_image_channels; i++)
|
|
|
|
|
|
|
+ for (uint64_t i = 0; i < size * m_tga_channels; i++)
|
|
|
{
|
|
{
|
|
|
uint16_t pixel_data;
|
|
uint16_t pixel_data;
|
|
|
|
|
|
|
|
- in_file->read(&pixel_data, sizeof(pixel_data));
|
|
|
|
|
|
|
+ in_file.read((char*)&pixel_data, sizeof(pixel_data));
|
|
|
|
|
|
|
|
- m_image_data[j + 0] = (pixel_data & 0x7c) >> 10;
|
|
|
|
|
- m_image_data[j + 1] = (pixel_data & 0x3e) >> 5;
|
|
|
|
|
- m_image_data[j + 2] = (pixel_data & 0x1f);
|
|
|
|
|
|
|
+ m_texture_data[j + 0] = (pixel_data & 0x7c) >> 10;
|
|
|
|
|
+ m_texture_data[j + 1] = (pixel_data & 0x3e) >> 5;
|
|
|
|
|
+ m_texture_data[j + 2] = (pixel_data & 0x1f);
|
|
|
|
|
|
|
|
j += 3;
|
|
j += 3;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- in_file->read(m_image_data, (size_t)(size * m_image_channels));
|
|
|
|
|
|
|
+ in_file.read((char*)m_texture_data, (size_t)(size * m_tga_channels));
|
|
|
|
|
|
|
|
swap_red_blue();
|
|
swap_red_blue();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
-void TGACompiler::load_compressed(DiskFile* in_file)
|
|
|
|
|
|
|
+void TGACompiler::load_compressed(std::fstream& in_file)
|
|
|
{
|
|
{
|
|
|
uint8_t rle_id = 0;
|
|
uint8_t rle_id = 0;
|
|
|
uint32_t i = 0;
|
|
uint32_t i = 0;
|
|
@@ -197,28 +191,28 @@ void TGACompiler::load_compressed(DiskFile* in_file)
|
|
|
|
|
|
|
|
while (i < size)
|
|
while (i < size)
|
|
|
{
|
|
{
|
|
|
- in_file->read(&rle_id, sizeof(uint8_t));
|
|
|
|
|
|
|
+ in_file.read((char*)&rle_id, sizeof(uint8_t));
|
|
|
|
|
|
|
|
// If MSB == 1
|
|
// If MSB == 1
|
|
|
if (rle_id & 0x80)
|
|
if (rle_id & 0x80)
|
|
|
{
|
|
{
|
|
|
rle_id -= 127;
|
|
rle_id -= 127;
|
|
|
|
|
|
|
|
- in_file->read(&colors, m_image_channels);
|
|
|
|
|
|
|
+ in_file.read((char*)&colors, m_tga_channels);
|
|
|
|
|
|
|
|
while (rle_id)
|
|
while (rle_id)
|
|
|
{
|
|
{
|
|
|
- m_image_data[colors_read + 0] = colors[2];
|
|
|
|
|
- m_image_data[colors_read + 1] = colors[1];
|
|
|
|
|
- m_image_data[colors_read + 2] = colors[0];
|
|
|
|
|
|
|
+ m_texture_data[colors_read + 0] = colors[2];
|
|
|
|
|
+ m_texture_data[colors_read + 1] = colors[1];
|
|
|
|
|
+ m_texture_data[colors_read + 2] = colors[0];
|
|
|
|
|
|
|
|
- if (m_image_channels == 4)
|
|
|
|
|
|
|
+ if (m_tga_channels == 4)
|
|
|
{
|
|
{
|
|
|
- m_image_data[colors_read + 3] = colors[3];
|
|
|
|
|
|
|
+ m_texture_data[colors_read + 3] = colors[3];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
rle_id--;
|
|
rle_id--;
|
|
|
- colors_read += m_image_channels;
|
|
|
|
|
|
|
+ colors_read += m_tga_channels;
|
|
|
i++;
|
|
i++;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -228,19 +222,19 @@ void TGACompiler::load_compressed(DiskFile* in_file)
|
|
|
|
|
|
|
|
while (rle_id)
|
|
while (rle_id)
|
|
|
{
|
|
{
|
|
|
- in_file->read(colors, m_image_channels);
|
|
|
|
|
|
|
+ in_file.read((char*)colors, m_tga_channels);
|
|
|
|
|
|
|
|
- m_image_data[colors_read + 0] = colors[2];
|
|
|
|
|
- m_image_data[colors_read + 1] = colors[1];
|
|
|
|
|
- m_image_data[colors_read + 2] = colors[0];
|
|
|
|
|
|
|
+ m_texture_data[colors_read + 0] = colors[2];
|
|
|
|
|
+ m_texture_data[colors_read + 1] = colors[1];
|
|
|
|
|
+ m_texture_data[colors_read + 2] = colors[0];
|
|
|
|
|
|
|
|
- if (m_image_channels == 4)
|
|
|
|
|
|
|
+ if (m_tga_channels == 4)
|
|
|
{
|
|
{
|
|
|
- m_image_data[colors_read + 3] = colors[3];
|
|
|
|
|
|
|
+ m_texture_data[colors_read + 3] = colors[3];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
rle_id--;
|
|
rle_id--;
|
|
|
- colors_read += m_image_channels;
|
|
|
|
|
|
|
+ colors_read += m_tga_channels;
|
|
|
i++;
|
|
i++;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -250,13 +244,12 @@ void TGACompiler::load_compressed(DiskFile* in_file)
|
|
|
//-----------------------------------------------------------------------------
|
|
//-----------------------------------------------------------------------------
|
|
|
void TGACompiler::swap_red_blue()
|
|
void TGACompiler::swap_red_blue()
|
|
|
{
|
|
{
|
|
|
- for (uint64_t i = 0; i < m_image_size * m_image_channels; i += m_image_channels)
|
|
|
|
|
|
|
+ for (uint64_t i = 0; i < m_tga_size * m_tga_channels; i += m_tga_channels)
|
|
|
{
|
|
{
|
|
|
- m_image_data[i + 0] ^= m_image_data[i + 2];
|
|
|
|
|
- m_image_data[i + 2] ^= m_image_data[i + 0];
|
|
|
|
|
- m_image_data[i + 0] ^= m_image_data[i + 2];
|
|
|
|
|
|
|
+ m_texture_data[i + 0] ^= m_texture_data[i + 2];
|
|
|
|
|
+ m_texture_data[i + 2] ^= m_texture_data[i + 0];
|
|
|
|
|
+ m_texture_data[i + 0] ^= m_texture_data[i + 2];
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} // namespace crown
|
|
} // namespace crown
|
|
|
-
|
|
|