/*
	KaniView loads all the .dll files from the /plugins folder. The other folders here contain the source code for the default plugins, just run the build.bat script to compile them (gcc expected) (it will overwrite the .dll in /plugins).
	
	At first init_1() is called, it's goal is to fill in [out__info] with information about the plugin, mainly which file formats it supports. It should also store the [plugin_api] struct so it can be used elsewhere. If the plugin format is changed in the future, KaniView will first attempt to call init_2 or init_3 etc, but for now there's only init_1.
	
	load_image() is called whenever KaniView wants to load an image that the plugin supports. The plugin with highest priority will be used first, and if it fails then the next highest priority will be used. Use a negative priority if the plugin has lacking feature support and/or should ideally be replaced with a better one.
*/

// Base header.
#include "plugin_base.h"

// Initialize plugin.
Plugin_file_format supported_types [] = {
	{.ext=S("png")},
	{.ext=S("jpg")},
	{.ext=S("jpeg")},
};
Plugin_api api = {0}; // Store this here so it can be easily used around the plugin.
void init_1 (Plugin_api* plugin_api, Plugin_info* out__info) {
	api = *plugin_api;
	*out__info = (Plugin_info){
		.priority = 0,
		.supported_types_count = countof(supported_types),
		.supported_types = supported_types,
	};
}

// Load an image. The job of this function is to read the file data from [len] and [data] and fill in [out__file].
// If this plugin supports multiple decoders, [ext] (file extension) can be used to choose the appropriate one.
// zoom is only used for vector images.
Errnum load_image (String ext, u64 len, void* data, f64 zoom, Format_file* out__file) {
	Format_file file = {
		.w = 128,
		.h = 128,
		.frame_count = 1,
	};
	file.frames = api.arena_alloc(NULL, sizeof(Format_frame)*file.frame_count);
	
	if (strings_are_equal(ext, s("png"))) {
		// Some special code for png files.
	}
	
	if (some_error_occurred) return 1;
	
	// Add first (and in this case the only) image. Note that every frame must be a keyframe, that's why they do not have x/y/width/height properties.
	Format_frame frame = {
		.pixels = api.permanent_alloc(NULL, file.w*file.h*sizeof(Rgba8)),
		.duration = 0, // If animation, this is the frame's duration.
	};
	file.frames[0] = frame;
	
	*out__file = file;
	return 0;
}
