Skip to content

Commit

Permalink
Add extra align size, issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2brain committed Apr 10, 2024
1 parent 4a5cb90 commit 682c6cf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
13 changes: 13 additions & 0 deletions jpegli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const (
DCTFloat
)

const (
alignSize = 16
)

// EncodingOptions are the encoding parameters.
type EncodingOptions struct {
// Quality in the range [0,100]. Default is 75.
Expand Down Expand Up @@ -198,9 +202,18 @@ func yCbCrSize(r image.Rectangle, subsampleRatio image.YCbCrSubsampleRatio) (w,
ch = h
}

w = pad(w, alignSize) + alignSize
h = pad(h, alignSize) + alignSize
cw = pad(cw, alignSize) + alignSize
ch = pad(ch, alignSize) + alignSize

return
}

func pad(a int, b int) int {
return (a + (b - 1)) & (^(b - 1))
}

func init() {
image.RegisterFormat("jpeg", "\xff\xd8", Decode, DecodeConfig)
}
2 changes: 1 addition & 1 deletion jpegli_wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func decode(r io.Reader, configOnly, fancyUpsampling, blockSmoothing, arithCode
var data []byte

if configOnly {
data = make([]byte, 512)
data = make([]byte, 1024)
_, err = r.Read(data)
if err != nil {
return nil, cfg, fmt.Errorf("read: %w", err)
Expand Down
6 changes: 6 additions & 0 deletions lib/jpegli.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ typedef enum {
YCbCr410
} chroma;

void error_exit(j_common_ptr info) {
(*info->err->output_message)(info);
}

int decode(uint8_t *jpeg_in, int jpeg_in_size, int config_only, uint32_t *width, uint32_t *height, uint32_t *colorspace, uint32_t *chroma, uint8_t *out,
int fancy_upsampling, int block_smoothing, int arith_code, int dct_method, int tw, int th) {

struct jpeg_decompress_struct dinfo;

struct jpeg_error_mgr jerr;
dinfo.err = jpegli_std_error(&jerr);
dinfo.err->error_exit = error_exit;

jpegli_create_decompress(&dinfo);
jpegli_mem_src(&dinfo, jpeg_in, jpeg_in_size);
Expand Down Expand Up @@ -233,6 +238,7 @@ uint8_t* encode(uint8_t *in, int width, int height, int colorspace, int chroma,

struct jpeg_error_mgr jerr;
cinfo.err = jpegli_std_error(&jerr);
cinfo.err->error_exit = error_exit;

jpegli_create_compress(&cinfo);

Expand Down
Binary file modified lib/jpegli.wasm.gz
Binary file not shown.

0 comments on commit 682c6cf

Please # to comment.