A lightweight, terminal-driven image viewer written in C using the SDL2 graphics library. It reads a PPM (P6 binary) image from stdin and renders it pixel-by-pixel in a native desktop window.
- Renders PPM (P6 binary format) images using SDL2.
- Pixel-accurate rendering via
SDL_FillRect. - Reads directly from
stdin, i.e., no file path argument needed. - Auto-sizes the window to match the image dimensions.
- Minimal and dependency-light, just C standard library + SDL2.
- Language: C and its Libraries
- Tools: GCC, Neovim, Git, GIMP (To convert files into .ppm format)
| Library | Purpose |
|---|---|
SDL2 |
Window creation and pixel rendering |
stdio.h |
Standard I/O for reading PPM from stdin |
stdlib.h |
Memory allocation (calloc, free) |
image-viewer/
├── main.c # Source code
├── images/
├── image.ppm # Sample PPM image (Converted using GIMP)
└── image.webp # Original WebP source image
To get a copy of this project up and running on their local machine:
Make sure you have the following installed:
- GCC (or any C compiler)
- SDL2 development libraries
- Clone the repository:
git clone https://github.com/vishnuparihar2008/image-viewer.git- Navigate into the project directory:
cd ./image-viewer- Compile the project:
gcc -Wall -Wextra -g -o bin/main main.c `sdl2-config --cflags --libs`This compiles main.c and links the SDL2 library automatically.
Note
sdl2-config is standard for only Unix-like systems and might not compile on Windows etc.
- Run the project:
cat images/image.ppm | ./bin/main Note
cat is standard for only Unix-like systems and might not run on Windows etc.
This runs the viewer by piping a PPM image to the binary
A window will open at the image's native dimensions. Close it by clicking the X button or pressing Alt+F4.
Binary (Raw)
P6
# Optional comment
<width> <height>
255
<binary RGB pixel data...>
- Header Parsing — Reads the PPM header from
stdinusingfgetsto extract image width and height. - Window Creation — Creates an SDL2 window sized to the image dimensions.
- Pixel Rendering — Reads raw RGB bytes pixel-by-pixel using
getchar()and fills each 1×1 rectangle on the SDL surface. - Display & Event Loop — Calls
SDL_UpdateWindowSurface()to show the final image and polls for anSDL_QUITevent to exit.
- Scale it up to preview more than just .ppm files.
- Add a full-scrreen option.
- Implement basic pixel manipulation like:
- grayscale conversion
- brightness adjustment
Vishnu Parihar — vishnuparihar2008
This project is open source. Feel free to use, modify, and distribute.
