This content is not currently approved and is visible here for review only.

Library tutorials & articles

Image Manipulation - Invert

Page 1 of 2
  1. Introduction
  2. The Algorithm

Introduction

Image inversion is by far the easiest Image Manipulation there is.  Outside the context of computers, an inverted image is referred to as a "negative".  That is, every color is the exact opposite of the original color.

This same effect can be mirrored in the digital world.  The human eye has three distinct color receptors, one for red, one for green, and one for blue.  This is how color is represented in the .NET environment ( Red Green Blue).  The current range for each channel (it may be increased in the future) is 0 to 255 -- one Byte.  The general, mathematical way to invert a number (where 0 is the minimum) is:
Inverted_Value = Maximum_Value - Actual_Value
If you apply this formula to each color channel (RGB) of each pixel (a Point of Color in an Image), you will have an inverted image!

To help visualize the transformation, take a look at the before and after pictures of our planet Earth:

Before:

 

After:

 

    To restate this in a more conceptual form, here is a picture with five primary colors on the top row, and their inversions on the bottom row:

 

    You can see the basic relations among colors.  Black goes to White, Red to Cyan, Green to Magenta, Blue to Yellow, and Grey to Grey.


Earth Image shot by NASA, provided by FreeImages.co.uk
Inversion performed by my image program, Colithium Graphics

Comments

  1. 11 Sep 2007 at 04:44

    I have just returned from an extended absence from this forum so I realize this is rather late.

    Thanks for your suggestion. I did research on the ColorMatrix way of doing things at the time of writing this article. What I found was that the UnSafe way of doing things was slightly faster than using the ColorMatrix. Granted the code is way more complicated but my goal was to make the routine as efficient as possible.

    The other aspect was that this article is part of a series of articles (which I haven't gotten around to finishing yet). I was eventually going to post an article about Flood Fill. The code above is much like the code I wrote for the FloodFill routine.  Since FloodFill doesn't lend itself to using a ColorMatrix, it would have to be done this way (as far as I know).

    But I'll definitely mention it in the article, I'm not sure why I didn't do that in the first place =)







  2. 22 Nov 2006 at 00:31

    Nice code but as you express memory issues can cause headaches, a much simpler, safer and managed way would be to create a new ColorMatrix from a set array of the correct elements and implement it by applying the ImageAttributes to the graphics object. Wether using VB.NET or C# this method is greatly effective without the need of any progress reports for either.

     

    Grant Thomas.

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Image Manipulation - Invert.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner

    One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!