about summary refs log tree commit diff
path: root/tools/eaglemode/plugins/avif/src/PlAvif.cpp
blob: e4807bacd49af231bada044242206bbd0c6f90bf (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <emCore/emFpPlugin.h>
#include <emCore/emImageFile.h>

#include "avif/avif.h"

class PlAvifImageFileModel : public emImageFileModel
{
public:

	static emRef<PlAvifImageFileModel> Acquire(
		emContext & context, const emString & name, bool common=true
	);

protected:
	PlAvifImageFileModel(emContext & context, const emString & name);
	virtual ~PlAvifImageFileModel();
	virtual void TryStartLoading();
	virtual bool TryContinueLoading();
	virtual void QuitLoading();
	virtual void TryStartSaving();
	virtual bool TryContinueSaving();
	virtual void QuitSaving();
	virtual emUInt64 CalcMemoryNeed();
	virtual double CalcFileProgress();

private:
	struct LoadingState;
	LoadingState * L = NULL;
};


struct PlAvifImageFileModel::LoadingState {
	avifRGBImage rgb;
	avifDecoder * decoder;
};


emRef<PlAvifImageFileModel> PlAvifImageFileModel::Acquire(
	emContext & context, const emString & name, bool common
)
{
	EM_IMPL_ACQUIRE(PlAvifImageFileModel, context, name, common)
}


PlAvifImageFileModel::PlAvifImageFileModel(
	emContext & context, const emString & name
)
	: emImageFileModel(context, name)
{
}


PlAvifImageFileModel::~PlAvifImageFileModel()
{
	PlAvifImageFileModel::QuitLoading();
	PlAvifImageFileModel::QuitSaving();
}


void PlAvifImageFileModel::TryStartLoading()
{
	avifResult result;

	L = new LoadingState;
	memset(L, 0, sizeof(LoadingState));

	L->decoder = avifDecoderCreate();
	if (L->decoder == NULL) {
		throw emException("failed to create AVIF decoder");
	}

	result = avifDecoderSetIOFile(L->decoder, GetFilePath());
	if (result != AVIF_RESULT_OK) {
		throw emException("%s", avifResultToString(result));
	}

	result = avifDecoderParse(L->decoder);
	if (result != AVIF_RESULT_OK) {
		throw emException("%s", avifResultToString(result));
	}

	FileFormatInfo = emString::Format(
		"AVIF %s %ubpc",
			avifPixelFormatToString(L->decoder->image->yuvFormat),
			L->decoder->image->depth
	);


	Signal(ChangeSignal);
}


bool PlAvifImageFileModel::TryContinueLoading()
{
	avifResult result;

	if (!Image.GetHeight()) {
		Image.Setup(
			L->decoder->image->width,
			L->decoder->image->height,
			L->decoder->alphaPresent ? 4 : 3
		);
	}

	result = avifDecoderNextImage(L->decoder);
	if (result != AVIF_RESULT_OK) {
		throw emException("%s", avifResultToString(result));
	}

	avifRGBImageSetDefaults(&L->rgb, L->decoder->image);
	L->rgb.format = L->decoder->alphaPresent ?
		AVIF_RGB_FORMAT_RGBA : AVIF_RGB_FORMAT_RGB;
	L->rgb.pixels   = Image.GetWritableMap();
	L->rgb.width    = Image.GetWidth();
	L->rgb.height   = Image.GetHeight();
	L->rgb.depth    = 8;
	L->rgb.rowBytes = Image.GetWidth() * Image.GetChannelCount();

	result = avifImageYUVToRGB(L->decoder->image, &L->rgb);
	if (result != AVIF_RESULT_OK) {
		throw emException("%s", avifResultToString(result));
	}

	Signal(ChangeSignal);
	return true;
}


void PlAvifImageFileModel::QuitLoading()
{
	if (L) {
		if (L->decoder) avifDecoderDestroy(L->decoder);
		delete L;
		L = NULL;
	}
}


void PlAvifImageFileModel::TryStartSaving()
{
	throw emException("PlAvifImageFileModel: Saving not implemented.");
}


bool PlAvifImageFileModel::TryContinueSaving()
{
	return false;
}


void PlAvifImageFileModel::QuitSaving()
{
}


emUInt64 PlAvifImageFileModel::CalcMemoryNeed()
{
	return
		(emUInt64)
			L->decoder->image->width *
			L->decoder->image->height *
			(L->decoder->alphaPresent ? 4 : 3);
}


double PlAvifImageFileModel::CalcFileProgress()
{
	return 0.0;
}

extern "C" {
	emPanel * PlAvifFpPluginFunc(
		emPanel::ParentArg parent, const emString & name,
		const emString & path, emFpPlugin * plugin,
		emString * errorBuf
	)
	{
		if (plugin->Properties.GetCount()) {
			*errorBuf="PlAvifFpPlugin: No properties allowed.";
			return NULL;
		}
		return new emImageFilePanel(
			parent, name,
			PlAvifImageFileModel::Acquire(
				parent.GetRootContext(), path
			)
		);
	}
}