Commit 683119a7 authored by Felix Morgner's avatar Felix Morgner
Browse files

week12: add exercises

parent b58261fa
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/jna-5.5.0.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>w12_solution_01_JuliaJNA</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
package ch.hsr.cpla;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
public interface Julia extends Library {
Julia INSTANCE = (Julia) Native.load("julia", Julia.class);
public static class Window extends Structure implements Structure.ByValue {
public double x, y, size;
public Window(double x, double y, double size) {
this.x = x;
this.y = y;
this.size = size;
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "x", "y", "size" });
}
}
public static class Image extends Pointer {
public Image(int size) {
super(Pointer.nativeValue(INSTANCE.createImage(size)));
}
public void dispose() {
INSTANCE.disposeImage(this);
}
public int getSize() {
return INSTANCE.getSize(this);
}
public byte[] getBitmapData() {
IntByReference size = new IntByReference();
Pointer dumped = INSTANCE.dump(this, size);
byte[] data = dumped.getByteArray(0, size.getValue());
Native.free(Pointer.nativeValue(dumped));
return data;
}
}
Pointer createImage(int size);
void disposeImage(Image image);
int getSize(Image image);
Pointer dump(Image image, IntByReference size);
void render(Image image, Window window, double exponent, double kReal, double kImaginary, int iterations);
}
package ch.hsr.cpla;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import ch.hsr.cpla.Julia.Image;
public class JuliaJNA {
static ImageViewer imageViewer;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
imageViewer = new ImageViewer(500);
imageViewer.setVisible(true);
});
}
public static class ImageViewer extends JFrame {
private static final long serialVersionUID = -9127187659667855390L;
public ImageViewer(int size) {
this.setTitle("CplA Image Viewer");
this.setSize(size, size);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image image = new Julia.Image(size);
Julia.INSTANCE.render(image, new Julia.Window(0.0, 0.0, 1.0), 2.0, 0.0, -1.0, 100);
byte[] rendered = image.getBitmapData();
image.dispose();
BufferedImage bitmap = null;
try {
bitmap = ImageIO.read(new ByteArrayInputStream(rendered));
} catch (IOException e) {
e.printStackTrace();
}
JPanel panel = new JPanel();
if (image != null) {
JLabel imageView = new JLabel(new ImageIcon(bitmap));
panel.add(imageView);
}
this.add(panel);
}
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="cdt.managedbuild.config.gnu.mingw.so.debug.1055223311">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.mingw.so.debug.1055223311" moduleId="org.eclipse.cdt.core.settings" name="Debug">
<externalSettings>
<externalSetting>
<entry flags="VALUE_WORKSPACE_PATH" kind="includePath" name="/w12_solution_01_Julia"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="libraryPath" name="/w12_solution_01_Julia/Debug"/>
<entry flags="RESOLVED" kind="libraryFile" name="julia" srcPrefixMapping="" srcRootPath=""/>
</externalSetting>
</externalSettings>
<extensions>
<extension id="org.eclipse.cdt.core.PE64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.MachO64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.XCOFF32" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.Cygwin_PE64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GNU_ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.SOM" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.Cygwin_PE" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.MachO" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.PE" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactExtension="dll" artifactName="julia" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.sharedLib" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.sharedLib,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.mingw.so.debug.1055223311" name="Debug" parent="cdt.managedbuild.config.gnu.mingw.so.debug">
<folderInfo id="cdt.managedbuild.config.gnu.mingw.so.debug.1055223311." name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.mingw.base.1940038799" name="MinGW GCC" superClass="cdt.managedbuild.toolchain.gnu.mingw.base">
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.PE64;org.eclipse.cdt.core.MachO64;org.eclipse.cdt.core.XCOFF32;org.eclipse.cdt.core.Cygwin_PE64;org.eclipse.cdt.core.ELF;org.eclipse.cdt.core.GNU_ELF;org.eclipse.cdt.core.SOM;org.eclipse.cdt.core.Cygwin_PE;org.eclipse.cdt.core.MachO;org.eclipse.cdt.core.PE" id="cdt.managedbuild.target.gnu.platform.mingw.base.878016717" name="Debug Platform" osList="win32" superClass="cdt.managedbuild.target.gnu.platform.mingw.base"/>
<builder buildPath="${workspace_loc:/w12_solution_01_Julia}/Debug" id="cdt.managedbuild.tool.gnu.builder.mingw.base.1654136987" keepEnvironmentInBuildfile="false" name="CDT Internal Builder" superClass="cdt.managedbuild.tool.gnu.builder.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.assembler.mingw.base.1048849789" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.base">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.705940976" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.archiver.mingw.base.1351003599" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.base.1397108175" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.base">
<option id="gnu.cpp.compiler.option.optimization.level.697836254" name="Optimization Level" superClass="gnu.cpp.compiler.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
<option defaultValue="gnu.cpp.compiler.debugging.level.max" id="gnu.cpp.compiler.option.debugging.level.1781775245" name="Debug Level" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option id="gnu.cpp.compiler.option.dialect.std.515655801" name="Language standard" superClass="gnu.cpp.compiler.option.dialect.std" useByScannerDiscovery="true" value="gnu.cpp.compiler.dialect.default" valueType="enumerated"/>
<option id="gnu.cpp.compiler.option.dialect.flags.1925446988" name="Other dialect flags" superClass="gnu.cpp.compiler.option.dialect.flags" useByScannerDiscovery="true" value="-std=c++17" valueType="string"/>
<option id="gnu.cpp.compiler.option.other.pic.522442286" name="Position Independent Code (-fPIC)" superClass="gnu.cpp.compiler.option.other.pic" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.393156645" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.compiler.mingw.base.1277984237" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.base">
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.option.optimization.level.436685033" name="Optimization Level" superClass="gnu.c.compiler.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option defaultValue="gnu.c.debugging.level.max" id="gnu.c.compiler.option.debugging.level.1506729705" name="Debug Level" superClass="gnu.c.compiler.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1314652277" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.base.1645630833" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.base">
<option defaultValue="true" id="gnu.c.link.option.shared.415556161" name="Shared (-shared)" superClass="gnu.c.link.option.shared" valueType="boolean"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.mingw.base.494422" name="MinGW C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.mingw.base">
<option defaultValue="true" id="gnu.cpp.link.option.shared.1760652350" name="Shared (-shared)" superClass="gnu.cpp.link.option.shared" useByScannerDiscovery="false" valueType="boolean"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1045331737" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
<cconfiguration id="cdt.managedbuild.config.gnu.mingw.so.release.1920313731">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.mingw.so.release.1920313731" moduleId="org.eclipse.cdt.core.settings" name="Release">
<externalSettings>
<externalSetting>
<entry flags="VALUE_WORKSPACE_PATH" kind="includePath" name="/w12_solution_01_Julia"/>
<entry flags="VALUE_WORKSPACE_PATH" kind="libraryPath" name="/w12_solution_01_Julia/Release"/>
<entry flags="RESOLVED" kind="libraryFile" name="w12_solution_01_Julia" srcPrefixMapping="" srcRootPath=""/>
</externalSetting>
</externalSettings>
<extensions>
<extension id="org.eclipse.cdt.core.PE64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.MachO64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.XCOFF32" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.Cygwin_PE64" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GNU_ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.SOM" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.Cygwin_PE" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.MachO" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.PE" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactExtension="dll" artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.sharedLib" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.sharedLib,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.mingw.so.release.1920313731" name="Release" parent="cdt.managedbuild.config.gnu.mingw.so.release">
<folderInfo id="cdt.managedbuild.config.gnu.mingw.so.release.1920313731." name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.mingw.so.release.1979865257" name="MinGW GCC" superClass="cdt.managedbuild.toolchain.gnu.mingw.so.release">
<targetPlatform binaryParser="org.eclipse.cdt.core.PE64;org.eclipse.cdt.core.MachO64;org.eclipse.cdt.core.XCOFF32;org.eclipse.cdt.core.Cygwin_PE64;org.eclipse.cdt.core.ELF;org.eclipse.cdt.core.GNU_ELF;org.eclipse.cdt.core.SOM;org.eclipse.cdt.core.Cygwin_PE;org.eclipse.cdt.core.MachO;org.eclipse.cdt.core.PE" id="cdt.managedbuild.target.gnu.platform.mingw.so.release.1546798485" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.mingw.so.release"/>
<builder buildPath="${workspace_loc:/w12_solution_01_Julia}/Release" id="cdt.managedbuild.tool.gnu.builder.mingw.base.2063785416" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="CDT Internal Builder" superClass="cdt.managedbuild.tool.gnu.builder.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.assembler.mingw.so.release.1642965659" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.mingw.so.release">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.132123639" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.archiver.mingw.base.1409835697" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.mingw.base"/>
<tool id="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.release.2062060313" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.release">
<option id="gnu.cpp.compiler.mingw.so.release.option.optimization.level.1624557038" name="Optimization Level" superClass="gnu.cpp.compiler.mingw.so.release.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.most" valueType="enumerated"/>
<option defaultValue="gnu.cpp.compiler.debugging.level.none" id="gnu.cpp.compiler.mingw.so.release.option.debugging.level.1302105876" name="Debug Level" superClass="gnu.cpp.compiler.mingw.so.release.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option id="gnu.cpp.compiler.option.dialect.flags.1698882799" name="Other dialect flags" superClass="gnu.cpp.compiler.option.dialect.flags" useByScannerDiscovery="true" value="-std=c++1z" valueType="string"/>
<option id="gnu.cpp.compiler.option.other.other.284791204" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" useByScannerDiscovery="false" value="-c -fmessage-length=0 -Wno-attributes" valueType="string"/>
<option id="gnu.cpp.compiler.option.other.pic.733340978" name="Position Independent Code (-fPIC)" superClass="gnu.cpp.compiler.option.other.pic" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option id="gnu.cpp.compiler.option.dialect.std.1605804553" name="Language standard" superClass="gnu.cpp.compiler.option.dialect.std" useByScannerDiscovery="true" value="gnu.cpp.compiler.dialect.default" valueType="enumerated"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1493390500" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.compiler.mingw.so.release.1250668281" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.mingw.so.release">
<option defaultValue="gnu.c.optimization.level.most" id="gnu.c.compiler.mingw.so.release.option.optimization.level.2105595039" name="Optimization Level" superClass="gnu.c.compiler.mingw.so.release.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option defaultValue="gnu.c.debugging.level.none" id="gnu.c.compiler.mingw.so.release.option.debugging.level.511846729" name="Debug Level" superClass="gnu.c.compiler.mingw.so.release.option.debugging.level" useByScannerDiscovery="false" valueType="enumerated"/>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1411528871" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.c.linker.mingw.so.release.992367683" name="MinGW C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.mingw.so.release">
<option defaultValue="true" id="gnu.c.link.mingw.so.release.option.shared.364915193" name="Shared (-shared)" superClass="gnu.c.link.mingw.so.release.option.shared" valueType="boolean"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cpp.linker.mingw.so.release.1425153754" name="MinGW C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.mingw.so.release">
<option defaultValue="true" id="gnu.cpp.link.mingw.so.release.option.shared.194013385" name="Shared (-shared)" superClass="gnu.cpp.link.mingw.so.release.option.shared" useByScannerDiscovery="false" valueType="boolean"/>
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1681608863" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="src" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="w12_solution_01_Julia.cdt.managedbuild.target.gnu.mingw.so.1541013652" name="Shared Library" projectType="cdt.managedbuild.target.gnu.mingw.so"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.release.1920313731;cdt.managedbuild.config.gnu.mingw.so.release.1920313731.;cdt.managedbuild.tool.gnu.c.compiler.mingw.so.release.1250668281;cdt.managedbuild.tool.gnu.c.compiler.input.1411528871">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.debug.1055223311;cdt.managedbuild.config.gnu.mingw.so.debug.1055223311.;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.debug.34232129;cdt.managedbuild.tool.gnu.cpp.compiler.input.47121712">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.debug.1055223311;cdt.managedbuild.config.gnu.mingw.so.debug.1055223311.;cdt.managedbuild.tool.gnu.c.compiler.mingw.so.debug.1388639026;cdt.managedbuild.tool.gnu.c.compiler.input.542458592">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.mingw.so.release.1920313731;cdt.managedbuild.config.gnu.mingw.so.release.1920313731.;cdt.managedbuild.tool.gnu.cpp.compiler.mingw.so.release.2062060313;cdt.managedbuild.tool.gnu.cpp.compiler.input.1493390500">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="refreshScope" versionNumber="2">
<configuration configurationName="Debug">
<resource resourceType="PROJECT" workspacePath="/w12_solution_01_Julia"/>
</configuration>
<configuration configurationName="Release">
<resource resourceType="PROJECT" workspacePath="/w12_solution_01_Julia"/>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
</cproject>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>w12_solution_01_JuliaLib</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
#include "Bmp.h"
#include <cstddef>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <utility>
#include <cassert>
namespace julia {
namespace {
template<typename ValueType>
void write(std::vector<std::byte> & buffer, std::size_t offset, ValueType const &value) {
std::memcpy(buffer.data() + offset, &value, sizeof(ValueType));
}
}
auto constexpr bmp_file_header_size { 14 };
auto constexpr bmp_image_header_size { static_cast<std::uint32_t>(40) };
auto constexpr bmp_file_header_magic { "BM" };
auto constexpr bmp_file_header_magic_offset { 0 };
auto constexpr bmp_file_size_offset { bmp_file_header_magic_offset + 2 };
auto constexpr bmp_data_offset_offset { bmp_file_size_offset + 8 };
auto constexpr bmp_image_header_size_offset { bmp_file_header_size };
auto constexpr bmp_image_width_offset { bmp_image_header_size_offset + sizeof(std::uint32_t) };
auto constexpr bmp_image_height_offset { bmp_image_width_offset + sizeof(std::uint32_t) };
auto constexpr bmp_image_planes_offset { bmp_image_height_offset + sizeof(std::uint32_t) };
auto constexpr bmp_image_bits_per_pixel_offset { bmp_image_planes_offset + sizeof(std::uint16_t) };
auto constexpr bmp_image_compression_offset { bmp_image_bits_per_pixel_offset + sizeof(std::uint16_t) };
auto constexpr bmp_image_size_offset { bmp_image_compression_offset + sizeof(std::uint32_t) };
auto constexpr bmp_image_x_pixels_per_meter_offset { bmp_image_size_offset + sizeof(std::uint32_t) };
auto constexpr bmp_image_y_pixels_per_meter_offset { bmp_image_x_pixels_per_meter_offset + sizeof(std::uint32_t) };
auto constexpr bmp_image_color_map_entry_count_offset { bmp_image_y_pixels_per_meter_offset + sizeof(std::uint32_t) };
auto constexpr bmp_image_significant_colors_count_offset { bmp_image_color_map_entry_count_offset + sizeof(std::uint32_t) };
auto constexpr bmp_data_offset { bmp_file_header_size + bmp_image_header_size };
Bmp::Bmp(std::uint32_t size) :
m_size { size }, m_pixel_data { size * size } {
}
void Bmp::put(Pixel pixel) noexcept {
assert(pixel.x < m_size);
assert(pixel.y < m_size);
auto pixel_index = pixel.x + m_size * pixel.y;
m_pixel_data[pixel_index] = pixel;
}
std::uint32_t Bmp::size() const noexcept {
return m_size;
}
std::vector<std::byte> Bmp::dump() {
auto const pixel_data_size { m_size * m_size * 3 };
auto buffer = std::vector<std::byte> { bmp_file_header_size + bmp_image_header_size + pixel_data_size };
write_file_header(buffer);
write_image_header(buffer);
write_pixel_data(buffer);
return buffer;
}
void Bmp::write_file_header(std::vector<std::byte> &buffer) const noexcept {
std::memcpy(buffer.data(), bmp_file_header_magic, 2);
auto const file_size = std::uint32_t { bmp_file_header_size + bmp_image_header_size + 4 * m_size * m_size };
write(buffer, bmp_file_size_offset, file_size);
auto const data_offset = std::uint32_t { bmp_file_header_size + bmp_image_header_size };
write(buffer, bmp_data_offset_offset, data_offset);
}
void Bmp::write_image_header(std::vector<std::byte> &buffer) const noexcept {
write(buffer, bmp_image_header_size_offset, bmp_image_header_size);
write(buffer, bmp_image_width_offset, m_size);
write(buffer, bmp_image_height_offset, m_size);
write(buffer, bmp_image_planes_offset, static_cast<std::uint16_t>(1));
write(buffer, bmp_image_bits_per_pixel_offset, static_cast<std::uint16_t>(24));
write(buffer, bmp_image_compression_offset, std::uint32_t { });
write(buffer, bmp_image_size_offset, static_cast<std::uint32_t>((m_size + (m_size % 4)) * m_size));
auto ppm = static_cast<std::uint32_t>(std::lround(72.0 * 39.3701));
write(buffer, bmp_image_x_pixels_per_meter_offset, ppm);
write(buffer, bmp_image_y_pixels_per_meter_offset, ppm);
write(buffer, bmp_image_color_map_entry_count_offset, std::uint32_t { });
write(buffer, bmp_image_significant_colors_count_offset, std::uint32_t { });
}
void Bmp::write_pixel_data(std::vector<std::byte> &buffer) const noexcept {
auto const row_length = m_size * 3 + m_size % 4;
for_each(cbegin(m_pixel_data), cend(m_pixel_data), [&](auto pixel) {
std::swap(pixel.color.red, pixel.color.blue);
auto const row = m_size - 1 - pixel.y;
auto const pixel_offset = bmp_data_offset + row * row_length + pixel.x * 3;
write(buffer, pixel_offset, pixel.color);
});
}
}
#ifndef INCLUDE_BMP_H_
#define INCLUDE_BMP_H_
#include <cstddef>
#include <cstdint>
#include <vector>
namespace julia {
struct Pixel {
std::uint32_t x{};
std::uint32_t y{};
struct {
std::uint8_t red{};
std::uint8_t green{};
std::uint8_t blue{};
} color{};
};
struct Bmp {
explicit Bmp(std::uint32_t size);
void put(Pixel pixel) noexcept;
std::uint32_t size() const noexcept;
std::vector<std::byte> dump();
private:
void write_file_header(std::vector<std::byte> &buffer) const noexcept;
void write_image_header(std::vector<std::byte> &buffer) const noexcept;
void write_pixel_data(std::vector<std::byte> & buffer) const noexcept;
std::uint32_t m_size;
std::vector<Pixel> m_pixel_data;
};
}
#endif /* INCLUDE_BMP_H_ */
#include "Julia.h"
#include <array>
#include <complex>
namespace julia {
using rgb_color = decltype(Pixel::color);
//@formatter:off
constexpr auto palette = std::array{
rgb_color{ 66, 30, 15 },
rgb_color{ 25, 7, 26 },
rgb_color{ 9, 1, 47 },
rgb_color{ 4, 4, 73 },
rgb_color{ 0, 7, 100 },
rgb_color{ 12, 44, 138 },
rgb_color{ 24, 82, 177 },
rgb_color{ 57, 125, 209 },
rgb_color{ 134, 181, 229 },
rgb_color{ 211, 236, 248 },
rgb_color{ 241, 233, 191 },
rgb_color{ 248, 201, 95 },
rgb_color{ 255, 170, 0 },
rgb_color{ 204, 128, 0 },
rgb_color{ 153, 87, 0 },
rgb_color{ 106, 52, 3 },
};
//@formatter:on
void render(Bmp &image, Window window, double exponent, std::complex<double> k, int iterations) {
auto const upper_left = std::complex<double> { window.size - window.center.x, window.size + window.center.y };
auto const lower_right = std::complex<double> { -window.size - window.center.x, -window.size + window.center.y };
auto const x_scaling_factor = (upper_left.real() - lower_right.real()) / (image.size() - 1);
auto const y_scaling_factor = (upper_left.imag() - lower_right.imag()) / (image.size() - 1);
for (auto y { 0u }; y < image.size(); ++y) {
auto c = std::complex<double> { 0.0, upper_left.imag() - y * y_scaling_factor };
for (auto x { 0u }; x < image.size(); ++x) {
c.real(lower_right.real() + x * x_scaling_factor);
auto z = c;
image.put([&]() -> Pixel {
for (auto n { 0 }; n < iterations; ++n) {
if (abs(z) > 2) {
return {x, y, palette[n % palette.size()]};
}
z = pow(z, exponent) + k;
}
return {x, y};
}());
}
}
}
}
#ifndef INCLUDE_JULIA_H_
#define INCLUDE_JULIA_H_
#include "Bmp.h"