Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indent cleanup and test_generate with progress bar for test image generation #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ CTestTestfile.cmake
*.vcxproj
*.sln
cmake_install.cmake
*.so
*.dll
*.a
*.lib
tests/data/*_*
test_generate*
test_c_loading*
test_c_linkage*
test_types*
test_dll*
test_static*
test_loading*
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "xTGA/include/rw_progress_bar"]
path = xTGA/include/rw_progress_bar
url = http://github.com/jopadan/rw_progress_bar
8 changes: 7 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ endforeach()

set(interface ${PROJECT_SOURCE_DIR}/xTGA/include)
set(common ${PROJECT_SOURCE_DIR}/tests)
set(progress ${interface}/rw_progress_bar)

add_executable(test_dll main.cpp)
target_link_libraries(test_dll xTGA)
target_include_directories(test_dll PUBLIC ${interface})

add_executable(test_generate ${progress}/progress.h generate.cpp)
target_link_libraries(test_generate xTGA)
target_include_directories(test_generate PUBLIC ${interface} ${progress})

add_executable(test_static main.cpp)
target_link_libraries(test_static xTGAs)
target_include_directories(test_static PUBLIC ${interface})
Expand All @@ -39,4 +44,5 @@ target_include_directories(test_c_linkage PUBLIC ${interface})

add_executable(test_c_loading c_loading.c assert_equal.h library_error.h cmake_file.h)
target_link_libraries(test_c_loading xTGA)
target_include_directories(test_c_loading PUBLIC ${interface} ${common})
target_include_directories(test_c_loading PUBLIC ${interface} ${common})

Binary file added tests/data/32x32.tga
Binary file not shown.
3 changes: 0 additions & 3 deletions tests/data/32x32_16BitOut.raw

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_16BitTGA.tga

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_16BitTGA_RLE.tga

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_24BitOut.raw

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_24BitTGA.tga

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_24BitTGA_RLE.tga

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_32BitOut.raw

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_32BitTGA.tga

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_32BitTGA_RLE.tga

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_8BitOut.raw

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_8BitTGA.tga

This file was deleted.

3 changes: 0 additions & 3 deletions tests/data/32x32_8BitTGA_RLE.tga

This file was deleted.

135 changes: 135 additions & 0 deletions tests/generate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//============ Copyright © 2019 Brett Anthony. All rights reserved. ============
///
/// This work is licensed under the terms of the MIT license.
/// For a copy, see <https://opensource.org/licenses/MIT>.
//==============================================================================
/// file : loading.cpp
/// purpose : Tests that the library loads and maintains the proper data for
/// all possible image types.
//==============================================================================

#define _CRT_SECURE_NO_WARNINGS

#include "assert_equal.h"
#include "library_error.h"
#include "xTGA/xTGA.h"
#include "file.h"
#include "progress.h"
#include <filesystem>
#include <cstddef>
#include <cstring>
#include <iostream>
#include <string>

using namespace xtga;
using namespace xtga::pixelformats;
using namespace xtga::flags;

bool tga2raw(uint8_t bpp, const std::filesystem::path& src);

int main(int argc, char** argv)
{
if(argc < 2)
{
std::cerr << "Usage: " << argv[0] << " SRC" << std::endl << "Generate raw/tga test images from a single tga file" << std::endl
<< std::endl << "The SRC argument is the filename path of the TGA image." << std::endl;
exit(EXIT_FAILURE);
}

exit(tga2raw(8, argv[1]) && tga2raw(16, argv[1]) && tga2raw(24, argv[1]) && tga2raw(32, argv[1]) ? EXIT_SUCCESS : EXIT_FAILURE);
}

bool tga2raw(uint8_t bpp, const std::filesystem::path& src)
{
if(bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32) bpp = 32;
char* bpp_str = nullptr;
asprintf(&bpp_str, "%02hhu", bpp);
std::filesystem::path dst = src;
dst = dst.replace_extension("").concat("_").concat(bpp_str).concat("Bit");
ERRORCODE terr = xtga::ERRORCODE::NONE;
auto tga = TGAFile::Alloc(src.c_str(), &terr);
ASSERT_ERRORCODE_NONE(terr);

ALPHATYPE at = ALPHATYPE::NOALPHA;
auto ImageData = (ManagedArray<RGBA8888>*)tga->GetImageRGBA(&at, &terr);

ASSERT_ERRORCODE_NONE(terr);

TGAFile* tga_out = nullptr;
void* obuffer = malloc(bpp / 8 * tga->GetWidth() * tga->GetHeight());
if (!obuffer) { UNKNOWN_ERROR; }
switch(bpp)
{
case 32:
for (uint32 i = 0; i < tga->GetWidth() * tga->GetHeight(); ++i)
{
((RGBA8888*)obuffer)[i].B = ImageData->at(i).R;
((RGBA8888*)obuffer)[i].G = ImageData->at(i).G;
((RGBA8888*)obuffer)[i].R = ImageData->at(i).B;
((RGBA8888*)obuffer)[i].A = ImageData->at(i).A;
}
tga_out = xtga::TGAFile::Alloc(obuffer, tga->GetWidth(), tga->GetHeight(), xtga::Parameters::BGRA32_STRAIGHT_ALPHA(), &terr);
ASSERT_ERRORCODE_NONE(terr);
break;
case 24:
for (uint32 i = 0; i < tga->GetWidth() * tga->GetHeight(); ++i)
{
((BGR888*)obuffer)[i].R = ImageData->at(i).R;
((BGR888*)obuffer)[i].G = ImageData->at(i).G;
((BGR888*)obuffer)[i].B = ImageData->at(i).B;
}
tga_out = xtga::TGAFile::Alloc(obuffer, tga->GetWidth(), tga->GetHeight(), xtga::Parameters::BGR24(), &terr);
ASSERT_ERRORCODE_NONE(terr);
break;
case 16:
for (uint32 i = 0; i < tga->GetWidth() * tga->GetHeight(); ++i)
{
((BGRA5551*)obuffer)[i].B = (ImageData->at(i).B & 0xF8) >> 3;
((BGRA5551*)obuffer)[i].G = (ImageData->at(i).G & 0xF8) >> 3;
((BGRA5551*)obuffer)[i].R = (ImageData->at(i).R & 0xF8) >> 3;
((BGRA5551*)obuffer)[i].A = (ImageData->at(i).A == 255) ? 1 : 0;
}
tga_out = xtga::TGAFile::Alloc(obuffer, tga->GetWidth(), tga->GetHeight(), xtga::Parameters::BGR16(), &terr);
ASSERT_ERRORCODE_NONE(terr);
break;
case 8:
for (uint32 i = 0; i < tga->GetWidth() * tga->GetHeight(); ++i)
((uchar*)obuffer)[i] = (uchar)((float)ImageData->at(i).R * 0.25 + (float)ImageData->at(i).G * 0.50 + (float)ImageData->at(i).B * 0.25);

tga_out = xtga::TGAFile::Alloc(obuffer, tga->GetWidth(), tga->GetHeight(), xtga::Parameters::I8(), &terr);
ASSERT_ERRORCODE_NONE(terr);
break;
default:
break;
}

if(tga_out != nullptr)
{
std::filesystem::path out = dst; out = out.replace_extension("").concat("TGA").replace_extension(".tga");
tga_out->SaveFile(out.c_str(), &terr);
ASSERT_ERRORCODE_NONE(terr);
tga_out->CompressWithRLE(&terr);
ASSERT_ERRORCODE_NONE(terr);
std::filesystem::path rle_out = dst; rle_out = rle_out.replace_extension("").concat("TGA_RLE").replace_extension(".tga");
tga_out->SaveFile(rle_out.c_str(), &terr);
ASSERT_ERRORCODE_NONE(terr);
TGAFile::Free(tga_out);
}

size_t len = bpp / 8 * tga->GetWidth() * tga->GetHeight();
size_t written = 0;
const std::filesystem::path raw_out = dst.concat("Out").replace_extension(".raw").c_str();

/* write raw image and print progress status */
FILE* fp = fopen(raw_out.c_str(), "wb");
if(fp != nullptr && (written = fwrite_progress_bar(obuffer, raw_out.c_str(), len, 1024, fp, 40, '=', ' ')) != len)
fclose(fp);

/* cleanup */
free(obuffer);
ManagedArray<RGBA8888>::Free(ImageData);
TGAFile::Free(tga);

return true;
}

1 change: 1 addition & 0 deletions xTGA/include/rw_progress_bar
Submodule rw_progress_bar added at 5a8b47
12 changes: 6 additions & 6 deletions xTGA/include/xTGA/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ namespace xtga
*/
enum class ERRORCODE : uint32
{
UNKNOWN = 0xFFFFFFFF, /*!< An unknown error occured. */
NONE = 0x00000000, /*!< Operation completed without error. */
FILE_ERROR = 0x00000001, /*!< An error occured while opening/saving a file descriptor. */
UNKNOWN = 0xFFFFFFFF, /*!< An unknown error occured. */
NONE = 0x00000000, /*!< Operation completed without error. */
FILE_ERROR = 0x00000001, /*!< An error occured while opening/saving a file descriptor. */
INDEX_OUT_OF_RANGE = 0x00000002, /*!< The requested index was out of range. */
REDUNDANT_OPERATION = 0x00000003, /*!< The requested operation would be redundant. */
OVERFLOW_DETECTED = 0x00000004, /*!< The requested operation causes an overflow. */
INVALID_OPERATION = 0x00000005, /*!< The requested operation is invalid for the object. */
INVALID_DEPTH = 0x00000010, /*!< The supplied image bit depth was invalid. */
INVALID_OPERATION = 0x00000005, /*!< The requested operation is invalid for the object. */
INVALID_DEPTH = 0x00000010, /*!< The supplied image bit depth was invalid. */
COLORMAP_TOO_LARGE = 0x00000011, /*!< The resulting color map wouldn't save space and thus was not returned. */
CONTAINER_FULL = 0x00000100 /*!< The container is at max capacity and cannot have any new items added. */
CONTAINER_FULL = 0x00000100 /*!< The container is at max capacity and cannot have any new items added. */
};
}

Expand Down
24 changes: 12 additions & 12 deletions xTGA/include/xTGA/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace xtga
*/
enum class IMAGETYPE : uchar
{
NONE = 0x00, /*!< No image data present. */
COLOR_MAPPED = 0x01, /*!< Uncompressed Color Mapped. */
TRUE_COLOR = 0x02, /*!< Uncompressed True Color. */
GRAYSCALE = 0x03, /*!< Uncompressed Grayscale. */
NONE = 0x00, /*!< No image data present. */
COLOR_MAPPED = 0x01, /*!< Uncompressed Color Mapped. */
TRUE_COLOR = 0x02, /*!< Uncompressed True Color. */
GRAYSCALE = 0x03, /*!< Uncompressed Grayscale. */
COLOR_MAPPED_RLE = 0x09, /*!< Run-length encoded Color Mapped. */
TRUE_COLOR_RLE = 0x0A, /*!< Run-length encoded True Color. */
GRAYSCALE_RLE = 0x0B /*!< Run-length encoded Grayscale. */
GRAYSCALE_RLE = 0x0B /*!< Run-length encoded Grayscale. */
};

/**
Expand All @@ -38,9 +38,9 @@ namespace xtga
enum class IMAGEORIGIN : uchar
{
BOTTOM_LEFT = 0x00, /*!< First pixel goes in the bottom left. */
BOTTOM_RIGHT = 0x01, /*!< First pixel goes in the bottom right. */
TOP_LEFT = 0x02, /*!< First pixel goes in the top left. */
TOP_RIGHT = 0x03 /*!< First pixel goes in the top right. */
BOTTOM_RIGHT = 0x01, /*!< First pixel goes in the bottom right. */
TOP_LEFT = 0x02, /*!< First pixel goes in the top left. */
TOP_RIGHT = 0x03 /*!< First pixel goes in the top right. */
};

/**
Expand All @@ -49,11 +49,11 @@ namespace xtga
*/
enum class ALPHATYPE : uchar
{
NOALPHA = 0x00, /*!< There is no alpha in the image. */
NOALPHA = 0x00, /*!< There is no alpha in the image. */
UNDEFINED_ALPHA_IGNORE = 0x01, /*!< The data in the alpha channel is undefined and can be ignored. */
UNDEFINED_ALPHA_KEEP = 0x02, /*!< The data in the alpha channel is undefined but should be kept. */
STRAIGHT = 0x03, /*!< The data in the alpha channel is a valid straight alpha. */
PREMULTIPLIED = 0x04 /*!< The data in the alpha channel is a valid premultiplied alpha. */
UNDEFINED_ALPHA_KEEP = 0x02, /*!< The data in the alpha channel is undefined but should be kept. */
STRAIGHT = 0x03, /*!< The data in the alpha channel is a valid straight alpha. */
PREMULTIPLIED = 0x04 /*!< The data in the alpha channel is a valid premultiplied alpha. */
};
}
}
Expand Down
34 changes: 17 additions & 17 deletions xTGA/include/xTGA/marray.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,49 @@ namespace xtga

//----------------------------------------------------------------------------------------------------
/// Allocates a new ManagedArray of type 'T' with 'size' elements while taking ownership of an exiting array.
/// @tparam T The type of data the array contains.
/// @param[in] data The exiting array, the new object takes ownership of it and will handle deallocation.
/// @param[in] size The number of elements the array contains.
/// @tparam T The type of data the array contains.
/// @param[in] data The exiting array, the new object takes ownership of it and will handle deallocation.
/// @param[in] size The number of elements the array contains.
/// @return ManagedArray<T>* The created managed array.
//----------------------------------------------------------------------------------------------------
XTGAAPI static ManagedArray* Alloc(T* data, addressable size);

//----------------------------------------------------------------------------------------------------
/// Frees the given object.
/// @tparam T The type of data the array contains.
/// @param[in,out] obj The object to free.
/// @tparam T The type of data the array contains.
/// @param[in,out] obj The object to free.
//----------------------------------------------------------------------------------------------------
XTGAAPI static void Free(ManagedArray*& obj);

//----------------------------------------------------------------------------------------------------
/// Returns the element at 'index' [editable].
/// @tparam T The type of data the array contains.
/// @param[in] index The index of the element.
/// @param[out] error The error/status code. Can be nullptr.
/// @return T& The returned element [editable]. If index is out of range the first item is returned.
/// @tparam T The type of data the array contains.
/// @param[in] index The index of the element.
/// @param[out] error The error/status code. Can be nullptr.
/// @return T& The returned element [editable]. If index is out of range the first item is returned.
//----------------------------------------------------------------------------------------------------
XTGAAPI T& at(addressable index, ERRORCODE* error = nullptr);

//----------------------------------------------------------------------------------------------------
/// Identical to at(). However does not provide error checking.
/// @tparam T The type of data the array contains.
/// @param[in] index The index of the element.
/// @return T* The returned element [editable]. If index is out of range the first item is returned.
/// @tparam T The type of data the array contains.
/// @param[in] index The index of the element.
/// @return T* The returned element [editable]. If index is out of range the first item is returned.
//----------------------------------------------------------------------------------------------------
XTGAAPI T& operator[](addressable index);

//----------------------------------------------------------------------------------------------------
/// Returns the raw pointer [editable] at the given index.
/// @tparam T The type of data the array contains (does not effect behaviour in this case).
/// @param[in] index The index of the element.
/// @param[out] error The error/status code. Can be nullptr.
/// @return void* The data at the index.
/// @tparam T The type of data the array contains (does not effect behaviour in this case).
/// @param[in] index The index of the element.
/// @param[out] error The error/status code. Can be nullptr.
/// @return void* The data at the index.
//----------------------------------------------------------------------------------------------------
XTGAAPI void* rawat(addressable index, ERRORCODE* error = nullptr);

//----------------------------------------------------------------------------------------------------
/// Returns the size of the array.
/// @return addressable The size of the array.
/// @return addressable The size of the array.
//----------------------------------------------------------------------------------------------------
XTGAAPI addressable size() const;

Expand Down
6 changes: 3 additions & 3 deletions xTGA/include/xTGA/pixelformats.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace xtga
BGR565 = 0x03, /*!< A BGR pixel with 5-bits blue, 6-bits green, and 5-bits green. */
ARGB1555 = 0x04, /*!< An RGB pixel with 5-bits per primary and 1-bit alpha/attribute. */
BGRA5551 = 0x05, /*!< A BGR pixel with 5-bits per primary and 1-bit alpha/attribute. */
I8 = 0x06, /*!< A luminance (I) pixel with 8-bits. */
IA88 = 0x07, /*!< An IA pixel with 8-bit primary and 8-bit alpha. */
AI88 = 0x08, /*!< An AI pixel with 8-bit primary and 8-bit alpha. */
I8 = 0x06, /*!< A luminance (I) pixel with 8-bits. */
IA88 = 0x07, /*!< An IA pixel with 8-bit primary and 8-bit alpha. */
AI88 = 0x08, /*!< An AI pixel with 8-bit primary and 8-bit alpha. */
RGBA8888 = 0x09, /*!< An RGBA pixel with 8-bits per primary and 8-bit alpha. */
ABGR8888 = 0x0A, /*!< An ABGR pixel with 8-bits per primary and 8-bit alpha. */
ARGB8888 = 0x0B, /*!< An ARGB pixel with 8-bits per primary and 8-bit alpha. */
Expand Down
Loading