Created
March 22, 2017 03:34
-
-
Save zephray/a7a7fb0da303ced138d7a8d068e189cf to your computer and use it in GitHub Desktop.
ebm image viewer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL.h> | |
#include <stdio.h> | |
#define RGB(r, g, b) (((r)<<16)|((g)<<8)|(b)) | |
void displayImage(SDL_Surface *surface, uint8_t *image) { | |
uint32_t *pixels = (uint32_t *)surface->pixels; | |
uint32_t pixel; | |
uint16_t raw; | |
uint8_t y; | |
for (int i = 0; i < 530; i++) { | |
for (int j = 0; j < 400; j++) { | |
raw = image[i * 400 + j]; | |
y = (raw >> 4); | |
y = ~(y << 4 | y); | |
pixel = RGB(y, y, y); | |
pixels[i * 800 + j * 2] = pixel; | |
y = (raw & 0x0F); | |
y = ~(y << 4 | y); | |
pixel = RGB(y, y, y); | |
pixels[i * 800 + j * 2 + 1] = pixel; | |
} | |
} | |
} | |
long fileLoad(const char * filename, unsigned char * * buffer, long size = 0) { | |
FILE * pFile; | |
long lSize; | |
pFile = fopen(filename, "rb"); | |
if (pFile != NULL) { | |
fseek(pFile, 0, SEEK_END); | |
lSize = ftell(pFile); | |
rewind(pFile); | |
if (size != 0) | |
lSize = size; | |
*buffer = (unsigned char *)malloc(lSize); | |
if (buffer != NULL) { | |
return fread(*buffer, 1, lSize, pFile); | |
} | |
fclose(pFile); | |
} else { | |
*buffer = (unsigned char *)malloc(size); | |
} | |
return 0; | |
} | |
int main(int argc, char* args[]) | |
{ | |
SDL_Window* window = NULL; | |
SDL_Surface* screenSurface = NULL; | |
SDL_Surface* display; | |
SDL_Event event; | |
uint8_t* file; | |
int quit = 0; | |
long fileRead; | |
if (argc != 2) { | |
printf("Usage: SDLTest image.ebm\n"); | |
return 1; | |
} | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) | |
{ | |
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); | |
return 1; | |
} | |
window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN); | |
screenSurface = SDL_GetWindowSurface(window); | |
display = SDL_CreateRGBSurface(0, 800, 600, 32, 0, 0, 0, 0); | |
fileRead = fileLoad(args[1], &file, 212000); | |
if (fileRead != 212000) { | |
printf("Error: wrong file size. Size: %d\n", fileRead); | |
} | |
displayImage(display, file); | |
SDL_BlitSurface(display, NULL, screenSurface, NULL); | |
SDL_UpdateWindowSurface(window); | |
while (!quit) { | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
quit = 1; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment