Last active
March 8, 2021 08:55
-
-
Save playmer/9ec5cd78f8a599a8481548edbfb18637 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PNG_TO_DDS_RGBA_EXPORTER.cpp : This file contains the 'main' function. Program execution begins and ends there. | |
// | |
#include <iostream> | |
#include <fstream> | |
#include <cinttypes> | |
#include <vector> | |
#define STB_IMAGE_IMPLEMENTATION | |
#include "stb_image.h" | |
int main(int argv, char** argc) | |
{ | |
if (argv != 3) | |
{ | |
printf("Whoops, make sure you call with two arguments! Like so:\n"); | |
printf("PNG_TO_DDS_RGBA_EXPORTER.exe input.png output.dds"); | |
return 1; | |
} | |
std::string pngFile = argc[1]; | |
std::string ddsFile = argc[2]; | |
int w, h, components; | |
stbi_uc* bytes = stbi_load(pngFile.c_str(), &w, &h, &components, 4); | |
if (bytes == nullptr) | |
{ | |
printf("Couldn't load the input png file, incorrect format?"); | |
} | |
std::ofstream _out(ddsFile, std::ios::binary); | |
int _zero = 0x00; | |
int const _dataSize = w*h*4; | |
uint32_t _output[] = {0x7C, 0x02100F, static_cast<uint32_t>(h), static_cast<uint32_t>(w), 0x800, 0x01, 0x01}; | |
uint32_t _output2[] = {0x20, 0x41, 0x00, 0x20, 0xFF, 0xFF00, 0xFF0000, 0xFF000000, 0x1000}; | |
std::vector<stbi_uc> _imgData; | |
_imgData.resize(_dataSize); | |
memcpy(_imgData.data(), bytes, _dataSize); | |
free(bytes); | |
for (int i = 3; i < _dataSize; i += 4) | |
{ | |
unsigned char const _factor = 2; | |
int _value = std::min(_imgData.at(i) * _factor, 255); | |
_imgData.at(i) = static_cast<unsigned char>(_value); | |
} | |
_out << "DDS "; | |
_out.write(reinterpret_cast<char*>(&_output), 4 * 7); | |
for (int o = 0; o < 11; o++) | |
_out.write(reinterpret_cast<char*>(&_zero), 4); | |
_out.write(reinterpret_cast<char*>(&_output2), 4 * 9); | |
for (int o = 0; o < 4; o++) | |
_out.write(reinterpret_cast<char*>(&_zero), 4); | |
_out.write(reinterpret_cast<char*>(_imgData.data()), _dataSize); | |
_out.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment