forked from external-repos/squoosh
Format C / C++ with Chromium style
This commit is contained in:
committed by
Ingvar Stepanyan
parent
de543b3206
commit
a95cb740bf
2
.clang-format
Normal file
2
.clang-format
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
BasedOnStyle: Chromium
|
||||||
|
ColumnLimit: 120
|
||||||
@@ -1,36 +1,37 @@
|
|||||||
#include "emscripten/bind.h"
|
#include <emscripten/bind.h>
|
||||||
#include "emscripten/val.h"
|
#include <emscripten/val.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "libimagequant.h"
|
#include "libimagequant.h"
|
||||||
|
|
||||||
using namespace emscripten;
|
using namespace emscripten;
|
||||||
|
|
||||||
int version() {
|
int version() {
|
||||||
return (((LIQ_VERSION/10000) % 100) << 16) |
|
return (((LIQ_VERSION / 10000) % 100) << 16) | (((LIQ_VERSION / 100) % 100) << 8) |
|
||||||
(((LIQ_VERSION/100 ) % 100) << 8) |
|
(((LIQ_VERSION / 1) % 100) << 0);
|
||||||
(((LIQ_VERSION/1 ) % 100) << 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class RawImage {
|
class RawImage {
|
||||||
public:
|
public:
|
||||||
val buffer;
|
val buffer;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
|
|
||||||
RawImage(val b, int w, int h)
|
RawImage(val b, int w, int h) : buffer(b), width(w), height(h) {}
|
||||||
: buffer(b), width(w), height(h) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
liq_attr* attr;
|
||||||
liq_attr *attr;
|
liq_image* image;
|
||||||
liq_image *image;
|
liq_result* res;
|
||||||
liq_result *res;
|
|
||||||
uint8_t* result;
|
uint8_t* result;
|
||||||
RawImage quantize(std::string rawimage, int image_width, int image_height, int num_colors, float dithering) {
|
RawImage quantize(std::string rawimage,
|
||||||
|
int image_width,
|
||||||
|
int image_height,
|
||||||
|
int num_colors,
|
||||||
|
float dithering) {
|
||||||
const uint8_t* image_buffer = (uint8_t*)rawimage.c_str();
|
const uint8_t* image_buffer = (uint8_t*)rawimage.c_str();
|
||||||
int size = image_width * image_height;
|
int size = image_width * image_height;
|
||||||
attr = liq_attr_create();
|
attr = liq_attr_create();
|
||||||
@@ -38,12 +39,12 @@ RawImage quantize(std::string rawimage, int image_width, int image_height, int n
|
|||||||
liq_set_max_colors(attr, num_colors);
|
liq_set_max_colors(attr, num_colors);
|
||||||
liq_image_quantize(image, attr, &res);
|
liq_image_quantize(image, attr, &res);
|
||||||
liq_set_dithering_level(res, dithering);
|
liq_set_dithering_level(res, dithering);
|
||||||
uint8_t* image8bit = (uint8_t*) malloc(size);
|
uint8_t* image8bit = (uint8_t*)malloc(size);
|
||||||
result = (uint8_t*) malloc(size * 4);
|
result = (uint8_t*)malloc(size * 4);
|
||||||
liq_write_remapped_image(res, image, image8bit, size);
|
liq_write_remapped_image(res, image, image8bit, size);
|
||||||
const liq_palette *pal = liq_get_palette(res);
|
const liq_palette* pal = liq_get_palette(res);
|
||||||
// Turn palletted image back into an RGBA image
|
// Turn palletted image back into an RGBA image
|
||||||
for(int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
result[i * 4 + 0] = pal->entries[image8bit[i]].r;
|
result[i * 4 + 0] = pal->entries[image8bit[i]].r;
|
||||||
result[i * 4 + 1] = pal->entries[image8bit[i]].g;
|
result[i * 4 + 1] = pal->entries[image8bit[i]].g;
|
||||||
result[i * 4 + 2] = pal->entries[image8bit[i]].b;
|
result[i * 4 + 2] = pal->entries[image8bit[i]].b;
|
||||||
@@ -53,11 +54,8 @@ RawImage quantize(std::string rawimage, int image_width, int image_height, int n
|
|||||||
liq_result_destroy(res);
|
liq_result_destroy(res);
|
||||||
liq_image_destroy(image);
|
liq_image_destroy(image);
|
||||||
liq_attr_destroy(attr);
|
liq_attr_destroy(attr);
|
||||||
return {
|
return {val(typed_memory_view(image_width * image_height * 4, result)), image_width,
|
||||||
val(typed_memory_view(image_width*image_height*4, result)),
|
image_height};
|
||||||
image_width,
|
|
||||||
image_height
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const liq_color zx_colors[] = {
|
const liq_color zx_colors[] = {
|
||||||
@@ -81,15 +79,16 @@ const liq_color zx_colors[] = {
|
|||||||
uint8_t block[8 * 8 * 4];
|
uint8_t block[8 * 8 * 4];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ZX has one bit per pixel, but can assign two colours to an 8x8 block. The two colours must
|
* The ZX has one bit per pixel, but can assign two colours to an 8x8 block. The
|
||||||
* both be 'regular' or 'bright'. Black exists as both regular and bright.
|
* two colours must both be 'regular' or 'bright'. Black exists as both regular
|
||||||
|
* and bright.
|
||||||
*/
|
*/
|
||||||
RawImage zx_quantize(std::string rawimage, int image_width, int image_height, float dithering) {
|
RawImage zx_quantize(std::string rawimage, int image_width, int image_height, float dithering) {
|
||||||
const uint8_t* image_buffer = (uint8_t*) rawimage.c_str();
|
const uint8_t* image_buffer = (uint8_t*)rawimage.c_str();
|
||||||
int size = image_width * image_height;
|
int size = image_width * image_height;
|
||||||
int bytes_per_pixel = 4;
|
int bytes_per_pixel = 4;
|
||||||
result = (uint8_t*) malloc(size * bytes_per_pixel);
|
result = (uint8_t*)malloc(size * bytes_per_pixel);
|
||||||
uint8_t* image8bit = (uint8_t*) malloc(8 * 8);
|
uint8_t* image8bit = (uint8_t*)malloc(8 * 8);
|
||||||
|
|
||||||
// For each 8x8 grid
|
// For each 8x8 grid
|
||||||
for (int block_start_y = 0; block_start_y < image_height; block_start_y += 8) {
|
for (int block_start_y = 0; block_start_y < image_height; block_start_y += 8) {
|
||||||
@@ -99,7 +98,8 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
|
|||||||
int block_width = 8;
|
int block_width = 8;
|
||||||
int block_height = 8;
|
int block_height = 8;
|
||||||
|
|
||||||
// If the block hangs off the right/bottom of the image dimensions, make it smaller to fit.
|
// If the block hangs off the right/bottom of the image dimensions, make
|
||||||
|
// it smaller to fit.
|
||||||
if (block_start_y + block_height > image_height) {
|
if (block_start_y + block_height > image_height) {
|
||||||
block_height = image_height - block_start_y;
|
block_height = image_height - block_start_y;
|
||||||
}
|
}
|
||||||
@@ -125,10 +125,9 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
|
|||||||
for (int color_index = 0; color_index < 15; color_index++) {
|
for (int color_index = 0; color_index < 15; color_index++) {
|
||||||
liq_color color = zx_colors[color_index];
|
liq_color color = zx_colors[color_index];
|
||||||
|
|
||||||
// Using Euclidean distance. LibQuant has better methods, but it requires conversion to
|
// Using Euclidean distance. LibQuant has better methods, but it
|
||||||
// LAB, so I don't think it's worth it.
|
// requires conversion to LAB, so I don't think it's worth it.
|
||||||
int distance =
|
int distance = pow(color.r - image_buffer[pixel_start + 0], 2) +
|
||||||
pow(color.r - image_buffer[pixel_start + 0], 2) +
|
|
||||||
pow(color.g - image_buffer[pixel_start + 1], 2) +
|
pow(color.g - image_buffer[pixel_start + 1], 2) +
|
||||||
pow(color.b - image_buffer[pixel_start + 2], 2);
|
pow(color.b - image_buffer[pixel_start + 2], 2);
|
||||||
|
|
||||||
@@ -151,7 +150,8 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
|
|||||||
|
|
||||||
for (int color_index = 0; color_index < 15; color_index++) {
|
for (int color_index = 0; color_index < 15; color_index++) {
|
||||||
if (color_popularity[color_index] > highest_popularity) {
|
if (color_popularity[color_index] > highest_popularity) {
|
||||||
// Store this as the most popular pixel, and demote the current values:
|
// Store this as the most popular pixel, and demote the current
|
||||||
|
// values:
|
||||||
third_color_index = second_color_index;
|
third_color_index = second_color_index;
|
||||||
third_highest_popularity = second_highest_popularity;
|
third_highest_popularity = second_highest_popularity;
|
||||||
second_color_index = first_color_index;
|
second_color_index = first_color_index;
|
||||||
@@ -169,8 +169,8 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ZX images can't mix bright and regular colours, except black which appears in both.
|
// ZX images can't mix bright and regular colours, except black which
|
||||||
// Resolve any conflict:
|
// appears in both. Resolve any conflict:
|
||||||
while (1) {
|
while (1) {
|
||||||
// If either colour is black, there's no conflict to resolve.
|
// If either colour is black, there's no conflict to resolve.
|
||||||
if (first_color_index != 0 && second_color_index != 0) {
|
if (first_color_index != 0 && second_color_index != 0) {
|
||||||
@@ -183,12 +183,13 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If, during conflict resolving, we now have two of the same colour (because we initially
|
// If, during conflict resolving, we now have two of the same colour
|
||||||
// selected the bright & regular version of the same colour), retry again with the third
|
// (because we initially selected the bright & regular version of the
|
||||||
// most popular colour.
|
// same colour), retry again with the third most popular colour.
|
||||||
if (first_color_index == second_color_index) {
|
if (first_color_index == second_color_index) {
|
||||||
second_color_index = third_color_index;
|
second_color_index = third_color_index;
|
||||||
} else break;
|
} else
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quantize
|
// Quantize
|
||||||
@@ -200,13 +201,15 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
|
|||||||
liq_image_quantize(image, attr, &res);
|
liq_image_quantize(image, attr, &res);
|
||||||
liq_set_dithering_level(res, dithering);
|
liq_set_dithering_level(res, dithering);
|
||||||
liq_write_remapped_image(res, image, image8bit, size);
|
liq_write_remapped_image(res, image, image8bit, size);
|
||||||
const liq_palette *pal = liq_get_palette(res);
|
const liq_palette* pal = liq_get_palette(res);
|
||||||
|
|
||||||
// Turn palletted image back into an RGBA image, and write it into the full size result image.
|
// Turn palletted image back into an RGBA image, and write it into the
|
||||||
for(int y = 0; y < block_height; y++) {
|
// full size result image.
|
||||||
for(int x = 0; x < block_width; x++) {
|
for (int y = 0; y < block_height; y++) {
|
||||||
|
for (int x = 0; x < block_width; x++) {
|
||||||
int image8BitPos = y * block_width + x;
|
int image8BitPos = y * block_width + x;
|
||||||
int resultStartPos = ((block_start_y + y) * bytes_per_pixel * image_width) + ((block_start_x + x) * bytes_per_pixel);
|
int resultStartPos = ((block_start_y + y) * bytes_per_pixel * image_width) +
|
||||||
|
((block_start_x + x) * bytes_per_pixel);
|
||||||
result[resultStartPos + 0] = pal->entries[image8bit[image8BitPos]].r;
|
result[resultStartPos + 0] = pal->entries[image8bit[image8BitPos]].r;
|
||||||
result[resultStartPos + 1] = pal->entries[image8bit[image8BitPos]].g;
|
result[resultStartPos + 1] = pal->entries[image8bit[image8BitPos]].g;
|
||||||
result[resultStartPos + 2] = pal->entries[image8bit[image8BitPos]].b;
|
result[resultStartPos + 2] = pal->entries[image8bit[image8BitPos]].b;
|
||||||
@@ -221,11 +224,8 @@ RawImage zx_quantize(std::string rawimage, int image_width, int image_height, fl
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(image8bit);
|
free(image8bit);
|
||||||
return {
|
return {val(typed_memory_view(image_width * image_height * 4, result)), image_width,
|
||||||
val(typed_memory_view(image_width*image_height*4, result)),
|
image_height};
|
||||||
image_width,
|
|
||||||
image_height
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_result() {
|
void free_result() {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#include <emscripten/bind.h>
|
#include <emscripten/bind.h>
|
||||||
#include <emscripten/val.h>
|
#include <emscripten/val.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "jpeglib.h"
|
#include "jpeglib.h"
|
||||||
@@ -14,8 +14,9 @@ extern "C" {
|
|||||||
|
|
||||||
using namespace emscripten;
|
using namespace emscripten;
|
||||||
|
|
||||||
// MozJPEG doesn’t expose a numeric version, so I have to do some fun C macro hackery to turn it
|
// MozJPEG doesn’t expose a numeric version, so I have to do some fun C macro
|
||||||
// into a string. More details here: https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html
|
// hackery to turn it into a string. More details here:
|
||||||
|
// https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html
|
||||||
#define xstr(s) str(s)
|
#define xstr(s) str(s)
|
||||||
#define str(s) #s
|
#define str(s) #s
|
||||||
|
|
||||||
@@ -42,8 +43,8 @@ int version() {
|
|||||||
char buffer[] = xstr(MOZJPEG_VERSION);
|
char buffer[] = xstr(MOZJPEG_VERSION);
|
||||||
int version = 0;
|
int version = 0;
|
||||||
int last_index = 0;
|
int last_index = 0;
|
||||||
for(int i = 0; i < strlen(buffer); i++) {
|
for (int i = 0; i < strlen(buffer); i++) {
|
||||||
if(buffer[i] == '.') {
|
if (buffer[i] == '.') {
|
||||||
buffer[i] = '\0';
|
buffer[i] = '\0';
|
||||||
version = version << 8 | atoi(&buffer[last_index]);
|
version = version << 8 | atoi(&buffer[last_index]);
|
||||||
buffer[i] = '.';
|
buffer[i] = '.';
|
||||||
@@ -58,13 +59,12 @@ uint8_t* last_result;
|
|||||||
struct jpeg_compress_struct cinfo;
|
struct jpeg_compress_struct cinfo;
|
||||||
|
|
||||||
val encode(std::string image_in, int image_width, int image_height, MozJpegOptions opts) {
|
val encode(std::string image_in, int image_width, int image_height, MozJpegOptions opts) {
|
||||||
uint8_t* image_buffer = (uint8_t*) image_in.c_str();
|
uint8_t* image_buffer = (uint8_t*)image_in.c_str();
|
||||||
|
|
||||||
// The code below is basically the `write_JPEG_file` function from
|
// The code below is basically the `write_JPEG_file` function from
|
||||||
// https://github.com/mozilla/mozjpeg/blob/master/example.c
|
// https://github.com/mozilla/mozjpeg/blob/master/example.c
|
||||||
// I just write to memory instead of a file.
|
// I just write to memory instead of a file.
|
||||||
|
|
||||||
|
|
||||||
/* This struct contains the JPEG compression parameters and pointers to
|
/* This struct contains the JPEG compression parameters and pointers to
|
||||||
* working space (which is allocated as needed by the JPEG library).
|
* working space (which is allocated as needed by the JPEG library).
|
||||||
* It is possible to have several such structures, representing multiple
|
* It is possible to have several such structures, representing multiple
|
||||||
@@ -126,7 +126,7 @@ val encode(std::string image_in, int image_width, int image_height, MozJpegOptio
|
|||||||
*/
|
*/
|
||||||
jpeg_set_defaults(&cinfo);
|
jpeg_set_defaults(&cinfo);
|
||||||
|
|
||||||
jpeg_set_colorspace(&cinfo, (J_COLOR_SPACE) opts.color_space);
|
jpeg_set_colorspace(&cinfo, (J_COLOR_SPACE)opts.color_space);
|
||||||
|
|
||||||
if (opts.quant_table != -1) {
|
if (opts.quant_table != -1) {
|
||||||
jpeg_c_set_int_param(&cinfo, JINT_BASE_QUANT_TBL_IDX, opts.quant_table);
|
jpeg_c_set_int_param(&cinfo, JINT_BASE_QUANT_TBL_IDX, opts.quant_table);
|
||||||
@@ -146,17 +146,17 @@ val encode(std::string image_in, int image_width, int image_height, MozJpegOptio
|
|||||||
jpeg_c_set_bool_param(&cinfo, JBOOLEAN_TRELLIS_Q_OPT, opts.trellis_opt_table);
|
jpeg_c_set_bool_param(&cinfo, JBOOLEAN_TRELLIS_Q_OPT, opts.trellis_opt_table);
|
||||||
jpeg_c_set_int_param(&cinfo, JINT_TRELLIS_NUM_LOOPS, opts.trellis_loops);
|
jpeg_c_set_int_param(&cinfo, JINT_TRELLIS_NUM_LOOPS, opts.trellis_loops);
|
||||||
|
|
||||||
// A little hacky to build a string for this, but it means we can use set_quality_ratings which
|
// A little hacky to build a string for this, but it means we can use
|
||||||
// does some useful heuristic stuff.
|
// set_quality_ratings which does some useful heuristic stuff.
|
||||||
std::string quality_str = std::to_string(opts.quality);
|
std::string quality_str = std::to_string(opts.quality);
|
||||||
|
|
||||||
if (opts.separate_chroma_quality && opts.color_space == JCS_YCbCr) {
|
if (opts.separate_chroma_quality && opts.color_space == JCS_YCbCr) {
|
||||||
quality_str += "," + std::to_string(opts.chroma_quality);
|
quality_str += "," + std::to_string(opts.chroma_quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
char const *pqual = quality_str.c_str();
|
char const* pqual = quality_str.c_str();
|
||||||
|
|
||||||
set_quality_ratings(&cinfo, (char*) pqual, opts.baseline);
|
set_quality_ratings(&cinfo, (char*)pqual, opts.baseline);
|
||||||
|
|
||||||
if (!opts.auto_subsample && opts.color_space == JCS_YCbCr) {
|
if (!opts.auto_subsample && opts.color_space == JCS_YCbCr) {
|
||||||
cinfo.comp_info[0].h_samp_factor = opts.chroma_subsample;
|
cinfo.comp_info[0].h_samp_factor = opts.chroma_subsample;
|
||||||
@@ -191,8 +191,8 @@ val encode(std::string image_in, int image_width, int image_height, MozJpegOptio
|
|||||||
* Here the array is only one element long, but you could pass
|
* Here the array is only one element long, but you could pass
|
||||||
* more than one scanline at a time if that's more convenient.
|
* more than one scanline at a time if that's more convenient.
|
||||||
*/
|
*/
|
||||||
row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
|
row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride];
|
||||||
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
|
(void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Step 6: Finish compression */
|
/* Step 6: Finish compression */
|
||||||
@@ -228,8 +228,7 @@ EMSCRIPTEN_BINDINGS(my_module) {
|
|||||||
.field("chroma_subsample", &MozJpegOptions::chroma_subsample)
|
.field("chroma_subsample", &MozJpegOptions::chroma_subsample)
|
||||||
.field("auto_subsample", &MozJpegOptions::auto_subsample)
|
.field("auto_subsample", &MozJpegOptions::auto_subsample)
|
||||||
.field("separate_chroma_quality", &MozJpegOptions::separate_chroma_quality)
|
.field("separate_chroma_quality", &MozJpegOptions::separate_chroma_quality)
|
||||||
.field("chroma_quality", &MozJpegOptions::chroma_quality)
|
.field("chroma_quality", &MozJpegOptions::chroma_quality);
|
||||||
;
|
|
||||||
|
|
||||||
function("version", &version);
|
function("version", &version);
|
||||||
function("encode", &encode);
|
function("encode", &encode);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
|
#include <string>
|
||||||
#include "emscripten/bind.h"
|
#include "emscripten/bind.h"
|
||||||
#include "emscripten/val.h"
|
#include "emscripten/val.h"
|
||||||
#include "src/webp/decode.h"
|
#include "src/webp/decode.h"
|
||||||
#include "src/webp/demux.h"
|
#include "src/webp/demux.h"
|
||||||
#include <string>
|
|
||||||
|
|
||||||
using namespace emscripten;
|
using namespace emscripten;
|
||||||
|
|
||||||
@@ -11,24 +11,19 @@ int version() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class RawImage {
|
class RawImage {
|
||||||
public:
|
public:
|
||||||
val buffer;
|
val buffer;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
|
|
||||||
RawImage(val b, int w, int h)
|
RawImage(val b, int w, int h) : buffer(b), width(w), height(h) {}
|
||||||
: buffer(b), width(w), height(h) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
uint8_t* last_result;
|
uint8_t* last_result;
|
||||||
RawImage decode(std::string buffer) {
|
RawImage decode(std::string buffer) {
|
||||||
int width, height;
|
int width, height;
|
||||||
last_result = WebPDecodeRGBA((const uint8_t*)buffer.c_str(), buffer.size(), &width, &height);
|
last_result = WebPDecodeRGBA((const uint8_t*)buffer.c_str(), buffer.size(), &width, &height);
|
||||||
return RawImage(
|
return RawImage(val(typed_memory_view(width * height * 4, last_result)), width, height);
|
||||||
val(typed_memory_view(width*height*4, last_result)),
|
|
||||||
width,
|
|
||||||
height
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_result() {
|
void free_result() {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#include <emscripten/bind.h>
|
#include <emscripten/bind.h>
|
||||||
#include <emscripten/val.h>
|
#include <emscripten/val.h>
|
||||||
#include "src/webp/encode.h"
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include "src/webp/encode.h"
|
||||||
|
|
||||||
using namespace emscripten;
|
using namespace emscripten;
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ int version() {
|
|||||||
uint8_t* last_result;
|
uint8_t* last_result;
|
||||||
|
|
||||||
val encode(std::string img, int width, int height, WebPConfig config) {
|
val encode(std::string img, int width, int height, WebPConfig config) {
|
||||||
uint8_t* img_in = (uint8_t*) img.c_str();
|
uint8_t* img_in = (uint8_t*)img.c_str();
|
||||||
|
|
||||||
// A lot of this is duplicated from Encode in picture_enc.c
|
// A lot of this is duplicated from Encode in picture_enc.c
|
||||||
WebPPicture pic;
|
WebPPicture pic;
|
||||||
@@ -35,7 +35,7 @@ val encode(std::string img, int width, int height, WebPConfig config) {
|
|||||||
|
|
||||||
WebPMemoryWriterInit(&wrt);
|
WebPMemoryWriterInit(&wrt);
|
||||||
|
|
||||||
ok = WebPPictureImportRGBA(&pic, (uint8_t*) img_in, width * 4) && WebPEncode(&config, &pic);
|
ok = WebPPictureImportRGBA(&pic, (uint8_t*)img_in, width * 4) && WebPEncode(&config, &pic);
|
||||||
WebPPictureFree(&pic);
|
WebPPictureFree(&pic);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
WebPMemoryWriterClear(&wrt);
|
WebPMemoryWriterClear(&wrt);
|
||||||
@@ -51,14 +51,12 @@ void free_result() {
|
|||||||
WebPFree(last_result);
|
WebPFree(last_result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(my_module) {
|
EMSCRIPTEN_BINDINGS(my_module) {
|
||||||
enum_<WebPImageHint>("WebPImageHint")
|
enum_<WebPImageHint>("WebPImageHint")
|
||||||
.value("WEBP_HINT_DEFAULT", WebPImageHint::WEBP_HINT_DEFAULT)
|
.value("WEBP_HINT_DEFAULT", WebPImageHint::WEBP_HINT_DEFAULT)
|
||||||
.value("WEBP_HINT_PICTURE", WebPImageHint::WEBP_HINT_PICTURE)
|
.value("WEBP_HINT_PICTURE", WebPImageHint::WEBP_HINT_PICTURE)
|
||||||
.value("WEBP_HINT_PHOTO", WebPImageHint::WEBP_HINT_PHOTO)
|
.value("WEBP_HINT_PHOTO", WebPImageHint::WEBP_HINT_PHOTO)
|
||||||
.value("WEBP_HINT_GRAPH", WebPImageHint::WEBP_HINT_GRAPH)
|
.value("WEBP_HINT_GRAPH", WebPImageHint::WEBP_HINT_GRAPH);
|
||||||
;
|
|
||||||
|
|
||||||
value_object<WebPConfig>("WebPConfig")
|
value_object<WebPConfig>("WebPConfig")
|
||||||
.field("lossless", &WebPConfig::lossless)
|
.field("lossless", &WebPConfig::lossless)
|
||||||
@@ -87,8 +85,7 @@ EMSCRIPTEN_BINDINGS(my_module) {
|
|||||||
.field("near_lossless", &WebPConfig::near_lossless)
|
.field("near_lossless", &WebPConfig::near_lossless)
|
||||||
.field("exact", &WebPConfig::exact)
|
.field("exact", &WebPConfig::exact)
|
||||||
.field("use_delta_palette", &WebPConfig::use_delta_palette)
|
.field("use_delta_palette", &WebPConfig::use_delta_palette)
|
||||||
.field("use_sharp_yuv", &WebPConfig::use_sharp_yuv)
|
.field("use_sharp_yuv", &WebPConfig::use_sharp_yuv);
|
||||||
;
|
|
||||||
|
|
||||||
function("version", &version);
|
function("version", &version);
|
||||||
function("encode", &encode);
|
function("encode", &encode);
|
||||||
|
|||||||
Reference in New Issue
Block a user