summaryrefslogtreecommitdiff
path: root/tests/avif/avif_im2im.c
blob: 3a07ebdae6c63a208945352e4c5fb31b02dc6b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
 * File: avif_im2im
 *
 * Sanity check for AVIF encoding and decoding.
 * We create a simple gd image, we encode it to AVIF, and we decode it back to gd.
 * Then we make sure the image we started with and the image we finish with are the same.
 *
 */

#include "gd.h"
#include "gdtest.h"
#include <stdio.h>

int main()
{
	gdImagePtr srcGdIm, destGdIm;
	void *avifImageDataPtr;
	FILE *fp;
	int r, g, b;
	int size = 0;
	CuTestImageResult result = {0, 0};

	// Create new gd image and add some shapes to it.
	srcGdIm = gdImageCreateTrueColor(100, 100);
	gdTestAssertMsg(srcGdIm != NULL, "could not create source image\n");

	r = gdImageColorAllocate(srcGdIm, 0xFF, 0, 0);
	g = gdImageColorAllocate(srcGdIm, 0, 0xFF, 0);
	b = gdImageColorAllocate(srcGdIm, 0, 0, 0xFF);
	gdImageFilledRectangle(srcGdIm, 0, 0, 99, 99, r);
	gdImageRectangle(srcGdIm, 20, 20, 79, 79, g);
	gdImageEllipse(srcGdIm, 70, 25, 30, 20, b);

	// Encode the gd image to a test AVIF file.
	fp = gdTestTempFp();
	gdImageAvif(srcGdIm, fp);
	fclose(fp);

	// Encode the gd image to an AVIF image in memory.
	avifImageDataPtr = gdImageAvifPtrEx(srcGdIm, &size, 100, 10);
	gdTestAssertMsg(avifImageDataPtr != NULL, "gdImageAvifPtr() returned null\n");
	gdTestAssertMsg(size > 0, "gdImageAvifPtr() returned a non-positive size\n");

	// Encode the AVIF image back into a gd image.
	destGdIm = gdImageCreateFromAvifPtr(size, avifImageDataPtr);
	gdTestAssertMsg(destGdIm != NULL, "gdImageAvifPtr() returned null\n");

	// Encode that gd image to a test AVIF file.
	fp = gdTestTempFp();
	gdImageAvif(destGdIm, fp);
	fclose(fp);

	// Make sure the image we started with is the same as the image after two conversions.
	 gdTestImageDiff(srcGdIm, destGdIm, NULL, &result);
	 gdTestAssertMsg(result.pixels_changed == 0, "pixels changed: %d\n", result.pixels_changed);

	if (srcGdIm)
		gdImageDestroy(srcGdIm);

	if (destGdIm)
		gdImageDestroy(destGdIm);

	if (avifImageDataPtr)
		gdFree(avifImageDataPtr);

	return gdNumFailures();
}