Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Negative

This thread is locked; no one can reply to it. rss feed Print
Negative
trcurvello
Member #7,784
September 2006

How can I make to take off the negative of a photo???

imaxcs
Member #4,036
November 2003

trcurvello
Member #7,784
September 2006

using allegro

Andrei Ellman
Member #3,434
April 2003

Find the inverted value of each pixel. Assuming your picture is an image that has 8-bits for each of R,G and B channels, subtract the channel's value from 255 for the 3 channels in the pixel, and do this for all pixels in the image.

AE.

--
Don't let the illegitimates turn you into carbon.

trcurvello
Member #7,784
September 2006

How I do this in the code???

Matt Smith
Member #783
November 2000

for (y=0 ; y<bmp->h ; y++)
   for (x=0 ; x<bmp->w ; x++) {
      c = _getpixel(bmp, x, y);
      r = getr(c); g = getg(c); b = getb(c);
      
      _putpixel(bmp, x, y, makecol(255-r, 255-g, 255-b) );
   }

untested, all vars are ints (except bmp of course)

James Stanley
Member #7,275
May 2006
avatar

That's not actually a negative though, that's just inverted. A negative is something that might be called solarized. I believe they're different. I could be wrong.

Jakub Wasilewski
Member #3,653
June 2003
avatar

Quote:

A negative is something that might be called solarized. I believe they're different. I could be wrong.

Hm, maybe find out then ;).

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

Arthur Kalliokoski
Second in Command
February 2005
avatar

"Solarization" refers to extreme overexposure of photographic film, I think it occurred to old TV cameras too. You could see normal parts of the picture the way it was supposed to be, but something extremely bright made some spots dark again. If you see old TV clips from game shows or something from the '50's or '60's, you might notice "flares" with weird effects coming from jewelry reflections of the stage lights etc.

They all watch too much MSNBC... they get ideas.

Jonatan Hedborg
Member #4,886
July 2004
avatar

In pseudo code, you could probably do something like this:
For each pixel
{
Get Rgb value for pixel
Covert to HSV
Add ("rotate") 180 to Hue (ie, 200 + 180 = 20)
Convert back to Rgb
Write to target image
}

Untested, but i guess that's how it works.

James Stanley
Member #7,275
May 2006
avatar

Quote:

Hm, maybe find out [en.wikipedia.org] then ;).

Fair enough :).

Or, another pseudo-code:
For each pixel
{
Get RGB values for pixel (with getr, getg, getb)
Take each from 255 (r=255-getr)
write to target image
}

Andrei Ellman
Member #3,434
April 2003

James Stanley said:

That's not actually a negative though, that's just inverted.

A negative involves inverting all the R, G, and B values, not just the lightness. What you had in mind would result in the correct lightness, but the colours would still be inverted. The algorithm in question for what you had in mind would look something like this.

For each pixel
{
Get Rgb value for pixel
Covert to HSL
H = 1.0f - H
Convert back to Rgb
Write to target image
}

If this algorithm was applied to the output (or input) of Jonatan's pseudocode, you'd basically end up with the same effect as Matt Smith's code which is what the negative of an image is.

Arthur Kalliokoski said:

"Solarization" refers to extreme overexposure of photographic film, I think it occurred to old TV cameras too.

Not to mention with my phone's digital camera add-on whenever the sun happens to be in one of my photos.

James Stanley said:

For each pixel
{
Get RGB values for pixel (with getr, getg, getb)
Take each from 255 (r=255-getr)
write to target image
}

Which is basically the same code that Matt Smith wrote earlier.

AE.

--
Don't let the illegitimates turn you into carbon.

Mr. Big
Member #6,196
September 2005

The correct formulae are:

Rn = 255 - R
Gn = 255 - G
Bn = 255 - B

Red becomes cyan, green becomes magenta, blue becomes yellow, dark becomes bright and vice versa:

(255, 0, 0) <-> (255 - 255, 255 - 0, 255 - 0) <-> (0, 255, 255)
(0, 255, 0) <-> (255 - 0, 255 - 255, 255 - 0) <-> (255, 0, 255)
(0, 0, 255) <-> (255 - 0, 255 - 0, 255 - 255) <-> (255, 255, 0)
(10, 10, 10) <-> (255 - 10, 255 - 10, 255 - 10) <-> (245, 245, 245)

Fladimir da Gorf
Member #1,565
October 2001
avatar

Just do: color = ~color. Much easier...

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Jakub Wasilewski
Member #3,653
June 2003
avatar

Quote:

Just do: color = ~color. Much easier...

Yeah, especially fun when alpha 255 becomes alpha 0 and you stop seeing anything when blitting with alpha :)).

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

Andrei Ellman
Member #3,434
April 2003

Quote:

Rn = 255 - R
Gn = 255 - G
Bn = 255 - B

Is it just me or does this thread have an exceptionally high echo???

AE.

--
Don't let the illegitimates turn you into carbon.

Mr. Big
Member #6,196
September 2005

\o/!
I've just noticed that my solution has already been proposed 3 times in this thread.
The reason is, I didn't really bother reading all the posts.
I saw somebody mentioning 'HSV' and 'HSL' and thought, "what the...?", so I've decided to post the correct solution. ;D

Go to: