Files
libimg/README.md
T
DarthMooMancer 7745967382 DOCS: Added basic explaination to example code.
README: Added readme with copy of example code and first push of readme.
2026-09-03 14:36:46 -04:00

1.4 KiB

/*
 * Keep in mind any function that creates a new buffer and returns it can
 * overwrite the old one, however it is lost. SO create a new variable and free
 * the old one after use.
 */
int
main()
{
	// Init base image containers
	PPM img;
	JPEG jimg;

	// Add a file location of image to modify
	img.name = "../assets/test_2.ppm";

	// Load the PPM image
	unsigned int* buf = ppm_load(img.name, &img);

	// Check for null image load. In case image doesnt exist or fails to
	// load
	if (!buf)
	{
		LOGERROR(ERR_BUF_NULL);
		return ERR_BUF_NULL;
	}
	// Blur image and save to new pointer
	unsigned int* blur = box_blur(buf, &img, 20);

	// Free old buffer. MUST DO THIS. Maybe not immediately but before
	// ending or possibly leaking. Also it stays in heap and a large image
	// can take a lot of memory so better to clear anyways.
	free(buf);
	// Check for null image load. In case image doesnt exist or fails to
	// load
	if (!blur)
	{
		LOGERROR(ERR_BUF_NULL);
		return ERR_BUF_NULL;
	}

	// Loop image and apply filter to each pixel.
	for (int i = 0; i < img.len; i++)
	{
		blur[i] = u32_rotl(blur[i], 4);
	}

	// Take the attributes from ppm image so jpeg file can be saved
	jimg = inherit_from_ppm(&img);

	// Saves jpeg to location specified
	jpeg_save(blur, &jimg, "../assets/test_2_new.jpg");

	// Make sure to free any buffer still active
	free(blur);

	return 0;
}