Last active
June 5, 2018 00:31
-
-
Save Ghabry/92cd9a379e102f6d7c0ca54281ba4d95 to your computer and use it in GitHub Desktop.
xyzimage example
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
--- xyzcrush.cpp 2018-01-27 02:08:27.431825844 +0100 | |
+++ xyzcrush_new.cpp 2018-01-27 02:03:22.000000000 +0100 | |
@@ -25,6 +25,7 @@ | |
# include <algorithm> | |
#endif | |
#include "zopfli/zlib_container.h" | |
+#include "../../include/xyzimage.h" | |
# ifdef __MINGW64_VERSION_MAJOR | |
int _dowildcard = -1; /* enable wildcard expansion for mingw-w64 */ | |
@@ -73,8 +74,33 @@ | |
return s; | |
} | |
+ZopfliOptions zopfli_options; | |
+ | |
+static size_t zopfli_compress_func(void* buffer_in, size_t len_in, void* buffer_out, size_t len_out, xyzimage_error_t* error) { | |
+ // buffer_in, buffer_out, len_in and len_out verified by caller | |
+ | |
+ size_t comp_size = 0; | |
+ unsigned char* comp_data = NULL; | |
+ | |
+ ZopfliZlibCompress( | |
+ &zopfli_options, (const unsigned char*)buffer_in, len_in, &comp_data, &comp_size); | |
+ | |
+ if (comp_size > len_out) { | |
+ free(comp_data); | |
+ if (error) { | |
+ *error = XYZIMAGE_ERROR_BUFFER_TOO_SMALL; | |
+ } | |
+ return 0; | |
+ } | |
+ | |
+ memcpy(buffer_out, comp_data, comp_size); | |
+ | |
+ free(comp_data); | |
+ | |
+ return comp_size; | |
+} | |
+ | |
int main(int argc, char* argv[]) { | |
- ZopfliOptions zopfli_options; | |
ZopfliInitOptions(&zopfli_options); | |
zopfli_options.verbose = 0; | |
zopfli_options.verbose_more = 0; | |
@@ -93,8 +119,7 @@ | |
char header[5]; | |
for (int arg = 1; arg < argc; arg++) { | |
- std::ifstream file(argv[arg], | |
- std::ios::binary | std::ios::ate); | |
+ FILE* file = fopen(argv[arg], "rb"); | |
if (!file) { | |
std::cerr << "Error reading file " << argv[arg] << "." | |
<< std::endl; | |
@@ -102,65 +127,49 @@ | |
continue; | |
} | |
- long size = file.tellg(); | |
- header[4] = '\0'; | |
+ xyzimage_error_t error_code = XYZIMAGE_ERROR_OK; | |
+ XYZImage* img = xyzimage_fopen(file, &error_code); | |
- file.seekg(0, std::ios::beg); | |
- file.read(header, 4); | |
+ fclose(file); | |
- if (memcmp(header, "XYZ1", 4) != 0) { | |
+ if (error_code != XYZIMAGE_ERROR_OK) { | |
std::cerr << "Input file " << argv[arg] | |
- << " is not an XYZ file: '" << header << "'." << std::endl; | |
+ << " is not an XYZ file" << std::endl; | |
errors++; | |
continue; | |
} | |
- unsigned short width; | |
- unsigned short height; | |
- file.read((char*) &width, 2); | |
- file.read((char*) &height, 2); | |
- | |
- int compressed_xyz_size = size - 8; | |
- Bytef* compressed_xyz_data = new Bytef[compressed_xyz_size]; | |
- | |
- file.read((char*) compressed_xyz_data, compressed_xyz_size); | |
- | |
- uLongf xyz_size = 768 + (width * height); | |
- Bytef* xyz_data = new Bytef[xyz_size]; | |
+ // Compress XYZ data | |
+ xyzimage_set_compress_func(img, zopfli_compress_func); | |
- int status = uncompress(xyz_data, &xyz_size, compressed_xyz_data, | |
- compressed_xyz_size); | |
+ std::stringstream ss; | |
+ ss << GetFilename(argv[arg]) + std::string(".xyz"); | |
+ std::string xyz_filename = ss.str(); | |
- if (status != Z_OK) { | |
- std::cerr << "XYZ error in file " << argv[arg] << "." << std::endl; | |
+ FILE* xyz_file = fopen(xyz_filename.c_str(), "wb"); | |
+ if (!xyz_file) { | |
+ std::cerr << "Error writing file " << argv[arg] << "." | |
+ << std::endl; | |
errors++; | |
continue; | |
} | |
- delete[] compressed_xyz_data; | |
- // Compress XYZ data | |
- size_t comp_size = 0; | |
- unsigned char* comp_data = 0; | |
+ xyzimage_fwrite(img, xyz_file, &error_code); | |
- ZopfliZlibCompress(&zopfli_options, xyz_data, xyz_size, &comp_data, | |
- &comp_size); | |
+ fclose(xyz_file); | |
- delete[] xyz_data; | |
+ if (error_code != XYZIMAGE_ERROR_OK) { | |
+ std::cerr << "Error writing file " << argv[arg] << "." | |
+ << std::endl; | |
+ errors++; | |
+ continue; | |
+ } | |
- std::stringstream ss; | |
- ss << GetFilename(argv[arg]) + std::string(".xyz"); | |
- std::string xyz_filename = ss.str(); | |
- std::ofstream xyz_file(xyz_filename.c_str(), std::ofstream::binary); | |
- xyz_file.write("XYZ1", 4); | |
- xyz_file.write(reinterpret_cast<char*>(&width), 2); | |
- xyz_file.write(reinterpret_cast<char*>(&height), 2); | |
- xyz_file.write(reinterpret_cast<char*>(comp_data), comp_size); | |
- xyz_file.close(); | |
- delete[] comp_data; | |
+ xyzimage_free(img); | |
- std::cout << "Input file " << argv[arg] << ": " << size << "->" | |
+ /*std::cout << "Input file " << argv[arg] << ": " << size << "->" | |
<< comp_size + 8 << " (" << (comp_size + 8) * 100 / size << "%)" | |
- << std::endl; | |
+ << std::endl;*/ | |
} | |
if (errors > 0) { |
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
#include <stdio.h> | |
#include "xyzimage.h" | |
int main() { | |
xyzimage_error_t error_code = XYZIMAGE_ERROR_OK; | |
FILE* f = fopen("WHATEVER.xyz", "rb"); | |
XYZImage* img = xyzimage_fopen(f, &error_code); | |
if (error_code != XYZIMAGE_ERROR_OK) { | |
return 1; | |
} | |
fclose(f); | |
f = fopen("/tmp/out.xyz", "wb"); | |
xyzimage_fwrite(img, f, &error_code); | |
if (error_code != XYZIMAGE_ERROR_OK) { | |
return 1; | |
} | |
xyzimage_free(img); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment