Playing with ColorMatrix

Environment: VC++ 7.0, XP

Introduction

GDI+, the new Windows graphics API, comes with a ColorMatrix. It's a pity that ColorMatrix is just a simple struct, without any member functions. And it's also a pity that Microsoft's documentation is very, very sketchy. It's a pity, because you can do some really nice things with color matrices. Some of these things are near to impossible with other means. That's why I wrote an extension class, QColorMatrix, to open up the possibilities of color matrices in GDI+.

I also wrote a mini-application, Tinter, to demonstrate some of the things you can do. See the pictures at the end of this article for the effects you can apply to bitmap pictures. Tinter changes the coloring of JPG, GIF, BMP, and TIFF files interactively, can convert the types into one another, and prints. Pretty impressive for an EXE of just over 70 Kb, I think.

What Is a Color Matrix?

Just as the co-ordinates x, y, and z define a single point in 3D space, the three color components R (red), G (green), and B (blue) define a single point in color space. In 3D geometrics (and in 2D geometrics as well), matrices can be used to transform the position of a point, and, more importantly, the position of a group of points. Change the matrix, apply the matrix to a group of points, and all the points are moved together. If you apply the matrix to all the points in a specific scene, the whole scene is transformed.

A color matrix works the same way in color space. With a color matrix, you can change one specific color, say dark blue into light red, but you can also change all colors together, in a co-ordinated way. Just like a scene in 3D space, you can scale, translate, rotate, and shear the color space, or apply any combination of these basic transformations to it.

For technical reasons, points in 3D space are transformed with 4x4 matrices. A color in RGB color space can also be transformed with a 4x4 matrix. But as GDI+ works with a fourth 'color', A (alpha, or opacity) throughout, ColorMatrix is defined as a 5x5 matrix of REALs:

typedef struct { REAL m[5][5]; } ColorMatrix;

QColorMatrix

Microsoft leaves it to us to fill in the 25 REALs of ColorMatrix, which is no easy task for even a simple rotation. My class, QColorMatrix, adds some useful member functions to put meaningful values in the matrix. You can scale, translate, rotate, and shear the color space in one step. There are also two member functions for more complex, but very useful operations: one to set the saturation of the color space, and one to rotate the hue, while preserving the luminance. Better than trying to describe what this all means, I'll let you have a look at the pictures (yes, that's me!).

Tinter

Tinter is a mini-application, demonstrating what you can do with QColorMatrix. It is even a small tool in its own right, for correcting bitmap pictures—or disforming, if you like. You can change the contrast, brightness, saturation, and hue. You can even change the gamma, although this has nothing to do with color matrices. I just threw it in for free.

Tinter is built in MFC 7.0. Of course, your system must support GDI+, which currently only XP does. However, Windows 98 and later versions can be upgraded.

More Information

There is not much information on color matrices on the Web. I leant heavily on a rather dense, 1993 article by Paul Haeberli, published by Silicon Graphics. Also, the documentation of SGI's Color Matrix Extension for OpenGL may come in useful.

Contrast
Brightness
Saturation
Hue
Gamma

Download

Download demo project and source - 63 Kb



Comments

  • karen millen outlet

    Posted by karen millen sale on 06/07/2013 06:10am

    adems, tiene capacidad de contener agua lquida y, por lo tanto, de ser habitable, dicen los astrnomos. louis vuitton 財布 "He is a good team player and it makes it hard for the opposition to keep him under control. karen millen outlet online "I think up to now people have known me as a violinist and then more so as a singer," Shaw said in a phone interview from New York where she lives. SaintMarin. www.lvlviloveu.com Somehow, even the display of dressage at the Royal Andaluc School of Equestrian Art was a bit pofaced.louboutin heels All content submitted is true, accurate and not misleading. karen millen outlet online If you can schedule your trip for deep autumn, you will benefit from sharply reduced airfares and hotel accommodationsas witness the following: (1) China: $1,307 to $1,718 for nine nights in five Chinese cities, including roundtrip airfare to China from San Francisco. Dear customers no need to think at all, as time is money so if you will think more that's mean you are wasting your money. karen millen black Restarted and I ran into a BSOD. cheap ghds Dans la foule, hors mis les annuellement 01 janvier, 11 fvrier, 08 mars, Pques, 20 mai, l'lection de papa Jean Claude Nguekeng la tte du conseil des sages Douala, l'organisation des lections du Chef de la Communaut Fongotongo de Douala, la tourne spciale du roi des FongoTongo travers le pays, et bien d'autres. ghd straightener What if can be certainly torrential downpour your day? onlinekarenmillen.webeden.co.uk Does not violate any local, state or federal laws, regulations, statutes or ordinances.louis vuitton 財布 Russell Crowe is just irate, because Souths made the foolish decision to resign John Sutton, who has been hopeless for the last few years, paving the way for their one shining light this season to leave. nicoletta stefania sur PAM75: 8 annes d'existence en crmonie. ugg boots Are they taking constructive roles in the process of governing? karen millen outlet uk It's definitely not CSPAN's problem. louis vuitton site officiel ??ity considers buying water "They're not interested in a great deal of water. www.lvlviloveu.com Mais il est exact de faire remarquer que les libraux, qui n'ont absolument aucun esprit critique d'aucune espce de sorte quand il s'agit de leur parti, sont toujours trs rapides sur la dtente quand il s'agit de s'attaquer au PQ.

    Reply
  • Matrices

    Posted by didula on 10/18/2007 12:44pm

    Hi, great interesting article. I have a few questions: Could you tell what matrices you are using for the hue rotation? Is it possible to multiply all of them to get one master matrix which can change the hue? It would be great to see more of the math behind this. I didnt find much about it at the links.

    Reply
  • Rotating Hues with this code

    Posted by jimmygyuma on 05/21/2007 08:37pm

    Using this code or any of its progeny, which are legion, to rotate hues in an image will give you bogus results. It seems to be doing what you want it to do, but the results are so inaccurate that it is only useful for effects. I translated this code, as well as a VB version, into C#, but my results were so far off I thought I had done something wrong. But, no. I got out Mr. Priester's demo and ran the same test, with the same results. I had an image consisting of red, green, blue, yellow, magenta, and cyan bands. I rotated it 120 degrees. Red should become green, green should become blue, blue red, and so forth. And they did, sort of. Red became a kind of forest green, green became sky blue, and blue became maroon, and so forth. It amazes me that this code has been propagated far and wide, with everyone singing its praises, when it gives these kind of results. It's not even remotely useful for any halfway serious work.

    • Re: QColorMatrix

      Posted by jimmygyuma on 07/15/2007 05:48pm

      All well and good, but it is still inaccurate. You need to preserve both the luminance and the saturation. The only thing you want to change is the hue. I went back to LockBits, UnlockBits, so that I could convert each pixel to HSL, rotate the Hue, a simple addition, and convert the result back to RGB. The saturation and luminance are never changed. Of course you have to have accurate conversion routines. I have a class JColor, which I have been refining and translating since my Java days, to handle this. I use your saturation and brightness routines, which are fine for subjective work, but, I'm sorry, when I rotate pure yellow 120 degrees and get something other than pure cyan, it doesn't work for me.

      Reply
    • RE: Rotating Hues with this code

      Posted by Sjaakp on 05/24/2007 08:41am

      QColorMatrix rotates hues while preserving the luminance. In other words: omly the color information is modified, not the black-and-white levels. If you would remove the color from an image (by setting the saturation to zero), rotating the hue has no effect.
      
      Because the luminance of the brightest red on the computer screen (RGB 255, 0, 0) is way lower than the luminance of the brightest green (RGB 0, 255, 0), the brightest red does not translate into the brightest green, but into a darker green.
      
      It all has to do with the 'luminance weights' which are assigned to the R, G, and B elements of the color (see the const's in the top part of QColorMatrix.cpp). If all three luminance weights were equal (1.0), the brightest red would translate to the brightest green after rotation. However, rotating the hue would also mean modifying the luminance.
      
      See http://www.graficaobscura.com/matrix/ for some more information on hue rotation.
      
      For what it's worth, the Photoshop hue control works similar to QColormatrix in this respect.

      Reply
    Reply
  • Superb ..............................

    Posted by Legacy on 10/21/2003 12:00am

    Originally posted by: Vishal G

    Fantastic code given....................

    Reply
  • Fantastic !!!

    Posted by Legacy on 09/12/2003 12:00am

    Originally posted by: Oleg Salafonov

    VC6 version really works, but I am still getting
    the error: LNK2001: unresolved external symbol _GdiplusShutdown@8. I have to comment the line
    "Gdiplus::GdiplusShutdown(m_Token);" in the QGdiPlus.
    Can anybody help?

    • I think you got the wrong typdef

      Posted by AviadOffer on 05/18/2004 05:48pm

      typedef unsigned __int64 ULONG_PTR; should be typedef unsigned __int32 ULONG_PTR;

      Reply
    Reply
  • The best article i've read so far ....

    Posted by Legacy on 08/23/2003 12:00am

    Originally posted by: Vergil Cola

    This has to be the best GDI+ article i've read so far.
    Thanks. This has been a great help

    Reply
  • What is the best way to learn GDI+ ???

    Posted by Legacy on 08/20/2003 12:00am

    Originally posted by: SK

    Same as Subject.

    Reply
  • Excellent job - the first GDI+ sample I have seen

    Posted by Legacy on 08/12/2003 12:00am

    Originally posted by: Ajay

    That's excellent and works very well without flikering at all. Also the speed the image changes is also not noticeable. And it also allows saving the changed image!

    Fine!!

    Reply
  • VC6 version, please

    Posted by Legacy on 08/07/2003 12:00am

    Originally posted by: kimchi

    How can I compile the your sample?
    I tried to compile on the VC6 with GDI+ Macro(http://www.codeproject.com/vcpp/gdiplus/vc6gdiplusmacro.asp?target=gdiplus%7Cmacro).
    But I met following error:

    C:\Program Files\Microsoft SDK\include\GdiplusEnums.h(28) : error C2146: syntax error : missing ';' before identifier 'GraphicsState'
    C:\Program Files\Microsoft SDK\include\GdiplusEnums.h(28) : fatal error C1004: unexpected end of file found

    Reply
  • NIce!

    Posted by Legacy on 08/01/2003 12:00am

    Originally posted by: Andreas

    Good Introduction and Demo!

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds