saitoha / libsixel

A SIXEL encoder/decoder implementation derived from kmiya's sixel (https://github.com/saitoha/sixel).
MIT License
2.51k stars 83 forks source link

problems using sixel_encode with SIXEL_PIXELFORMAT_ARGB8888 #60

Open olaf-rogalsky opened 7 years ago

olaf-rogalsky commented 7 years ago

When encoding a ARGB8888 image 2 times in sequence, then the output from the second call is wrong and differs from the first call.

I've observed, that sometimes the image data is corrupted after the first call as well, but I can't reproduce the effect.

Olaf

Screenshot (both outputs): screenshot

Example Code

#include <stdlib.h>
#include <stdio.h>
#include <sixel.h>

int sixel_write(char *data, int size, void *priv) {
  return fwrite(data, 1, size, stdout);
}

int main(int argc, char **argv) {
  sixel_output_t *o_ctx;
  sixel_dither_t *d_ctx;
  unsigned char* data;
  int i;
  int w = 13;
  int h = 13;
  int bpp = 4;
  int format = SIXEL_PIXELFORMAT_ARGB8888;

  /* Create some arbitrary image. */
  data = calloc(1, bpp * w * h + 1);
  for (i = 0; i < bpp * w * h; i++) data[i] = 0x20 + (i % 0x40);

  /* Initalize output and dither context. */
  sixel_output_new(&o_ctx, &sixel_write, 0, 0);
  sixel_dither_new(&d_ctx, 100, 0);
  sixel_dither_initialize(d_ctx, data, w, h,
                          format, SIXEL_LARGE_AUTO, SIXEL_REP_AUTO, SIXEL_QUALITY_AUTO);

  /* The first call to sixel_encode is OK. */
  sixel_encode(data, w, h, 0, d_ctx, o_ctx);

  /* Output is different than in previous call. */
  /* With an additional sixel_dither_initialize both outputs are the same. */
  sixel_encode(data, w, h, 0, d_ctx, o_ctx);

  return 0;
}
saitoha commented 7 years ago

@olaf-rogalsky It's my API-design failure... Content of pixel buffer(data) is not immutable. it can be changed by sixel_encode(). I should rename this function to sixel_encode_from_mutable_buffer(). Furthermore I think new API named such as sixel_encode_from_immutable_buffer() is needed. For the present, I'm going to document about this matter...

olaf-rogalsky commented 7 years ago

Ah, OK, thanks for the explanation.

By the way: As a long term, stubborn terminal and shell user I really enjoy your efforts in mainstreaming SIXEL graphics :-).