summaryrefslogtreecommitdiff
path: root/Runtime/FileSystem/ImageJobs.cpp
blob: 1d836d290734e867c86a2ed612a43e10d7382005 (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
#include "ImageJobs.h"
#include "ThirdParty/stb/stb_image.h"
#include "Runtime/Graphics/ImageData.h"

//--------------------------------------------------------------------------

ReadImageFileJob::ReadImageFileJob(LuaBind::VM* vm, int indexOfCallback, const char* path)
	: isFinished(false)
	, callback(vm)
{
	this->path = path;
	LuaBind::State state = vm->GetCurThread();
	callback.SetRef(state, indexOfCallback);
}

ReadImageFileJob::~ReadImageFileJob()
{
	callback.UnRef();
	delete bridge.pixels; //for safe
}

void ReadImageFileJob::Process()
{
	stbi_set_flip_vertically_on_load(true);
	bridge.pixels = stbi_load(path.c_str(), &bridge.width, &bridge.height, &bridge.channels, 0);
	isFinished = true;
}

bool ReadImageFileJob::IsFinished()
{
	return isFinished;
}

// callback(imageData)
void ReadImageFileJob::Dispacth(void* param)
{
	LUA_BIND_STATE((lua_State*)param);
	
	ImageData* imgData = new ImageData(state.GetVM());
	imgData->pixels = bridge.pixels;
	bridge.pixels = NULL;
	imgData->width = bridge.width;
	imgData->height = bridge.height;
	imgData->format = EPixelFormat::PixelFormat_RGB;
	imgData->type = EPixelElementType::PixelType_UNSIGNED_BYTE;

	callback.PushRef(state);
	imgData->PushUserdata(state);
	state.Call(1, 0);
}

//--------------------------------------------------------------------------