Skip to content

Add library API#152

Open
jonbarrow wants to merge 4 commits intofhanau:masterfrom
jonbarrow:feat/lib-api
Open

Add library API#152
jonbarrow wants to merge 4 commits intofhanau:masterfrom
jonbarrow:feat/lib-api

Conversation

@jonbarrow
Copy link

@jonbarrow jonbarrow commented Feb 11, 2026

I apologize if any of this is improper, I haven't spent a ton of time with the codebase and I don't usually work on C++ image projects in general, nor have I used CMake much either. So some of this was just a bit of trial and error until I got something working.

Changes include:

  • Removed the core functionality from main.cpp into ect_core.cpp. This way both main.cpp and the library API can use the same core functions
  • Exported everything from ect_core.cpp into main.h so the library API can access it
  • Added a simple library API in libect.h and libect.cpp. This adds a new LibECTOptions struct, which is mapped to the internal ECTOptions struct internally. The main use of LibECTOptions is just to provide some more clear field names, since I found the ECTOptions to be a bit confusingly named but didn't want to modify the core project more than I had to. It also adds a number of constants and enums to make using the API a bit more clear.

What this doesn't do, however, is extend the existing feature set of the tool. This means that even with this API, images are expected to be loaded from disk. Having an in-memory version of these functions would be ideal too, similar to oxipng (https://docs.rs/oxipng/latest/oxipng/#functions), but this would require some larger scale changes to the rest of the project, since every function currently assumes a file is being passed around. I did not go for these changes in this PR since I wanted to keep the scope minimal and not break anything (due to my limited experience with the codebase), but if that's something you're interested in I can give it a try too.

This should make features like #107 possible. I have not done extensive testing, but I can confirm that the original CLI program builds and functions just fine on my Mac, and using the new library API I was able to write a small test program that works as expected:

#include "libect.h"
#include <stdio.h>

int main(int argc, char** argv) {
	if (argc < 2) {
		printf("Usage: %s <image.png>\n", argv[0]);
		return 1;
	}

	LibECTOptions options;
	ect_init_options(&options);

	options.compression_level = ECT_COMPRESSION_LEVEL_MAX;
	options.strip = true;
	options.png_all_filters_mode = ECT_PNG_ALL_FILTERS_BRUTEFORCE;

	printf("Optimizing %s...\n", argv[1]);

	int result = ect_optimize_png(argv[1], &options);

	if (result == 0) {
		printf("Successfully optimized!\n");
	} else {
		printf("Optimization failed\n");
	}

	return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant