DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Convert Image Files Into GIF or WebP Format Using Java
  • How to Convert a PDF to Text (TXT) Using Java
  • What SREs Can Learn From Capt. Sully: When To Follow Playbooks
  • Migrating from Sakila-MySQL to Couchbase - Part 3: Stored Procedures

Trending

  • Replay, Scheduling, and Throttling: Engineering a Self-Service Kafka Streaming Tool at Scale
  • Beyond @Transactional: Solving the Dual-Write Problem in Distributed Microservices
  • Multi-Agent Orchestration on AWS With AgentCore Runtime and A2A
  • Agentic Systems and Design Patterns

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Converting an Image to Negative using SDL2

Converting an Image to Negative using SDL2

SDL2 can be great for altering images. Here's how to convert a picture to a negative.

By 
Daniel D'agostino
·
Nov. 18, 15 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
3.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

in “converting an image to grayscale using sdl2,” we manipulated the pixels of an existing image in order to convert it to grayscale. it is now very easy to add all sorts of effects by changing pixel values in different ways.

another effect we can apply is the negative of an image. this means that the dark areas become light, and the light areas become dark. for instance, if we have this image of a canadian goose:

sdl2-negative-goose-positive

by using any image editor, say irfanview , we can get the following negative:

sdl2-negative-goose-negative

this is a very easy effect to apply. since each of the red, green, and blue channels is represented by a byte, its value is in the range between 0 and 255. so we can compute the negative by subtracting each value from 255. that way a bright value (e.g. 210) will become dark (e.g. 45), and vice versa.

here’s the code for the negative image effect:

            case sdlk_n:
                for (int y = 0; y < image->h; y++)
                {
                    for (int x = 0; x < image->w; x++)
                    {
                        uint32 pixel = pixels[y * image->w + x];

                        uint8 r = pixel >> 16 & 0xff;
                        uint8 g = pixel >> 8 & 0xff;
                        uint8 b = pixel & 0xff;

                        r = 255 - r;
                        g = 255 - g;
                        b = 255 - b;

                        pixel = (0xff << 24) | (r << 16) | (g << 8) | b;
                        pixels[y * image->w + x] = pixel;
                    }
                }
                break;
            }
            break;

the code for negative is very similar to grayscale in that we’re looping over each pixel, calculating new values for red, green, and blue, and then applying the new value to the pixel.

let’s try this out with a photo i took of eldon house in london, canada last september. here’s the photo in its normal state:

sdl2-negative-eldon-positive

when i press the n key to apply the negative effect, here’s the result:

sdl2-negative-eldon-negative

i can also press n again to apply negative on the negative, and end up with the original image again:

sdl2-negative-eldon-positive

that’s an important difference between grayscale and negative. grayscale is an operation that loses colour information, and you can’t go back. negative, on the other hand, is symmetric, and you can go back to the original image simply by applying the same operation again.

the source code for this article is available at the gigi labs bitbucket repository . it’s the same as in the grayscale article, plus the code for the negative effect in this article.

Light (web browser) Convert (command) IT Sort (Unix) Repository (version control) House (operating system) IrfanView Plus (programming language)

Published at DZone with permission of Daniel D'agostino. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Convert Image Files Into GIF or WebP Format Using Java
  • How to Convert a PDF to Text (TXT) Using Java
  • What SREs Can Learn From Capt. Sully: When To Follow Playbooks
  • Migrating from Sakila-MySQL to Couchbase - Part 3: Stored Procedures

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook