For fun, I want to write my own image format. I'm going to have to draw pixels to a Window, but I haven't found any nice, simple, light librarys that can do this. I've thought about using OpenGL, but it's kind of messy and over-complicated to write code for and a lot of the time it doesn't seem to agree with me. All I need the graphics library to do is initialise a window for me, and let me feed it an X,Y and RGB so I can plot pixels.

Any suggestions?

Dani AI

Generated

's CImg suggestion is a very practical choice for this task: it is a single-header, easy-to-drop-into-C++ library that handles image buffers and provides simple on-screen display routines — ideal for prototyping a custom image format when C++ is fine. CImg. (cimg.eu)

Other lightweight options, with trade-offs:

  • SDL2 (C): cross-platform, low-level windowing + pixel uploads (use streaming textures or locking for best speed). Good for a pure-C approach. (wiki.libsdl.org)
  • SFML (C++): higher-level C++ API; update a texture from a pixel array with Texture::update. Easy if staying in C++. (en.sfml-dev.org)
  • GLFW + OpenGL: tiny window/context provider; upload a CPU buffer to a GL texture with glTexSubImage2D if GPU operations are desired. (glfw.org)
  • [Allegro] (Allegro 5): simple C API but per-pixel calls (e.g., al_put_pixel) are slow — prefer locked memory bitmaps or batch updates. (allegro5.org)

Typical, efficient workflow (pattern to follow):

  • Keep a single CPU pixel buffer (e.g., uint32_t pixels[w*h]).
  • Write pixels by index: pixels[y*width + x] = packed_color.
  • Push the whole buffer to the window once per frame (SDL_UpdateTexture / SDL_LockTexture + SDL_UnlockTexture, SFML Texture::update, or glTexSubImage2D). This avoids per-pixel API calls and is far faster. (wiki.libsdl.org)

Troubleshooting and tips:

  • Mind pixel format and endianness (RGBA vs BGRA) and the texture's expected format.
  • Watch pitch/stride when copying rows.
  • Coordinate origin differs (many windowing APIs use top-left; OpenGL texture uploads may be bottom-left).
  • For saving files while developing a format, use a tiny single-file writer like stb_image_write. stb. (github.com)

Recommendation: for a C-based, minimal route pick SDL2 + a CPU pixel buffer; for quick C++ prototyping accept 's CImg or SFML for brevity.

Recommended Answers

All 2 Replies

CImg springs to mind. Here is a complete working program to draw a 500x400 image on screen, with one pixel set to green. It is C++, though; have you got your heart set on doing it completely in C?

#include "CImg.h"
#include <iostream>
using namespace cimg_library;

int main()
{
    // create image size 500x400x1,3 channels (RGB) default colour white
    CImg<unsigned char>  theImage(500,400,1,3,1);

    // Now change one pixel value
    theImage(10,10,1) = 255; // different colour on pixel 10, 10- green to max


    CImgDisplay main_disp(theImage); // display it
    std::cin.ignore(); //wait
}
commented: Perfect answer, cheers! +2

Wow, that's abolutely perfect! Precisely what I was looking for. Yeah the fact that it's in C++ is absolutely fine, doesn't worry me in the slightest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.