Skip to content

Commit

Permalink
First implementation, that only display some informations about the B…
Browse files Browse the repository at this point in the history
…LP file
  • Loading branch information
Kanma committed Jul 14, 2011
1 parent 91c4672 commit 07ef52d
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
project(BLPCONVERTER)
cmake_minimum_required(VERSION 2.6)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BLPCONVERTER_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BLPCONVERTER_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BLPCONVERTER_BINARY_DIR}/bin")

add_subdirectory(dependencies)

include_directories("${BLPCONVERTER_SOURCE_DIR}/dependencies/include/"
)

add_executable(BLPConverter main.cpp blp.cpp)
53 changes: 53 additions & 0 deletions blp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "blp.h"
#include <string.h>


bool blp_processFile(FILE* pFile, tBLP2Header* pHeader)
{
fseek(pFile, 0, SEEK_SET);
fread((void*) pHeader, sizeof(uint8_t), 4, pFile);

if (strncmp((char*) pHeader->magic, "BLP2", 4) != 0)
return false;

fread((void*) &pHeader->type, sizeof(tBLP2Header) - 4 * sizeof(uint8_t), 1, pFile);

return true;
}


tBLPFormat blp_format(tBLP2Header* pHeader)
{
if (pHeader->encoding == BLP_ENCODING_UNCOMPRESSED)
return tBLPFormat((pHeader->encoding << 16) | (pHeader->alphaDepth << 8));

return tBLPFormat((pHeader->encoding << 16) | (pHeader->alphaDepth << 8) | pHeader->alphaEncoding);
}


unsigned int blp_nbMipLevels(tBLP2Header* pHeader)
{
unsigned int nb = 0;

while ((pHeader->offsets[nb] != 0) && (nb < 16))
++nb;

return nb;
}


std::string blp_asString(tBLPFormat format)
{
switch (format)
{
case BLP_FORMAT_PALETTED_NO_ALPHA: return "Uncompressed paletted image, no alpha";
case BLP_FORMAT_PALETTED_ALPHA_1: return "Uncompressed paletted image, 1-bit alpha";
case BLP_FORMAT_PALETTED_ALPHA_8: return "Uncompressed paletted image, 8-bit alpha";
case BLP_FORMAT_DXT1_NO_ALPHA: return "DXT1, no alpha";
case BLP_FORMAT_DXT1_ALPHA_1: return "DXT1, 1-bit alpha";
case BLP_FORMAT_DXT3_ALPHA_4: return "DXT3, 4-bit alpha";
case BLP_FORMAT_DXT3_ALPHA_8: return "DXT3, 8-bit alpha";
case BLP_FORMAT_DXT5_ALPHA_8: return "DXT5, 8-bit alpha";
default: return "Unknown";
}
}
73 changes: 73 additions & 0 deletions blp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef _BLP_H_
#define _BLP_H_

#include <stdint.h>
#include <stdio.h>
#include <string>


// A description of the BLP format can be found on Wikipedia: http://en.wikipedia.org/wiki/.BLP

struct tBLP2Header
{
uint8_t magic[4]; // Always 'BLP2'
uint32_t type; // 0: JPEG, 1: see encoding
uint8_t encoding; // 1: Uncompressed, 2: DXT compression
uint8_t alphaDepth; // 0, 1, 4 or 8 bits
uint8_t alphaEncoding; // 0: DXT1, 1: DXT3, 7: DXT5
uint8_t hasMipLevels;
uint32_t width; // In pixels, power-of-two
uint32_t height;
uint32_t offsets[16];
uint32_t lengths[16];
uint32_t palette[256]; // 256 BGRA colors
};


enum tBLPEncoding
{
BLP_ENCODING_UNCOMPRESSED = 1,
BLP_ENCODING_DXT = 2,
};


enum tBLPAlphaDepth
{
BLP_ALPHA_DEPTH_0 = 0,
BLP_ALPHA_DEPTH_1 = 1,
BLP_ALPHA_DEPTH_4 = 4,
BLP_ALPHA_DEPTH_8 = 8,
};


enum tBLPAlphaEncoding
{
BLP_ALPHA_ENCODING_DXT1 = 0,
BLP_ALPHA_ENCODING_DXT3 = 1,
BLP_ALPHA_ENCODING_DXT5 = 7,
};


enum tBLPFormat
{
BLP_FORMAT_PALETTED_NO_ALPHA = (BLP_ENCODING_UNCOMPRESSED << 16) | (BLP_ALPHA_DEPTH_0 << 8),
BLP_FORMAT_PALETTED_ALPHA_1 = (BLP_ENCODING_UNCOMPRESSED << 16) | (BLP_ALPHA_DEPTH_1 << 8),
BLP_FORMAT_PALETTED_ALPHA_8 = (BLP_ENCODING_UNCOMPRESSED << 16) | (BLP_ALPHA_DEPTH_8 << 8),

BLP_FORMAT_DXT1_NO_ALPHA = (BLP_ENCODING_DXT << 16) | (BLP_ALPHA_DEPTH_0 << 8) | BLP_ALPHA_ENCODING_DXT1,
BLP_FORMAT_DXT1_ALPHA_1 = (BLP_ENCODING_DXT << 16) | (BLP_ALPHA_DEPTH_1 << 8) | BLP_ALPHA_ENCODING_DXT1,
BLP_FORMAT_DXT3_ALPHA_4 = (BLP_ENCODING_DXT << 16) | (BLP_ALPHA_DEPTH_4 << 8) | BLP_ALPHA_ENCODING_DXT3,
BLP_FORMAT_DXT3_ALPHA_8 = (BLP_ENCODING_DXT << 16) | (BLP_ALPHA_DEPTH_8 << 8) | BLP_ALPHA_ENCODING_DXT3,
BLP_FORMAT_DXT5_ALPHA_8 = (BLP_ENCODING_DXT << 16) | (BLP_ALPHA_DEPTH_8 << 8) | BLP_ALPHA_ENCODING_DXT5,
};


bool blp_processFile(FILE* pFile, tBLP2Header* pHeader);

tBLPFormat blp_format(tBLP2Header* pHeader);

unsigned int blp_nbMipLevels(tBLP2Header* pHeader);

std::string blp_asString(tBLPFormat format);

#endif
115 changes: 115 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "blp.h"
#include <SimpleOpt.h>
#include <iostream>
#include <string>


using namespace std;


/**************************** COMMAND-LINE PARSING ****************************/

// The valid options
enum
{
OPT_HELP,
OPT_INFOS,
};


const CSimpleOpt::SOption COMMAND_LINE_OPTIONS[] = {
{ OPT_HELP, "-h", SO_NONE },
{ OPT_HELP, "--help", SO_NONE },
{ OPT_INFOS, "-i", SO_NONE },
{ OPT_INFOS, "--infos", SO_NONE },

SO_END_OF_OPTIONS
};


/********************************** FUNCTIONS *********************************/

void showUsage(const std::string& strApplicationName)
{
cout << "BLPConverter" << endl
<< "Usage: " << strApplicationName << " [options] <blp_filename> [<destination>]" << endl
<< endl;
}


void showInfos(const std::string& strFileName, tBLP2Header* pHeader)
{
cout << endl
<< "Infos about '" << strFileName << "':" << endl
<< " - Format: " << blp_asString(blp_format(pHeader)) << endl
<< " - Dimensions: " << pHeader->width << "x" << pHeader->height << endl
<< " - Mip levels: " << blp_nbMipLevels(pHeader) << endl
<< endl;
}


int main(int argc, char** argv)
{
bool bInfos = false;


// Parse the command-line parameters
CSimpleOpt args(argc, argv, COMMAND_LINE_OPTIONS);
while (args.Next())
{
if (args.LastError() == SO_SUCCESS)
{
switch (args.OptionId())
{
case OPT_HELP:
showUsage(argv[0]);
return 0;

case OPT_INFOS:
bInfos = true;
break;
}
}
else
{
cerr << "Invalid argument: " << args.OptionText() << endl;
return -1;
}
}

if (args.FileCount() == 0)
{
cerr << "No BLP file specified" << endl;
return -1;
}

// Only support the '--infos' option at the moment
if (!bInfos)
{
cerr << "The conversion of BLP files isn't implemented yet" << endl;
return -1;
}


FILE* pFile = fopen(args.File(0), "rb");
if (!pFile)
{
cerr << "Failed to open the file '" << args.File(0) << "'" << endl;
return -1;
}

tBLP2Header header;

if (!blp_processFile(pFile, &header))
{
cerr << "Failed to process the file '" << args.File(0) << "'" << endl;
fclose(pFile);
return -1;
}

showInfos(args.File(0), &header);

fclose(pFile);

return 0;
}

0 comments on commit 07ef52d

Please sign in to comment.