Skip to content

Commit

Permalink
add decoder->decodeall
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSizuku committed Nov 24, 2024
1 parent 4a215a9 commit 93b06be
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ For example, to built plugin `asset/png.c`
mkdir -p build_mingw64/plugin
. script/fetch_depend.sh
fetch_stb
gcc -g -Idepend/stb-lastest -Isrc -fPIC -fvisibility=hidden -static-libgcc -shared asset/plugin/util_png.c -o build_mingw64/plugin/util_png.dll
gcc -g -Idepend/stb-lastest -Isrc -fPIC -fvisibility=hidden -static-libgcc -shared asset/plugin/util_stb.c -o build_mingw64/plugin/util_stb.dll
```

### (3) Lua plugin
Expand Down
18 changes: 15 additions & 3 deletions asset/plugin/util_png.c → asset/plugin/util_stb.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* implement for c module plugin png
* implement for c module plugin to decode images by stb
* developed by devseed
*/

#include <stdio.h>
#include <string.h>
#include "plugin.h"

#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_STATIC
#include "stb_image.h"

#if defined(_MSC_VER) || defined(__TINYC__)
Expand Down Expand Up @@ -79,6 +80,17 @@ PLUGIN_STATUS decode_pixel(void *context,
return STATUS_OK;
}

PLUGIN_STATUS decode_pixels(void *context,
const uint8_t* data, size_t datasize,
const struct tilefmt_t *fmt, struct pixel_t *pixels[],
size_t *npixel, bool remain_index)
{
*pixels = (struct pixel_t *)g_img;
*npixel = fmt->nbytes;
return STATUS_OK;
}


PLUGIN_STATUS decode_pre(void *context,
const uint8_t* rawdata, size_t rawsize, struct tilecfg_t *cfg)
{
Expand Down Expand Up @@ -133,7 +145,7 @@ EXPORT struct tile_decoder_t decoder = {
.version = 340, .size = sizeof(struct tile_decoder_t),
.msg = s_msg, .context = NULL,
.open = decode_open, .close = decode_close,
.decodeone = decode_pixel, .decodeall = NULL,
.decodeone = NULL, .decodeall = decode_pixels, // this example use decode_pixels
.pre = decode_pre, .post=decode_post,
.setui=NULL, .getui=NULL,
};
59 changes: 43 additions & 16 deletions src/core_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,31 +240,59 @@ int TileSolver::Decode(struct tilecfg_t *cfg, wxFileName pluginfile)
// decoding
auto datasize = PrepareTilebuf();
size_t ntile = m_tiles.size();
size_t nbytes = calc_tile_nbytes(&m_tilecfg.fmt);
if(datasize)
{
for(int i=0; i< ntile; i++)
if(decoder->decodeall)
{
auto& tile = m_tiles[i];
uint8_t *rgbdata = tile.GetData();
uint8_t *adata = tile.GetAlpha();
for(int y=0; y < m_tilecfg.h; y++)
size_t npixel;
struct pixel_t *pixels;
status = decoder->decodeall(context,
rawdata + start, datasize, &m_tilecfg.fmt, &pixels, &npixel, true);
for(int i=0; i< ntile; i++)
{
for(int x=0; x < m_tilecfg.w; x++)
size_t offset = i * nbytes;
if(offset + nbytes > npixel) break;
auto& tile = m_tiles[i];
uint8_t *rgbdata = tile.GetData();
uint8_t *adata = tile.GetAlpha();
for(int pixeli=0; pixeli < m_tilecfg.fmt.w * m_tilecfg.fmt.h; pixeli++)
{
struct tilepos_t pos = {i, x, y};
struct pixel_t pixel = {0};
status = decoder->decodeone(context, // lua function might not be in omp parallel
rawdata + start, datasize, &pos, &m_tilecfg.fmt, &pixel, false);
if(!PLUGIN_SUCCESS(status))
memcpy(rgbdata + pixeli*3, (void*)(pixels + pixeli), 3);
adata[pixeli] = pixels[pixeli].a; // alpah is seperate channel
}
}
}
else if(decoder->decodeone)
{
for(int i=0; i< ntile; i++)
{
auto& tile = m_tiles[i];
uint8_t *rgbdata = tile.GetData();
uint8_t *adata = tile.GetAlpha();
for(int y=0; y < m_tilecfg.h; y++)
{
for(int x=0; x < m_tilecfg.w; x++)
{
continue;
struct tilepos_t pos = {i, x, y};
struct pixel_t pixel = {0};
status = decoder->decodeone(context, // lua function might not be in omp parallel
rawdata + start, datasize, &pos, &m_tilecfg.fmt, &pixel, false);
if(!PLUGIN_SUCCESS(status))
{
continue;
}
auto pixeli = y * m_tilecfg.w + x;
memcpy(rgbdata + pixeli*3, &pixel, 3);
adata[pixeli] = pixel.a; // alpah is seperate channel
}
auto pixeli = y * m_tilecfg.w + x;
memcpy(rgbdata + pixeli*3, &pixel, 3);
adata[pixeli] = pixel.a; // alpah is seperate channel
}
}
}
else
{
wxLogError("[TileSolver::Decode] no decode function");
}
}
else
{
Expand Down Expand Up @@ -296,7 +324,6 @@ int TileSolver::Decode(struct tilecfg_t *cfg, wxFileName pluginfile)
NOTIFY_UPDATE_TILENAV();
NOTIFY_UPDATE_TILECFG();
}
size_t nbytes = calc_tile_nbytes(&m_tilecfg.fmt);
wxLogMessage(wxString::Format(
"[TileSolver::Decode] decode %zu tiles with %zu bytes, in %llu ms",
ntile, nbytes, (time_end - time_start).GetMilliseconds()));
Expand Down
6 changes: 3 additions & 3 deletions src/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ typedef PLUGIN_STATUS (*CB_decode_pixel)(void *context,
/**
* decode all pixels
* @param data, corrent decoding data
* @param pixel out all pixel bufer
* @param pixels out all pixel buffer, this should be alloced by the plugin
* @param npixel how many pixels for all tiles
* @param remain_index keep the origin index
*/
typedef PLUGIN_STATUS (*CB_decode_pixels)(void *context,
const uint8_t* data, size_t datasize,
const struct tilefmt_t, struct pixel_t *pixel,
const struct tilefmt_t *fmt, struct pixel_t *pixels[],
size_t *npixel, bool remain_index);

/**
Expand All @@ -162,7 +162,7 @@ typedef PLUGIN_STATUS (*CB_decode_parse)(void *context,
* make config widget to main ui, or get config from main ui
*/
typedef PLUGIN_STATUS (*CB_decode_config)(void *context,
const char *jsonstr, size_t jsonsize);
char *jsonstr, size_t jsonsize);

/**
* interface for C plugin
Expand Down

0 comments on commit 93b06be

Please sign in to comment.