From 7c7027502e2e876834154fa0d7b820465277b77d Mon Sep 17 00:00:00 2001 From: omigamedev Date: Fri, 21 Sep 2018 01:53:22 +0200 Subject: [PATCH] fix thumbnail generator for iOS and open document when file clicked --- PanoPainter/AppDelegate.m | 2 +- PanoPainter/Info.plist | 6 +-- PanoThumb/ThumbnailProvider.m | 73 ++++++++++++++++++++++++++++++++--- src/app.cpp | 20 ++++++++++ src/app.h | 1 + src/app_dialogs.cpp | 16 +------- 6 files changed, 94 insertions(+), 24 deletions(-) diff --git a/PanoPainter/AppDelegate.m b/PanoPainter/AppDelegate.m index e324913..fc5f29d 100644 --- a/PanoPainter/AppDelegate.m +++ b/PanoPainter/AppDelegate.m @@ -38,7 +38,7 @@ void global_signal_handler(int e) - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options { - App::I.canvas->m_canvas->project_open([url fileSystemRepresentation]); + App::I.open_document([url fileSystemRepresentation]); return true; } diff --git a/PanoPainter/Info.plist b/PanoPainter/Info.plist index 89623d7..144f926 100644 --- a/PanoPainter/Info.plist +++ b/PanoPainter/Info.plist @@ -39,10 +39,10 @@ LSSupportsOpeningDocumentsInPlace - NSPhotoLibraryUsageDescription - Export the panoramic to the gallery NSMicrophoneUsageDescription Use the audio jack for the SonarPen + NSPhotoLibraryUsageDescription + Export the panoramic to the gallery UIFileSharingEnabled UILaunchStoryboardName @@ -86,7 +86,7 @@ public.filename-extension - pano + ppi public.mime-type image/ppi diff --git a/PanoThumb/ThumbnailProvider.m b/PanoThumb/ThumbnailProvider.m index 1fb93ec..26c1134 100644 --- a/PanoThumb/ThumbnailProvider.m +++ b/PanoThumb/ThumbnailProvider.m @@ -8,6 +8,54 @@ #import "ThumbnailProvider.h" + +struct PPIThumb +{ + int width; + int height; + int comp; + /* + bool valid() + { + return (width == 128 && height == 128 && comp == 4); + } + */ +}; + +struct PPIDocVersion +{ + int major; + int minor; +}; + +struct PPISoftVersion +{ + int major; + int minor; + int fix; + int build; +}; + +struct PPIHeader +{ + char magic[4]; + struct PPIDocVersion doc_version; + struct PPISoftVersion soft_version; + struct PPIThumb thumb_header; + /* + bool valid() + { + if (strcmp(magic, "PPI") != 0) + return false; + if (doc_version.major != 0 || doc_version.minor != 1) + return false; + if (!thumb_header.valid()) + return false; + return true; + } + */ +}; + @implementation ThumbnailProvider - (void)provideThumbnailForFileRequest:(QLFileThumbnailRequest *)request completionHandler:(void (^)(QLThumbnailReply * _Nullable, NSError * _Nullable))handler { @@ -35,15 +83,30 @@ FILE* fp = fopen([u fileSystemRepresentation], "rb"); if (fp) { - int h[3]; - fread(h, sizeof(int), 3, fp); + struct PPIHeader header; + fread(&header, sizeof(struct PPIHeader), 1, fp); + int w = header.thumb_header.width; + int h = header.thumb_header.height; + int c = header.thumb_header.comp; //uint8_t* data = (uint8_t*)malloc(h[0]*h[1]*h[2]); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - CGContextRef bmp = CGBitmapContextCreate(NULL, h[0], h[1], 8, h[0]*h[2], colorSpace, kCGImageAlphaPremultipliedLast); - uint8_t* data = CGBitmapContextGetData(bmp); - fread(data, h[0]*h[1]*h[2], 1, fp); + CGContextRef bmp = CGBitmapContextCreate(NULL, w, h, 8, w*c, colorSpace, kCGImageAlphaPremultipliedLast); + + uint8_t* data_dst = (uint8_t*)CGBitmapContextGetData(bmp); + uint8_t* data_src = (uint8_t*)malloc(w*h*c); + fread(data_src, w*h*c, 1, fp); fclose(fp); + + for(int i = 0; i < w*h; i++) + { + data_dst[i * 4 + 0] = data_src[i * c + 0]; + data_dst[i * 4 + 1] = data_src[i * c + 1]; + data_dst[i * 4 + 2] = data_src[i * c + 2]; + data_dst[i * 4 + 3] = 255; + } + + free(data_src); CGImageRef img = CGBitmapContextCreateImage(bmp); CGRect r2 = CGRectMake(0, 0, 128*request.scale, 128*request.scale); diff --git a/src/app.cpp b/src/app.cpp index d9a289b..e162315 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -31,6 +31,26 @@ void App::create() height = 1080/2; } +void App::open_document(std::string path) +{ + auto start = path.rfind("/") + 1; + doc_name = path.substr(start, path.length() - start - strlen(".ppi")); + canvas->reset_camera(); + layers->clear(); + canvas->m_canvas->project_open(path, [this] { + // on complete + async_start(); + title_update(); + for (auto& i : canvas->m_canvas->m_order) + { + auto* l = layers->add_layer(canvas->m_canvas->m_layers[i].m_name.c_str()); + l->m_opacity->m_value.x = canvas->m_canvas->m_layers[i].m_opacity; + } + async_end(); + }); + ActionManager::clear(); +} + bool App::request_close() { static bool dialog_already_opened = false; diff --git a/src/app.h b/src/app.h index 05b37be..969b9b9 100644 --- a/src/app.h +++ b/src/app.h @@ -102,6 +102,7 @@ public: void initShaders(); void initAssets(); void initLayout(); + void open_document(std::string path); void create(); bool request_close(); void terminate(); diff --git a/src/app_dialogs.cpp b/src/app_dialogs.cpp index 78732dc..5aa8bfa 100644 --- a/src/app_dialogs.cpp +++ b/src/app_dialogs.cpp @@ -192,22 +192,8 @@ void App::dialog_browse() dialog->btn_ok->on_click = [this, dialog](Node*) { - canvas->reset_camera(); - layers->clear(); - doc_name = dialog->selected_name; - canvas->m_canvas->project_open(dialog->selected_path, [this] { - // on complete - async_start(); - title_update(); - for (auto& i : canvas->m_canvas->m_order) - { - auto* l = layers->add_layer(canvas->m_canvas->m_layers[i].m_name.c_str()); - l->m_opacity->m_value.x = canvas->m_canvas->m_layers[i].m_opacity; - } - async_end(); - }); + open_document(dialog->selected_path); dialog->destroy(); - ActionManager::clear(); }; async_end(); };