Skip to content

Commit

Permalink
update 1.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
tonquer committed Nov 26, 2022
1 parent e156f2f commit 2e54e03
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 22 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
```
"""
Version = "1.1.4"
Version = "1.1.5"

Plat = sys.platform

Expand Down
2 changes: 1 addition & 1 deletion setup2.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
```
"""
Version = "1.1.4"
Version = "1.1.5"

Plat = sys.platform

Expand Down
3 changes: 2 additions & 1 deletion src/apng_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ bool load_apng(Task& v)
free(p_image);
}
ok = true;
if (v.file.length() == 0) v.file = "apng";
if (v.save_format.length() == 0) v.save_format = "apng";
v.load_format = "apng";
End:

if (png_ptr)
Expand Down
15 changes: 9 additions & 6 deletions src/other_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,26 @@ bool jpg_save(Task& v)

bool to_save(Task &v)
{
if (!v.file.compare("bmp"))
if (!v.save_format.compare("bmp"))
{
return bmp_save(v);
}
else if (!v.file.compare("png"))
else if (!v.save_format.compare("png"))
{
return png_save(v);
}
else if (!v.file.compare("jpg") || !v.file.compare("jpeg"))
else if (!v.save_format.compare("jpg") || !v.save_format.compare("jpeg"))
{
return jpg_save(v);
}
else if (!v.file.compare("webp") || !v.file.compare("gif"))
else if (!v.save_format.compare("webp"))
{
if (v.outImage.size() <= 1)
return webp_save(v);
else
return webp_save_ani(v);
}
else if (!v.file.compare("apng"))
else if (!v.save_format.compare("apng"))
{
return save_apng(v);
}
Expand Down Expand Up @@ -197,7 +197,10 @@ bool stbi_xload(Task &v)
v.inFrame.push_back(100);
}
End:
if (v.file.length() == 0) v.file = format;
if (v.save_format.length() == 0) v.save_format = format;
if (!v.save_format.compare("gif")) v.save_format = "webp";
v.load_format = format;

if (pixeldata) stbi_image_free(pixeldata);
if (delays) stbi_image_free(delays);
return ok;
Expand Down
2 changes: 1 addition & 1 deletion src/stb_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -6819,7 +6819,7 @@ static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y,
}
memcpy( out + ((layers - 1) * stride), u, stride );
if (layers >= 2) {
two_back = out - 2 * stride;
two_back = out + ((layers - 2) * stride);
}

if (delays) {
Expand Down
18 changes: 13 additions & 5 deletions src/waifu2x_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static int GpuId;
static int TotalJobsProc = 0;
static int NumThreads = 1;
static int TaskId = 1;
static int WebpQuality = 90;

bool IsDebug = false;

Expand Down Expand Up @@ -49,7 +50,7 @@ int waifu2x_getData(void*& out, unsigned long& outSize, double& tick, int& callB
outSize = v.outSize;

v.out = NULL;
strcpy(format, v.file.c_str());
strcpy(format, v.save_format.c_str());
tick = v.allTick;
return v.id;
}
Expand Down Expand Up @@ -141,7 +142,7 @@ void* waifu2x_proc(void* args)
name = "cpu";

waifu2x_printf(stdout, "[waifu2x] start encode imageId :%d, gpu:%s, format:%s, model:%s, noise:%d, scale:%d, tta:%d, tileSize:%d\n",
v.callBack, name, v.file.c_str(), waifu2x->mode_name.c_str(), waifu2x->noise, waifu2x->scale, waifu2x->tta_mode, v.tileSize);
v.callBack, name, v.save_format.c_str(), waifu2x->mode_name.c_str(), waifu2x->noise, waifu2x->scale, waifu2x->tta_mode, v.tileSize);
int scale_run_count = 1;
int frame = 0;
for (std::list<ncnn::Mat *>::iterator in = v.inImage.begin(); in != v.inImage.end(); in++)
Expand Down Expand Up @@ -590,6 +591,7 @@ int waifu2x_addData(const unsigned char* data, unsigned int size, int callBack,
v.toW = toW;
v.scale = scale;
v.tileSize = tileSize;
v.webp_quality = WebpQuality;
if ((toH <= 0 || toW <= 0) && scale <= 0)
{
waifu2x_set_error("invalid scale params");
Expand All @@ -601,10 +603,10 @@ int waifu2x_addData(const unsigned char* data, unsigned int size, int callBack,
waifu2x_set_error("invalid model index");
return sts;
}
if (format) v.file = format;
if (format) v.save_format = format;

transform(v.file.begin(), v.file.end(), v.file.begin(), ::tolower);
if (v.file.length() == 0 || !v.file.compare("jpg") || !v.file.compare("jpeg") || !v.file.compare("png") || !v.file.compare("webp") || !v.file.compare("jpg") || !v.file.compare("bmp") || !v.file.compare("apng"))
transform(v.save_format.begin(), v.save_format.end(), v.save_format.begin(), ::tolower);
if (v.save_format.length() == 0 || !v.save_format.compare("jpg") || !v.save_format.compare("jpeg") || !v.save_format.compare("png") || !v.save_format.compare("webp") || !v.save_format.compare("jpg") || !v.save_format.compare("bmp") || !v.save_format.compare("apng"))
{
Todecode.put(v);
return TaskId;
Expand Down Expand Up @@ -650,6 +652,12 @@ int waifu2x_set_debug(bool isdebug)
return 0;
}

int waifu2x_set_webp_quality(int webp_quality)
{
WebpQuality = webp_quality;
return 0;
}

int waifu2x_remove_wait(std::set<int>& taskIds)
{
Todecode.remove(taskIds);
Expand Down
5 changes: 4 additions & 1 deletion src/waifu2x_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class Task
void* fileDate;
int fileSize;
int allFileSize=0;
std::string file;
std::string load_format;
std::string save_format;
std::string err;
bool isSuc = true;

Expand All @@ -47,6 +48,7 @@ class Task
unsigned long toH;
float scale = 2;
int tileSize = 0;
int webp_quality = 90;

struct timeb startTick;
struct timeb decodeTick;
Expand Down Expand Up @@ -192,6 +194,7 @@ int waifu2x_get_path_size();
int waifu2x_stop();
int waifu2x_clear();
int waifu2x_set_debug(bool);
int waifu2x_set_webp_quality(int);
int waifu2x_printf(void* p, const char* fmt, ...);
int waifu2x_printf(void* p, const wchar_t* fmt, ...);
int waifu2x_remove_wait(std::set<int>&);
Expand Down
13 changes: 13 additions & 0 deletions src/waifu2x_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ waifu2x_py_init_set(PyObject* self, PyObject* args, PyObject* kwargs)
return PyLong_FromLong(sts);
}

static PyObject*
waifu2x_py_set_webp_quality(PyObject* self, PyObject* args)
{
int quality=0;
if (!PyArg_ParseTuple(args, "i", &quality))
PyLong_FromLong(-1);
if (quality <= 0 || quality > 100)
{
return PyLong_FromLong(-1);
}
waifu2x_set_webp_quality(quality);
return PyLong_FromLong(0);
}

static PyObject*
waifu2x_py_clear(PyObject* self, PyObject* args)
Expand Down
7 changes: 6 additions & 1 deletion src/waifu2x_py.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ waifu2x_py_set_debug(PyObject* self, PyObject* args);
static PyObject*
waifu2x_py_init_set(PyObject* self, PyObject* args, PyObject* kwargs);

static PyObject*
waifu2x_py_set_webp_quality(PyObject* self, PyObject* args);

static PyObject*
waifu2x_py_clear (PyObject* self, PyObject* args);

Expand Down Expand Up @@ -58,6 +61,8 @@ static PyMethodDef SpamMethods[] = {
"Init ncnn\n"},
{"initSet", (PyCFunction)waifu2x_py_init_set, METH_VARARGS | METH_KEYWORDS,
"Init setting\ngpuId: getGpuInfo get index \ncpuNum(Option): CPU model use CPU num, default cpu num / 2 \n"},
{"setWebpQuality", (PyCFunction)waifu2x_py_set_webp_quality, METH_VARARGS,
"set webp quality \ncan go from 0 (smaller output, lower quality) to 100 (best quality,larger output) \n"},
{"add", (PyCFunction)waifu2x_py_add, METH_VARARGS | METH_KEYWORDS,
"Add task, \ndata: img bytes \nmodelIndex: Model enum \nbackId: call back id \nformat(Option): export fmt, default import fmt \nimport support bmp png jpg gif webp \nexport support bmp png jpg webp \nwidth(Option): export set width \nhigh(Option): export set high \nscale(Option): export set width and high \ntileSize(Option): default Auto\n"},
{"getGpuInfo", (PyCFunction)waifu2x_py_get_info, METH_VARARGS,
Expand Down Expand Up @@ -98,5 +103,5 @@ static struct PyModuleDef spammodule = {

static bool IsInit = false;
static bool IsInitSet = false;
static const char* Version = "1.1.4";
static const char* Version = "1.1.5";
#endif
12 changes: 7 additions & 5 deletions src/webp_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ bool webp_load(Task &v)
}
v.inImage.push_back(inimage);
v.inFrame.push_back(100);
if (v.file.length() == 0) v.file = "webp";
if (v.save_format.length() == 0) v.save_format = "webp";
v.load_format = "webp";
return true;
}

Expand Down Expand Up @@ -112,7 +113,8 @@ bool webp_load_ani(Task& v)
++frame_index;
prev_frame_timestamp = timestamp;
}
if (v.file.length() == 0) v.file = "webp";
if (v.save_format.length() == 0) v.save_format = "webp";
v.load_format = "webp";
ok = true;
End:
WebPAnimDecoderDelete(dec);
Expand Down Expand Up @@ -169,7 +171,7 @@ bool webp_save_ani(Task &v)
ncnn::Mat & inimage = **i;
int duration= *j;

size = WebPEncodeLosslessRGBA((unsigned char*)inimage.data, inimage.w, inimage.h, inimage.w * (int)inimage.elemsize, &outb);
size = WebPEncodeRGBA((unsigned char*)inimage.data, inimage.w, inimage.h, inimage.w * (int)inimage.elemsize, v.webp_quality, &outb);

const WebPData webp_data = { outb, size };
frame.duration = duration;
Expand Down Expand Up @@ -209,11 +211,11 @@ bool webp_save(Task &v)

if (outimage.elemsize == 3)
{
length = WebPEncodeLosslessRGB((unsigned char*)outimage.data, outimage.w, outimage.h, outimage.w * (int)outimage.elemsize, &output);
length = WebPEncodeRGB((unsigned char*)outimage.data, outimage.w, outimage.h, outimage.w * (int)outimage.elemsize, v.webp_quality, &output);
}
else if (outimage.elemsize == 4)
{
length = WebPEncodeLosslessRGBA((unsigned char*)outimage.data, outimage.w, outimage.h, outimage.w * (int)outimage.elemsize, &output);
length = WebPEncodeRGBA((unsigned char*)outimage.data, outimage.w, outimage.h, outimage.w * (int)outimage.elemsize, v.webp_quality, &output);
}
else
{
Expand Down
1 change: 1 addition & 0 deletions waifu2x_vulkan/waifu2x_vulkan.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def add(data:bytes, modelIndex: MODEL, backId:int, width:int, height:int, format
def add(data:bytes, modelIndex: MODEL, backId:int, scale:float, format:_FORMAT="", tileSize:int=400) -> int: ...
def getVersion() -> str: ...
def setDebug(bool) -> None: ...
def setWebpQuality(int) -> None: ...
def getLastError() -> str: ...
def remove(backIds:List[int]) -> None: ...
def removeWaitProc(backIds:List[int]) -> None: ...
Expand Down

0 comments on commit 2e54e03

Please sign in to comment.