1
0
Pārlūkot izejas kodu

png export: added libpng dependency

Nicolas Winkler 5 gadi atpakaļ
vecāks
revīzija
1bf7ae0630

+ 9 - 2
Almond.cpp

@@ -4,6 +4,7 @@
 #include <QMessageBox>
 #include <QGradient>
 #include "gradientchoosedialog.h"
+#include "ImageExport.h"
 
 #include <cmath>
 
@@ -141,13 +142,19 @@ void Almond::on_exportImage_clicked()
         }
         mnd::MandelGenerator* currentGenerator = mw->getGenerator();
         mnd::MandelGenerator& g = currentGenerator ? *currentGenerator : mandelContext.getDefaultGenerator();
-        auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
+
+        alm::ImageExportInfo iei;
+        iei.drawInfo = mi;
+        iei.generator = &g;
+        iei.gradient = &mw->getGradient();
+        alm::exportPng(dialog.getPath().toStdString(), iei);
+        /*auto fmap = Bitmap<float>(mi.bWidth, mi.bHeight);
         g.generate(mi, fmap.pixels.get());
         auto bitmap = fmap.map<RGBColor>([&mi, this] (float i) {
             return i >= mi.maxIter ? RGBColor{ 0,0,0 } : mw->getGradient().get(i);
         });
         QImage img(reinterpret_cast<unsigned char*>(bitmap.pixels.get()), bitmap.width, bitmap.height, bitmap.width * 3, QImage::Format_RGB888);
-        img.save(dialog.getPath());
+        img.save(dialog.getPath());*/
     }
 }
 

+ 2 - 2
Almond.pro

@@ -125,7 +125,7 @@ unix|win32: LIBS += -L$FFMPEGPATH -lswscale
 
 RESOURCES += Almond.qrc
 
-unix|win32: LIBS += -L$$PWD/libmandel/ -L$$PWD/libalmond/ -lmandel -lqd -lasmjit -lalmond
+unix|win32: LIBS += -L$$PWD/libmandel/ -L$$PWD/libalmond/ -lmandel -lqd -lasmjit -lalmond -lpng
 unix: LIBS += -lrt
 
 INCLUDEPATH += $$PWD/libmandel/include $$PWD/libmandel/qd-2.3.22/include $$PWD/libalmond/include
@@ -134,7 +134,7 @@ INCLUDEPATH += $$PWD/libmandel/include $$PWD/libmandel/asmjit/src $$PWD/libalmon
 DEPENDPATH += $$PWD/libmandel/include $$PWD/libmandel/asmjit/stc $$PWD/libalmond/include
 
 win32:!win32-g++: PRE_TARGETDEPS += $$PWD/libmandel/asmjit.lib $$PWD/libmandel/mandel.lib $$PWD/libmandel/qd.lib $$PWD/libalmond/almond.lib
-else:unix|win32-g++: PRE_TARGETDEPS += $$PWD/libmandel/libmandel.a $$PWD/libmandel/libqd.a $$PWD/libmandel/libasmjit.a $$PWD/libalmond/almond.a
+else:unix|win32-g++: PRE_TARGETDEPS += $$PWD/libmandel/libmandel.a $$PWD/libmandel/libqd.a $$PWD/libmandel/libasmjit.a $$PWD/libalmond/libalmond.a
 
 
 win32:CONFIG(release, debug|release): LIBS += -L$$PWD/'../../../../../Program Files (x86)/OCL_SDK_Light/lib/x86_64/' -lopencl

+ 2 - 1
exportdialogs.cpp

@@ -54,7 +54,8 @@ void ExportImageDialog::on_pushButton_clicked()
 {
     QString saveAs = QFileDialog::getSaveFileName(this,
             tr("Save exported image"), "",
-            tr("PNG image (*.png);;JPEG image (*.jpg);;All Files (*)"));
+            //tr("PNG image (*.png);;JPEG image (*.jpg);;All Files (*)"));
+            tr("PNG image (*.png)"));
     if(!saveAs.isEmpty() && !saveAs.isNull())
         eid.savePath->setText(saveAs);
     this->repaint();

+ 3 - 1
libalmond/CMakeLists.txt

@@ -11,7 +11,7 @@ add_subdirectory(../libmandel ./libmandel)
 #set(Boost_DEBUG 1)
 set(Boost_USE_STATIC_LIBS ON)
 find_package(Boost 1.65 REQUIRED)
-
+find_package(PNG REQUIRED)
 
 set(CMAKE_CXX_STANDARD 17)
 
@@ -22,8 +22,10 @@ FILE(GLOB libalmondheaders include/*.h)
 add_library(libalmond STATIC ${libalmondsources})
 target_include_directories(libalmond PUBLIC "include")
 target_include_directories(libalmond SYSTEM PUBLIC ${FFMPEG_INCLUDE_DIRS})
+target_include_directories(libalmond SYSTEM PUBLIC ${PNG_INCLUDE_DIRS})
 target_link_libraries(libalmond PUBLIC mandel)
 target_link_libraries(libalmond PUBLIC ${FFMPEG_LIBRARIES})
+target_link_libraries(libalmond PUBLIC ${PNG_LIBRARIES})
 set_target_properties(libalmond PROPERTIES OUTPUT_NAME almond)
 
 

+ 23 - 0
libalmond/include/ImageExport.h

@@ -0,0 +1,23 @@
+#ifndef LIBALMOND_IMAGEEXPORT_H
+#define LIBALMOND_IMAGEEXPORT_H
+
+#include "Mandel.h"
+#include "Gradient.h"
+#include "../../libmandel/include/Mandel.h"
+
+namespace alm
+{
+    struct ImageExportInfo
+    {
+        mnd::MandelInfo drawInfo;
+        mnd::MandelGenerator* generator;
+        const Gradient* gradient;
+    };
+
+    void exportPng(const std::string& name, const ImageExportInfo& iei);
+}
+
+
+
+#endif // LIBALMOND_IMAGEEXPORT_H
+

+ 82 - 0
libalmond/src/ImageExport.cpp

@@ -0,0 +1,82 @@
+#include "ImageExport.h"
+#include "Bitmap.h"
+#include <png.h>
+#include <cstdio>
+
+namespace alm
+{
+
+
+void exportPng(const std::string& name, const ImageExportInfo& iei)
+{
+    if (iei.generator == nullptr) {
+        throw "no generator";
+    }
+    mnd::MandelGenerator& generator = *iei.generator;
+    FILE* file = fopen(name.c_str(), "wb");
+    if(!file) exit(1);
+
+    png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+    if (!png) exit(1);
+
+    png_infop info = png_create_info_struct(png);
+    if (!info) exit(1);
+
+    if (setjmp(png_jmpbuf(png))) exit(1);
+
+    png_init_io(png, file);
+
+    const long width = iei.drawInfo.bWidth;
+    const long height = iei.drawInfo.bHeight;
+    png_set_IHDR(
+        png,
+        info,
+        width, height,
+        8,
+        PNG_COLOR_TYPE_RGB,
+        PNG_INTERLACE_NONE,
+        PNG_COMPRESSION_TYPE_DEFAULT,
+        PNG_FILTER_TYPE_DEFAULT
+    );
+    png_write_info(png, info);
+
+    long chunkHeight = 512;
+    if (width <= 1024 && height <= 1024) {
+        chunkHeight = 1024;
+    }
+    if (width >= 4096) {
+        chunkHeight = 128;
+    }
+
+    auto rowPointers = std::make_unique<png_byte*[]>(chunkHeight);
+    for (long chunkY = 0; chunkY < height; chunkY += chunkHeight) {
+        auto minimum = [] (const auto& a, const auto& b) { return a < b ? a : b; };
+        long tmpHeight = minimum(chunkHeight, height - chunkY);
+        mnd::MandelInfo chunkInfo = iei.drawInfo;
+        chunkInfo.bHeight = tmpHeight;
+        chunkInfo.view.y += chunkInfo.view.height * chunkY / height;
+        chunkInfo.view.height *= mnd::Real(tmpHeight) / height;
+
+        Bitmap<float> chunk(width, tmpHeight);
+        generator.generate(chunkInfo, chunk.pixels.get());
+        Bitmap<RGBColor> coloredChunk = chunk.map<RGBColor>([&iei] (float i) {
+            return i >= iei.drawInfo.maxIter ? RGBColor{ 0, 0, 0 } : iei.gradient->get(i);
+        });
+        for (long i = 0; i < tmpHeight; i++) {
+            rowPointers[i] = reinterpret_cast<png_byte*>(&coloredChunk.get(0, i));
+        }
+        png_write_rows(png, &rowPointers[0], tmpHeight);
+    }
+
+    png_write_end(png, NULL);
+
+    fclose(file);
+
+    png_destroy_write_struct(&png, &info);
+}
+
+
+}
+
+
+

+ 1 - 1
libmandel/CMakeLists.txt

@@ -84,8 +84,8 @@ endif(WITH_ASMJIT)
 
 if(OPENCL_FOUND)
     target_compile_definitions(mandel PUBLIC WITH_OPENCL)
+    target_include_directories(mandel PUBLIC "include")
     target_include_directories(mandel SYSTEM PUBLIC
-        "include"
         ${OpenCL_INCLUDE_DIRS}
     )
     link_directories(${OpenCL_LIBRARY})

+ 19 - 2
mandelvid/src/main.cpp

@@ -1,4 +1,5 @@
 #include "MandelVideoGenerator.h"
+#include "ImageExport.h"
 #include "Gradient.h"
 #include "Mandel.h"
 #include "Fixed.h"
@@ -31,9 +32,25 @@ int main() {
 
     evi.start.adjustAspectRatio(evi.width, evi.height);
 
-    MandelVideoGenerator mvg(evi);
+    //MandelVideoGenerator mvg(evi);
+
+    //mvg.generate();
+    //
+    
+
+    mnd::MandelContext mc = mnd::initializeContext();
+    mnd::MandelInfo mi;
+    mi.view = evi.start;
+    mi.bWidth = 8000;
+    mi.bHeight = 8000;
+    mi.maxIter = 100;
+    mi.smooth = true;
+    alm::ImageExportInfo iei;
+    iei.drawInfo = mi;
+    iei.gradient = &evi.gradient;
+    iei.generator = &mc.getDefaultGenerator();
+    alm::exportPng("file.png", iei);
 
-    mvg.generate();
     return 0;
 }