The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
8bit Grayscale image not displayed correctly
Sascha Schwarz


Joined: 28 Apr 2015
Posts: 5
Location: Germany
Hi all.
I am having trouble displaying this image:

I am creating a texture (SDL2-image 2.0.0) and displaying it (SDL2-2.0.3) and was expecting SDL to "Just Do the Right Thing"(tm).
But somehow/somewhere the colors get changed and I am ending up with an odd looking yellowish/white gradient.

I can get it to work when I change my png from 8-bit grayscale to something else, but shouldn't it work with the picture at hand?

For your convenience, this is the code I use:
Code:
#include <memory>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

using namespace std;

struct windest{ void operator()(SDL_Window* w) const { SDL_DestroyWindow(w); } };
struct rendest{ void operator()(SDL_Renderer* r) const { SDL_DestroyRenderer(r); } };
struct texdest{ void operator()(SDL_Texture* t) const { SDL_DestroyTexture(t); } };

using window = unique_ptr<SDL_Window, windest>;
using renderer = unique_ptr<SDL_Renderer, rendest>;
using texture = unique_ptr<SDL_Texture, texdest>;

const window& get_window() {
  const auto pos = SDL_WINDOWPOS_UNDEFINED;
  static window w{SDL_CreateWindow(nullptr, pos, pos, 640, 320, 0)};
  return w;
}

const renderer& get_renderer() {
  static renderer r{SDL_CreateRenderer(get_window().get(), -1, 0)};
  return r;
}

void display(const texture& tex) {
  auto&& r = get_renderer().get();
  SDL_RenderClear(r);
  SDL_RenderSetLogicalSize(r, 20, 10);
  SDL_RenderCopy(r, tex.get(), nullptr, nullptr);
  SDL_RenderPresent(r);
}

struct initializer {
  initializer() { SDL_Init(SDL_INIT_VIDEO); }
  ~initializer() { SDL_Quit(); }
} global_sdl;

int main(int argc, char *argv[]) {
  if(argc!=2)
    return -1;
  texture t{IMG_LoadTexture(get_renderer().get(), argv[1])};
  if(!t)
    return -1;
  display(t);
  SDL_Delay(1000);
}