r/cpp • graphics and network things • Apr 05 '18

2D Graphics : A More Modest Proposal

Like a lot of folks, I have been following the story of the 2D Graphics ideas for inclusion in C++. Today, there was a post summing up the history of how it ran into trouble: https://www.reddit.com/r/cpp/comments/89q6wr/sg13_2d_graphics_why_it_failed/ My own tldr is basically that graphics is potentially a huge topic, and people tried to cram a lot of graphics functionality into C++ as a first step. A lot of it seemed to be focused on trying to paint a house that hadn't been built yet. Without a simple, completely generic foundation to build fancy features on top of, it wasn't super useful to get off in the weeds of how exactly you would display an image or draw onto it. As people worked on various iterations of proposals, people generally agreed that graphics are cool, and some sort of graphics support could be quite useful, and generally asked, "If this proposal isn't what you want, what do you propose instead?"

So, I haven't put a ton of thought into this, and I haven't fleshed it out for a formal paper with a precise spec, I'd love to toss a pitch at the Reddit community and see what people think of my spin on this topic. Perhaps it'll evolve into something that I or somebody else actually formalizes into a paper for a proposal.

So, what would I propose? An intentionally very narrow scope. Basically, the dumbest, stupidest, simplest image container that is marginally useful.

A very simple container would consist of integers for width and height, storage for the pixels, a minimum metadata describing the image content, and some simple accessor functions for getting and setting pixels. And not much else.

It would support uint8 and float32 data types with n channels.

  • How would I display it?

I dunno, with some GUI API. I don't care and I don't have to. Concepts like windows and screens are explicitly out of scope for what I would propose. The idea is that one could write completely generic image processing code to create an image, and then GUI API's like Qt or WxWidgets would be easy to adapt to using the new image class. Qt's QString provides a QString::fromStdString() function. I would expect a future version of QImage would support something like QImage::fromStdImage() in a similar manner. The glue code to adapt a standard image to some new GUI API would be quite simple, so you could write your image handling code and use whatever GUI system you would like, or is most appropriate for your platform quite easily.

  • So this proposal doesn't include GUI?

Not merely "No," but rather emphatically, "Hell no." It constitutes a massive expansion of scope that is easily underestimated, and requires interacting with things like windowing systems that will likely be much harder to integrate into other GUI systems than a simple image class would be since it would need concepts like an event loop that could interfere with an application's main event loop by blocking it or swallowing events. Having to figure out how to do this well shouldn't block the additional of a simple image class.

  • Is it GPU accelerated?

Maybe. Mostly no. C++ has no concept of a GPU at present. I think that an image spec is the wrong place to introduce it. If you are using something like Cuda which compiles C++ code for GPU execution, then drawing using the "std::image" would presumably be made as performant as possible by the compiler vendor. But there is no way to enumerate GPU's, tell the new operator to allocate memory in a GPU's address space, etc. That can all be added independently of an image class, then the image class would be possible to allocate in the GPU memory, and acted on using some GPU drawing API.

API's such as OpenGL and Vulkan could easily interoperate with the image class with a bit of glue, such that you could upload a std::image for use as a texture, or read a GPU framebuffer into host memory and get the result as a std::image. std::image would not, itself have a concept for expressing a graphics API's texture handle or the like as the image.

  • How would I load such an image from disk

I dunno, use some library. I like OpenImageIO.

And here's where the utility of std::image appears, despite the emphatic lack of features! If you can use your favorite library to load an image into memory, and you can use your favorite GUI API for displaying it, then your favorite image related libraries can suddenly all talk to each other. std::image becomes the lingua franca between OpenImageIO, Qt, Wx Widgets, OpenCV, Vulkan, etc., etc., in a C++ application. You no longer need to worry about n-factorial glue wrappers between various image formats as you add libraries to your application to be able to bounce from any API to any API.

  • Copying is slow

The API will need some support for creating an image by copying an existing image, or by creating a non-owning view, or by taking ownership of an existing pixel storage. It'll also need an API for "un-owning" pixel storage. In this way, an OpenImageIO class can be made into a std::image, and then stuffed into a QImage for display without ever copying the pixels. The exact API needs a little thinking. There would still be a little overhead of constructing/destructing three class instances to shuffle one image around. But that overhead isn't going to be incredibly high compared to copying the pixel data.

  • Pixel storage

Chunky pixels - not planar. Support approximating planar data storage if you need it using a std::vector<std::image> with each image in the vector storing on plane. 1-n channels would be supported, though 4 channel RGBA images would probably be the most common. All channels would be the same resolution.

  • Space filling curves, tile based access, etc

Textures are often stored in wacky formats. Vulkan has a concept of "Optimal" storage for an image based on what's most efficient to sample such that geometrically nearby texels are stored in nearby memory locations to improve locality of access in caches. Some image formats like EXR store images in tiles which can be sparsely populated. (pg. 9)

None of that would have any explicit support in my concept of std::image. It's just a linear, flat, contiguous buffer of pixels in-order. Don't let the perfect be the enemy of the good here. You are welcome to store pixels in some z-curve order that suits your application, and anything that processes individual pixels would let it pass unmolested. (Like color corrections. Doubling the brightness of every pixel doesn't care what order the pixels are stored in, as long as the consistent chunky storage is consistent.) If the place you send your pixels supports that format, good for you. If not, it'll probably look wrong and you'll have nobody to blame but yourself.

  • Colorspaces

I think a std::string with a color space name would be about as well as you can do for something at this level. If you are using something like OpenColorIO, there may be a whole range of color configurations that only exist on a user's machine at runtime. So trying to make a set of enums or template classes based on color spaces would be a failure because it's completely impossible to know what colorspaces a user will be dealing with at compile time. It's expected that a user may wind up using colorspaces that the developer of an application never heard of in his life. Specifying colorspaces thoroughly is way, way, way, way outside of the scope of the C++ language. Just the Documentation Guide for ACES colorspace (TB-2014-001) is nine pages, and that's just the document that refers you to the documentation for other specific specs! (And ACES actually specifies several color spaces in addition to the main 2065-1 spec.) The sRGB spec is apparently 16 pages. Rec.709, Log spaces, RedCine, Raw spaces, Rec.601, Linear with sRGB primaries, Alexa, scene-referred vs. display-referred spaces, etc, should all rightly be outside the scope of the C++ language. Doing good color space handling requires more pages of spec than the rest of C++, some of the specs cost money, some of the specs are underspecified or proprietary, etc.

Ten years ago, you might reasonably argue that it would be useful just to explicitly support sRGB and call it a day. Today ordinary consumer equipment supports weird HDR modes, and even the dumbest GPU on the market supports floating point texture input even if some still don't support more than 8 bit output. Nailing it down too much isn't just a waste of effort, it's actively pounding a hole in your own foot.

I'd make a std::string field for the colorspace and mention it could be populated with strings like "sRGB." It's up to application developers to do sensible things with their pixels. This is a storage container, not training wheels.

  • What about YUV?

Decimated-channel formats like YUV-4:2:0 would not be supported. (Though you could make a std::vector<std::image> where the first image is full res luma and the next two are half res chroma channels, as mentioned above for planar storage. I'm not gonna fight you. But the developer is responsible for doing something useful with that vector of image planes.)

Full res YUV can be stored in chunky pixel format. A lot of stuff will assume it's RGB, but I don't care. This proposal doesn't have any operations on the image in it -- just the container. Some later proposal might suggest operators that would care about the contents more deeply.

  • What about 10 bit images?

No special effort to deal with odd integer formats. 8 bit is obviously extremely common so it would be silly not to support it. Past that, just use float. Maybe somebody could talk me into the importance of 16 or 32 bit integer storage, but it'd frankly be a fairly uncommon use case to require integer storage of many bits. I know it's not unheard of, but I think there's logic in covering the 90% use case as easily as possible, rather than making things harder on the 90% for the sake of some use cases that will be rare.

Gosh, I have already gone on way longer than I expected about this. (The implementation would be much shorter than my attempt to justify implementing it!) I am curious to hear people's thoughts about this baby step into graphics for C++. Am I way off-base? This seems like exactly the sort of thing that would be the right first step that all of the other stuff that people want could be built to pass around. Since graphics is in the air today, I am curious to hear what other people think about trying massively narrow the scope and start with the minimum thing that would be useful to the most people.

2 Upvotes

50 comments sorted by

View all comments

33

u/johannes1971 Apr 05 '18

So you want to standardize std::vector<uint8_t> as an image format. I'm sorry, how is that helping anybody?

  • The concept of a container for pixels is not generally a problem that needs solving. It has already been solved, and considerably better than this.

  • This approach is incorrect anyway: you want an image format that doesn't need some kind of conversion step before hitting the screen. That means adjusting to whatever the OS demands of you.

  • This also does not meet the goal of getting beginners to easily get something on the screen. Not unless you want to start them by implementing their own line-drawing algorithms and whatever.

  • There are already numerous highly capable graphics libraries out there that could be used (whether standardized or not!), and are quite frankly a thousand times more attractive than having this kind of garbage in the standard.

The naive approach taken to graphics by people working on possible standardisation stands in sharp contrast to the incredibly detailed approach taken in other areas, such as threads, atomics, sockets, file IO, etc. I attribute this to lack of experience in that particular field by the people involved: they simply don't have the knowledge necessary to come up with an acceptable 2D API. Given this clear lack of involvement by professionals in the field of 2D graphics, it would be a much better choice to just leave it alone. If we need a library for helping students get up to speed, provide it as a separate tool, and don't make it part of the standard.

What the C++ community needs is a way to install that library. What we need is package management: some way for students, and professionals, to add additional libraries to their projects without spending days figuring out dependencies, build errors, and compiler settings. Once we have that, we'll have plenty of choices for whatever library we want to use.

> c++pm install https://www.curl.com/latest $HOME/packages/curl/latest
> c++pm add $HOME/packages/curl/latest $HOME/projects/my_project
> c++pm update $HOME/projects/my_project

That is what we need. Can we please have that?

3

u/wrosecrans graphics and network things Apr 05 '18

So you want to standardize std::vector<uint8_t> as an image format. I'm sorry, how is that helping anybody?

By providing a standard class that can be built on in the future, and used for interop between libraries. Also, I clearly stated that it would need more than 8 bit support and suggest that there would be a float type as well.

The concept of a container for pixels is not generally a problem that needs solving. It has already been solved, and considerably better than this.

It has been solved by every single library that deals with this stuff, in different ways because the standard is silent about it. Having so many examples of people reinventing the wheel implies that it's clearly the sort of thing that should be considered for standardisation.

This approach is incorrect anyway: you want an image format that doesn't need some kind of conversion step before hitting the screen. That means adjusting to whatever the OS demands of you.

The example I gave of a QImage constructed with the contents of a std::image would be trivial to display, without needing to worry about the details. Worst case scenario, the GUI API would do some format conversion internally to get it on screen.

This also does not meet the goal of getting beginners to easily get something on the screen. Not unless you want to start them by implementing their own line-drawing algorithms and whatever.

I feel very strongly that getting images on screen is way out of scope for a first proposal for 2D graphics. Making a proposal that involves stuff like input and a windowing system abstraction seems like there is just an error in naming if it is called "2D Graphics."

There are already numerous highly capable graphics libraries out there that could be used (whether standardized or not!), and are quite frankly a thousand times more attractive than having this kind of garbage in the standard.

It seems like the point of intersection of "numerous" libraries is exactly the sort of thing that should be in the standard!

I attribute this to lack of experience in that particular field by the people involved: they simply don't have the knowledge necessary to come up with an acceptable 2D API. Given this clear lack of involvement by professionals in the field of 2D graphics, it would be a much better choice to just leave it alone.

I'd argue that 2D graphics isn't really a field. There are a great many fields that use 2D graphics. I can claim professional experience in GUI application development and doing pipeline development for Visual Effects in film and television. (Hence some of the libraries I used as examples are probably best known in VFX, such as OpenColorIO.)

If we need a library for helping students get up to speed, provide it as a separate tool, and don't make it part of the standard.

This, I agree with. What I am suggesting would be useful for students, but also in various real applications.

What the C++ community needs is a way to install that library. What we need is package management:

Uhhh... Neat. But orthogonal to what I am talking about. You can reasonable expected that c++ libraries will use standard constructs like std::string and std::vector. If there was some vector class available in the package repos, some people would certainly use it, but not necessarily many. Being in the standard means it is the thing that everybody else can build on when they make their libraries that would live in your suggest package manager. So, I could build a blur algorithm that lives as a small package in the repository which operates on std::image, and you could build a package that displays the std::image I blurred. Absent a type in the standard for us to use when building our small packages, I need to do a whole lot of extra work to publish my blur package rather than being able to make something with the narrow scope of "blur a std::image."