3

I am trying to show a JPEG to a ANativeWindow with the Android NDK. I am getting the ANativeWindow* by doing:

_window = ANativeWindow_fromSurface(env, surface)

I am loading the jpeg, with libjpeg-turbo, by doing:

if (tjDecompressHeader2(tj, jpeg, jpegSize, &width, &height, &subsamp) == 0) {
    int format = TJPF_ARGB;
    int pitch  = _windowWidth * tjPixelSize[format];

    _bufferSize = pitch * _windowHeight;
    _buffer = realloc(_buffer, _bufferSize);
    tjDecompress2(tj, jpeg, jpegSize, _buffer, _windowWidth, pitch, _windowHeight, format, 0);
}

My Question is, how to show the decoded jpeg on the surface ? I am currently doing this:

ANativeWindow_Buffer surface_buffer;
if (ANativeWindow_lock(_window, &surface_buffer, NULL) == 0) {
    memcpy(surface_buffer.bits, _buffer,  _bufferSize);
    ANativeWindow_unlockAndPost(_window);
}

But the result (see below) is not what I was expecting. What should I do before sending the buffer to the surface ?

snapshot

Thanks

Theolodis
  • 4,977
  • 3
  • 34
  • 53
François
  • 123
  • 1
  • 10
  • Can you give more detail about what the result was? – krsteeve Aug 14 '13 at 19:59
  • I added a snapshot to the question of what is the current result. – François Aug 19 '13 at 16:01
  • 1
    it might be the window format. I noticed your decoding to ARGB, it looks like the supported window formats are: WINDOW_FORMAT_RGBA_8888, WINDOW_FORMAT_RGBX_8888, WINDOW_FORMAT_RGB_565 – krsteeve Aug 19 '13 at 18:46
  • Thanks, I already tried to change the decompress format but never tried to set the ANativeWindow's one with ANativeWindow_setBuffersGeometry(). – François Aug 20 '13 at 09:38
  • No problem! I guess I should have made my suggestion an answer... – krsteeve Aug 20 '13 at 11:07

1 Answers1

4

Just need to set ANativeWindow's format with ANativeWindow_setBuffersGeometry(_window, 0, 0, WINDOW_FORMAT_RGBA_8888). And then use TJPF_RGBA format instead of TJPF_ARGB.

François
  • 123
  • 1
  • 10