aboutsummaryrefslogtreecommitdiff
path: root/src/ImageUtils.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-09-17 19:20:36 +0200
committerdec05eba <dec05eba@protonmail.com>2020-09-17 19:27:00 +0200
commit3b21eda9ad3b2ece9c6e5472eb419fb4d88424bd (patch)
tree033558bb30d6ca9e580a59696e330e4e6bc1274a /src/ImageUtils.cpp
parent5e7215b4675955fee8197076914599fe62f39c26 (diff)
Add image upscaling with waifu2x-ncnn-vulkan, async load images in scroll image view mode
Diffstat (limited to 'src/ImageUtils.cpp')
-rw-r--r--src/ImageUtils.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/ImageUtils.cpp b/src/ImageUtils.cpp
new file mode 100644
index 0000000..d160450
--- /dev/null
+++ b/src/ImageUtils.cpp
@@ -0,0 +1,114 @@
+#include "../include/ImageUtils.hpp"
+#include <string.h>
+#include <arpa/inet.h>
+
+namespace QuickMedia {
+ static bool gif_get_size(unsigned char *data, size_t data_size, int *width, int *height) {
+ if(data_size >= 10 && memcmp(data, "GIF", 3) == 0) {
+ *width = ((int)data[7] << 8) | data[6];
+ *height = ((int)data[9] << 8) | data[8];
+ return true;
+ }
+ return false;
+ }
+
+ static bool png_get_size(unsigned char *data, size_t data_size, int *width, int *height) {
+ if(data_size >= 24 && memcmp(data, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0) {
+ memcpy(width, data + 16, sizeof(int));
+ memcpy(height, data + 16 + sizeof(int), sizeof(int));
+ *width = htonl(*width);
+ *height = htonl(*height);
+ return true;
+ }
+ return false;
+ }
+
+ static bool is_cpu_little_endian() {
+ unsigned short i;
+ memcpy(&i, "LE", sizeof(i));
+ return (i & 0xF) == 'E';
+ }
+
+ static unsigned short read_uint16(unsigned char *data, bool cpu_little_endian, bool file_little_endian) {
+ unsigned short result;
+ memcpy(&result, data, sizeof(result));
+ if(cpu_little_endian != file_little_endian)
+ result = __builtin_bswap16(result);
+ return result;
+ }
+
+ static unsigned int read_uint32(unsigned char *data, bool cpu_little_endian, bool file_little_endian) {
+ unsigned int result;
+ memcpy(&result, data, sizeof(result));
+ if(cpu_little_endian != file_little_endian)
+ result = __builtin_bswap32(result);
+ return result;
+ }
+#if 0
+ static bool tiff_get_size(unsigned char *data, size_t data_size, int *width, int *height) {
+ if(data_size < 8)
+ return false;
+
+ bool cpu_little_endian = is_cpu_little_endian();
+ bool file_little_endian = true;
+ if(memcmp(data, "II", 2) == 0)
+ file_little_endian = true;
+ else if(memcmp(data, "MM", 2) == 0)
+ file_little_endian = false;
+ else
+ return false;
+
+ unsigned short id = read_uint16(data, cpu_little_endian, file_little_endian);
+ if(id != 42)
+ return false;
+
+ unsigned int offset_to_ifd = read_uint32(data, cpu_little_endian, file_little_endian);
+ if(offset_to_ifd )
+ }
+#endif
+
+ static bool jpeg_get_size(unsigned char *data, size_t data_size, int *width, int *height) {
+ if(data_size < 11 || memcmp(data, "\xFF\xD8\xFF\xE0", 4) != 0)
+ return false;
+
+ unsigned short block_length = ((int)data[4] << 8) | data[5];
+ if(memcmp(data + 6, "JFIF\0", 5) != 0)
+ return false;
+
+ size_t index = 4;
+ while(index < data_size) {
+ index += block_length;
+ if(index + 1 >= data_size) return false;
+ if(data[index] != 0xFF) return false; // Check if start of block
+ if((data[index + 1] == 0xC0 || data[index + 1] == 0xC2) && index + 8 < data_size) { // Start of frame marker
+ *height = ((int)data[index + 5] << 8) | data[index + 6];
+ *width = ((int)data[index + 7] << 8) | data[index + 8];
+ return true;
+ } else {
+ if(index + 2 >= data_size) return false;
+ index += 2;
+ block_length = ((int)data[index] << 8) | data[index + 1];
+ }
+ }
+
+ return false;
+ }
+
+ bool image_get_resolution(const Path &path, int *width, int *height) {
+ FILE *file = fopen(path.data.c_str(), "rb");
+ if(!file)
+ return false;
+
+ unsigned char data[512];
+ size_t bytes_read = fread(data, 1, sizeof(data), file);
+ fclose(file);
+
+ if(png_get_size(data, bytes_read, width, height))
+ return true;
+ else if(gif_get_size(data, bytes_read, width, height))
+ return true;
+ else if(jpeg_get_size(data, bytes_read, width, height))
+ return true;
+ return false;
+ }
+} \ No newline at end of file