diff options
Diffstat (limited to 'Source/Images/image_export.cpp')
-rw-r--r-- | Source/Images/image_export.cpp | 142 |
1 files changed, 70 insertions, 72 deletions
diff --git a/Source/Images/image_export.cpp b/Source/Images/image_export.cpp index b136549f..55f2c03e 100644 --- a/Source/Images/image_export.cpp +++ b/Source/Images/image_export.cpp @@ -45,67 +45,65 @@ #include <algorithm> #include <cstdio> -using std::min; using std::max; +using std::min; namespace { - void RGBAtoYCOCG(unsigned char *data, unsigned long width, unsigned long height) { - int index = 0; - for(unsigned i=0; i<width*height; i++){ - const int r = data[index+2]; - const int g = data[index+1]; - const int b = data[index+0]; - - const int Co = r - b; - const int t = b + Co/2; - const int Cg = g - t; - const int Y = t + Cg/2; - - data[index+2] = min(max(Co + 128, 0), 255); - data[index+1] = min(max(Cg + 128, 0), 255); - data[index+0] = 0; - data[index+3] = Y; - index+=4; - } +void RGBAtoYCOCG(unsigned char *data, unsigned long width, unsigned long height) { + int index = 0; + for (unsigned i = 0; i < width * height; i++) { + const int r = data[index + 2]; + const int g = data[index + 1]; + const int b = data[index + 0]; + + const int Co = r - b; + const int t = b + Co / 2; + const int Cg = g - t; + const int Y = t + Cg / 2; + + data[index + 2] = min(max(Co + 128, 0), 255); + data[index + 1] = min(max(Cg + 128, 0), 255); + data[index + 0] = 0; + data[index + 3] = Y; + index += 4; } } +} // namespace -std::string ImageExport::FindEmptySequentialFile(const char* filename, const char* suffix) { +std::string ImageExport::FindEmptySequentialFile(const char *filename, const char *suffix) { char abs_path[kPathSize]; - for(int i=1; i<99999; i++){ - FormatString(abs_path, kPathSize,"%s%s%05d%s", GetWritePath(CoreGameModID).c_str(), filename, i, suffix); - if(!CheckFileAccess(abs_path)) { + for (int i = 1; i < 99999; i++) { + FormatString(abs_path, kPathSize, "%s%s%05d%s", GetWritePath(CoreGameModID).c_str(), filename, i, suffix); + if (!CheckFileAccess(abs_path)) { return abs_path; } - } + } return filename; } +void ImageExport::ScaleImageUp(const unsigned char *data, + int levels, + unsigned long *width_ptr, + unsigned long *height_ptr, + std::vector<unsigned char> *new_data_ptr) { + unsigned long &width = *width_ptr; + unsigned long &height = *height_ptr; + std::vector<unsigned char> &new_data = *new_data_ptr; -void ImageExport::ScaleImageUp(const unsigned char *data, - int levels, - unsigned long *width_ptr, - unsigned long *height_ptr, - std::vector<unsigned char> *new_data_ptr) -{ - unsigned long& width = *width_ptr; - unsigned long& height = *height_ptr; - std::vector<unsigned char> &new_data = *new_data_ptr; - const int bytes_per_pixel = 4; - size_t image_size = width*height*bytes_per_pixel; + size_t image_size = width * height * bytes_per_pixel; new_data.resize(image_size); memcpy(&(new_data[0]), data, image_size); - for(int i=0; i<levels; i++){ + for (int i = 0; i < levels; i++) { width *= 2; height *= 2; - image_size = width*height*bytes_per_pixel; + image_size = width * height * bytes_per_pixel; std::vector<unsigned char> new_new_data(image_size); - for(unsigned i=0; i<width; i++){ - for(unsigned j=0; j<height; j++){ - for(int k=0; k<bytes_per_pixel; k++){ - new_new_data[(i+j*width)*bytes_per_pixel+k] = new_data[((i/2)+(j/2)*width/2)*bytes_per_pixel+k]; + for (unsigned i = 0; i < width; i++) { + for (unsigned j = 0; j < height; j++) { + for (int k = 0; k < bytes_per_pixel; k++) { + new_new_data[(i + j * width) * bytes_per_pixel + k] = new_data[((i / 2) + (j / 2) * width / 2) * bytes_per_pixel + k]; } } } @@ -117,18 +115,18 @@ void ImageExport::SavePNG(const char *file_path, unsigned char *data, unsigned l std::vector<unsigned char> scaled_data; ImageExport::ScaleImageUp(data, levels, &width, &height, &scaled_data); FREE_IMAGE_FORMAT format = FIF_PNG; - FIBITMAP *image = FreeImage_ConvertFromRawBits(&scaled_data[0], width, height, 4*width, 32, 0xC0, 0x38, 0x7); + FIBITMAP *image = FreeImage_ConvertFromRawBits(&scaled_data[0], width, height, 4 * width, 32, 0xC0, 0x38, 0x7); CreateParentDirs(file_path); #ifdef _WIN32 createfile(file_path); std::string short_path(file_path); ShortenWindowsPath(short_path); - if(!FreeImage_Save(format, image, short_path.c_str())) { - DisplayError("Error","Problem exporting .png file"); + if (!FreeImage_Save(format, image, short_path.c_str())) { + DisplayError("Error", "Problem exporting .png file"); } #else - if(!FreeImage_Save(format, image, file_path)) { - DisplayError("Error","Problem exporting .png file"); + if (!FreeImage_Save(format, image, file_path)) { + DisplayError("Error", "Problem exporting .png file"); } #endif FreeImage_Unload(image); @@ -138,21 +136,21 @@ void ImageExport::SavePNGTransparent(const char *file_path, unsigned char *data, std::vector<unsigned char> scaled_data; ImageExport::ScaleImageUp(data, levels, &width, &height, &scaled_data); FREE_IMAGE_FORMAT format = FIF_PNG; - FIBITMAP *image = FreeImage_ConvertFromRawBits(&scaled_data[0], width, height, 4*width, 32, 0xC0, 0x38, 0x7); + FIBITMAP *image = FreeImage_ConvertFromRawBits(&scaled_data[0], width, height, 4 * width, 32, 0xC0, 0x38, 0x7); LOGI << "BPP: " << (int)FreeImage_GetBPP(image) << std::endl; - LOGI << "IsTransparent: " << (int)FreeImage_IsTransparent(image) << std::endl; + LOGI << "IsTransparent: " << (int)FreeImage_IsTransparent(image) << std::endl; FreeImage_SetTransparent(image, true); LOGI << "IsTransparent: " << (int)FreeImage_IsTransparent(image) << std::endl; int bytespp = FreeImage_GetLine(image) / FreeImage_GetWidth(image); - for(unsigned y = 0; y < FreeImage_GetHeight(image); y++) { + for (unsigned y = 0; y < FreeImage_GetHeight(image); y++) { BYTE *bits = FreeImage_GetScanLine(image, y); - for(unsigned x = 0; x < FreeImage_GetWidth(image); x++) { + for (unsigned x = 0; x < FreeImage_GetWidth(image); x++) { // Set pixel color to green with a transparency of 128 - bits[FI_RGBA_RED] = min(255,(int)((bits[FI_RGBA_RED]/255.0f / (scaled_data[y*width*4+x*4+3]/255.0f))*255)); - bits[FI_RGBA_GREEN] = min(255,(int)((bits[FI_RGBA_GREEN]/255.0f / (scaled_data[y*width*4+x*4+3]/255.0f))*255)); - bits[FI_RGBA_BLUE] = min(255,(int)((bits[FI_RGBA_BLUE]/255.0f / (scaled_data[y*width*4+x*4+3]/255.0f))*255)); - bits[FI_RGBA_ALPHA] = scaled_data[y*width*4+x*4+3]; + bits[FI_RGBA_RED] = min(255, (int)((bits[FI_RGBA_RED] / 255.0f / (scaled_data[y * width * 4 + x * 4 + 3] / 255.0f)) * 255)); + bits[FI_RGBA_GREEN] = min(255, (int)((bits[FI_RGBA_GREEN] / 255.0f / (scaled_data[y * width * 4 + x * 4 + 3] / 255.0f)) * 255)); + bits[FI_RGBA_BLUE] = min(255, (int)((bits[FI_RGBA_BLUE] / 255.0f / (scaled_data[y * width * 4 + x * 4 + 3] / 255.0f)) * 255)); + bits[FI_RGBA_ALPHA] = scaled_data[y * width * 4 + x * 4 + 3]; // jump to next pixel bits += bytespp; } @@ -163,45 +161,45 @@ void ImageExport::SavePNGTransparent(const char *file_path, unsigned char *data, createfile(file_path); std::string short_path(file_path); ShortenWindowsPath(short_path); - if(!FreeImage_Save(format, image, short_path.c_str())) { - DisplayError("Error","Problem exporting .png file"); + if (!FreeImage_Save(format, image, short_path.c_str())) { + DisplayError("Error", "Problem exporting .png file"); } #else - if(!FreeImage_Save(format, image, file_path)) { - DisplayError("Error","Problem exporting .png file"); + if (!FreeImage_Save(format, image, file_path)) { + DisplayError("Error", "Problem exporting .png file"); } #endif FreeImage_Unload(image); } -void ImageExport::SaveJPEG(const char* abs_path, unsigned char *data, unsigned long width, unsigned long height) { +void ImageExport::SaveJPEG(const char *abs_path, unsigned char *data, unsigned long width, unsigned long height) { FREE_IMAGE_FORMAT format = FIF_JPEG; - FIBITMAP *image = FreeImage_ConvertFromRawBits(data, width, height, 3*width, 24, 0xC0, 0x38, 0x7); + FIBITMAP *image = FreeImage_ConvertFromRawBits(data, width, height, 3 * width, 24, 0xC0, 0x38, 0x7); CreateParentDirs(abs_path); #ifdef _WIN32 createfile(abs_path); std::string short_path(abs_path); ShortenWindowsPath(short_path); - if(!FreeImage_Save(format, image, short_path.c_str(), JPEG_QUALITYSUPERB)) { - DisplayError("Error","Problem exporting .jpeg file"); + if (!FreeImage_Save(format, image, short_path.c_str(), JPEG_QUALITYSUPERB)) { + DisplayError("Error", "Problem exporting .jpeg file"); } #else - if(!FreeImage_Save(format, image, abs_path, JPEG_QUALITYSUPERB)) { - DisplayError("Error","Problem exporting .jpeg file"); + if (!FreeImage_Save(format, image, abs_path, JPEG_QUALITYSUPERB)) { + DisplayError("Error", "Problem exporting .jpeg file"); } #endif FreeImage_Unload(image); } -void ImageExport::SavePNGofGrayscaleFloats(const char *file_path, std::vector<float>& data, unsigned long width, unsigned long height) { - if (data.size() != width*height) return; - unsigned char* byte_data = (unsigned char*)(OG_MALLOC(sizeof(*byte_data)*data.size()*4)); +void ImageExport::SavePNGofGrayscaleFloats(const char *file_path, std::vector<float> &data, unsigned long width, unsigned long height) { + if (data.size() != width * height) return; + unsigned char *byte_data = (unsigned char *)(OG_MALLOC(sizeof(*byte_data) * data.size() * 4)); for (unsigned i = 0; i < data.size(); i++) { - unsigned char data_i = (unsigned char)(data[i]*255); - byte_data[4*i] = data_i; - byte_data[4*i+1] = data_i; - byte_data[4*i+2] = data_i; - byte_data[4*i+3] = 255; + unsigned char data_i = (unsigned char)(data[i] * 255); + byte_data[4 * i] = data_i; + byte_data[4 * i + 1] = data_i; + byte_data[4 * i + 2] = data_i; + byte_data[4 * i + 3] = 255; } SavePNG(file_path, byte_data, width, height); OG_FREE(byte_data); |