aboutsummaryrefslogtreecommitdiff
path: root/extras/webp_to_sdl.c
blob: 971ffc8874d0a96d9570ae3453384307d209d3b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the COPYING file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS. All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
// -----------------------------------------------------------------------------
//
//  Simple WebP-to-SDL wrapper. Useful for emscripten.
//
// Author: James Zern (jzern@google.com)

#ifdef HAVE_CONFIG_H
#include "src/webp/config.h"
#endif

#if defined(WEBP_HAVE_SDL)

#include "webp_to_sdl.h"

#include <stdio.h>

#include "src/webp/decode.h"

#if defined(WEBP_HAVE_JUST_SDL_H)
#include <SDL.h>
#else
#include <SDL2/SDL.h>
#endif

static int init_ok = 0;
int WebPToSDL(const char* data, unsigned int data_size) {
  int ok = 0;
  VP8StatusCode status;
  WebPBitstreamFeatures input;
  uint8_t* output = NULL;
  SDL_Window* window = NULL;
  SDL_Renderer* renderer = NULL;
  SDL_Texture* texture = NULL;
  int width, height;

  if (!init_ok) {
    SDL_Init(SDL_INIT_VIDEO);
    init_ok = 1;
  }

  status = WebPGetFeatures((uint8_t*)data, (size_t)data_size, &input);
  if (status != VP8_STATUS_OK) goto Error;
  width = input.width;
  height = input.height;

  SDL_CreateWindowAndRenderer(width, height, 0, &window, &renderer);
  if (window == NULL || renderer == NULL) {
    fprintf(stderr, "Unable to create window or renderer!\n");
    goto Error;
  }
  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY,
              "linear");  // make the scaled rendering look smoother.
  SDL_RenderSetLogicalSize(renderer, width, height);

  texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888,
                              SDL_TEXTUREACCESS_STREAMING, width, height);
  if (texture == NULL) {
    fprintf(stderr, "Unable to create %dx%d RGBA texture!\n", width, height);
    goto Error;
  }

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  output = WebPDecodeBGRA((const uint8_t*)data, (size_t)data_size, &width,
                          &height);
#else
  output = WebPDecodeRGBA((const uint8_t*)data, (size_t)data_size, &width,
                          &height);
#endif
  if (output == NULL) {
    fprintf(stderr, "Error decoding image (%d)\n", status);
    goto Error;
  }

  SDL_UpdateTexture(texture, NULL, output, width * sizeof(uint32_t));
  SDL_RenderClear(renderer);
  SDL_RenderCopy(renderer, texture, NULL, NULL);
  SDL_RenderPresent(renderer);
  ok = 1;

 Error:
  // We should call SDL_DestroyWindow(window) but that makes .js fail.
  SDL_DestroyRenderer(renderer);
  SDL_DestroyTexture(texture);
  WebPFree(output);
  return ok;
}

//------------------------------------------------------------------------------

#endif  // WEBP_HAVE_SDL