<commit_before>#include "fitz.h"
#include "mupdf.h"

static fz_error
pdf_loadcompositeshadefunc(fz_shade *shade, pdf_xref *xref, fz_obj *dict, fz_obj *funcref, float t0, float t1)
{
	fz_error error;
	pdf_function *func;
	int i;

	error = pdf_loadfunction(&func, xref, funcref);
	if (error)
		return fz_rethrow(error, "unable to evaluate shading function");

	for (i = 0; i < 256; ++i)
	{
		float t = t0 + (i / 256.0) * (t1 - t0);

		error = pdf_evalfunction(func, &t, 1, shade->function[i], shade->cs->n);
		if (error)
		{
			pdf_dropfunction(func);
			return fz_rethrow(error, "unable to evaluate shading function at %g", t);
		}
	}

	pdf_dropfunction(func);

	return fz_okay;
}

static fz_error
pdf_loadcomponentshadefunc(fz_shade *shade, pdf_xref *xref, fz_obj *dict, fz_obj *funcs, float t0, float t1)
{
	fz_error error;
	pdf_function **func = nil;
	int i;

	if (fz_arraylen(funcs) != shade->cs->n)
	{
		error = fz_throw("incorrect number of shading functions");
		goto cleanup;
	}

	func = fz_malloc(fz_arraylen(funcs) * sizeof(pdf_function *));
	memset(func, 0x00, fz_arraylen(funcs) * sizeof(pdf_function *));

	for (i = 0; i < fz_arraylen(funcs); i++)
	{
		fz_obj *obj = nil;

		obj = fz_arrayget(funcs, i);
		if (!obj)
		{
			error = fz_throw("shading function component not found");
			goto cleanup;
		}

		error = pdf_loadfunction(&func[i], xref, obj);
		if (error)
		{
			error = fz_rethrow(error, "unable to evaluate shading function");
			goto cleanup;
		}
	}

	for (i = 0; i < 256; ++i)
	{
		float t = t0 + (i / 256.0) * (t1 - t0);
		int k;

		for (k = 0; k < fz_arraylen(funcs); k++)
		{
			error = pdf_evalfunction(func[k], &t, 1, &shade->function[i][k], 1);
			if (error)
			{
				error = fz_rethrow(error, "unable to evaluate shading function at %g", t);
				goto cleanup;
			}
		}
	}

	for (i = 0; i < fz_arraylen(funcs); i++)
		pdf_dropfunction(func[i]);
	fz_free(func);

	return fz_okay;

cleanup:
	if (func)
	{
		for (i = 0; i < fz_arraylen(funcs); i++)
			if (func[i])
				pdf_dropfunction(func[i]);
		fz_free(func);
	}

	return error;
}

fz_error
pdf_loadshadefunction(fz_shade *shade, pdf_xref *xref, fz_obj *dict, float t0, float t1)
{
	fz_error error;
	fz_obj *obj;

	obj = fz_dictgets(dict, "Function");
	if (!obj)
		return fz_throw("shading function not found");

	shade->usefunction = 1;

	if (fz_isdict(obj))
		error = pdf_loadcompositeshadefunc(shade, xref, dict, obj, t0, t1);
	else if (fz_isarray(obj))
		error = pdf_loadcomponentshadefunc(shade, xref, dict, obj, t0, t1);
	else
		error = fz_throw("invalid shading function");

	if (error)
		return fz_rethrow(error, "couldn't load shading function");

	return fz_okay;
}<commit_msg>Parsing functions in shadings only requires functions dict, not shading dict.<commit_after>#include "fitz.h"
#include "mupdf.h"

static fz_error
pdf_loadcompositeshadefunc(fz_shade *shade, pdf_xref *xref, fz_obj *funcref, float t0, float t1)
{
	fz_error error;
	pdf_function *func;
	int i;

	error = pdf_loadfunction(&func, xref, funcref);
	if (error)
		return fz_rethrow(error, "unable to evaluate shading function");

	for (i = 0; i < 256; ++i)
	{
		float t = t0 + (i / 256.0) * (t1 - t0);

		error = pdf_evalfunction(func, &t, 1, shade->function[i], shade->cs->n);
		if (error)
		{
			pdf_dropfunction(func);
			return fz_rethrow(error, "unable to evaluate shading function at %g", t);
		}
	}

	pdf_dropfunction(func);

	return fz_okay;
}

static fz_error
pdf_loadcomponentshadefunc(fz_shade *shade, pdf_xref *xref, fz_obj *funcs, float t0, float t1)
{
	fz_error error;
	pdf_function **func = nil;
	int i;

	if (fz_arraylen(funcs) != shade->cs->n)
	{
		error = fz_throw("incorrect number of shading functions");
		goto cleanup;
	}

	func = fz_malloc(fz_arraylen(funcs) * sizeof(pdf_function *));
	memset(func, 0x00, fz_arraylen(funcs) * sizeof(pdf_function *));

	for (i = 0; i < fz_arraylen(funcs); i++)
	{
		fz_obj *obj = nil;

		obj = fz_arrayget(funcs, i);
		if (!obj)
		{
			error = fz_throw("shading function component not found");
			goto cleanup;
		}

		error = pdf_loadfunction(&func[i], xref, obj);
		if (error)
		{
			error = fz_rethrow(error, "unable to evaluate shading function");
			goto cleanup;
		}
	}

	for (i = 0; i < 256; ++i)
	{
		float t = t0 + (i / 256.0) * (t1 - t0);
		int k;

		for (k = 0; k < fz_arraylen(funcs); k++)
		{
			error = pdf_evalfunction(func[k], &t, 1, &shade->function[i][k], 1);
			if (error)
			{
				error = fz_rethrow(error, "unable to evaluate shading function at %g", t);
				goto cleanup;
			}
		}
	}

	for (i = 0; i < fz_arraylen(funcs); i++)
		pdf_dropfunction(func[i]);
	fz_free(func);

	return fz_okay;

cleanup:
	if (func)
	{
		for (i = 0; i < fz_arraylen(funcs); i++)
			if (func[i])
				pdf_dropfunction(func[i]);
		fz_free(func);
	}

	return error;
}

fz_error
pdf_loadshadefunction(fz_shade *shade, pdf_xref *xref, fz_obj *dict, float t0, float t1)
{
	fz_error error;
	fz_obj *obj;

	obj = fz_dictgets(dict, "Function");
	if (!obj)
		return fz_throw("shading function not found");

	shade->usefunction = 1;

	if (fz_isdict(obj))
		error = pdf_loadcompositeshadefunc(shade, xref, obj, t0, t1);
	else if (fz_isarray(obj))
		error = pdf_loadcomponentshadefunc(shade, xref, obj, t0, t1);
	else
		error = fz_throw("invalid shading function");

	if (error)
		return fz_rethrow(error, "couldn't load shading function");

	return fz_okay;
}
{'fraction_non_alphanumeric': 0.14850746268656717, 'fraction_numerical': 0.016044776119402984, 'mean_word_length': 4.038533834586466, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6672577', 'n_tokens_mistral': 2472, 'n_tokens_neox': 2266, 'n_words': 554}