[Tutorial] How to negative a bitmap in C# with low-level API

Example requests & Code samples for GdPicture Toolkits.
Post Reply
User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

[Tutorial] How to negative a bitmap in C# with low-level API

Post by Loïc » Fri May 06, 2011 4:28 pm

Hi,

Here a C# sample that demonstrates how to use the low-level API of GdPicture to access bits of a Bitmap.

The sample prompts the user to select a bitmap, apply a negative effect, and save the processed bitmap into c:\negative.tif

Supported bitdepth are 1bpp, 4bpp (grayscale), 8bpp (grayscale), 16bpp, 24bpp & 32bpp.


Feel free to ask any technical question.

Code: Select all

      unsafe private void negativeImage(){
         GdPictureImaging oGdPictureImaging = new GdPictureImaging();
         oGdPictureImaging.SetLicenseNumber("XXX");

         int ImageID = oGdPictureImaging.CreateGdPictureImageFromFile(""); //Prompt the user to select a file, and try to open it as a memory Bitmap
         if (ImageID != 0)
         {
            int bpp = oGdPictureImaging.GetBitDepth(ImageID); //Number of bit per pixel.
            byte* pData = (byte*)oGdPictureImaging.GetBits(ImageID);
            int width = oGdPictureImaging.GetWidth(ImageID); 
            int height = oGdPictureImaging.GetHeight(ImageID);
            int stride = oGdPictureImaging.GetStride(ImageID);
            
            switch (bpp){
               case 1:
               case 4: //We should check the image uses grayscale palette entry for this configuration, else the color palette should be modified
               case 8: //same remark as prev.
               case 16:
               case 24:
                  {
                     for (int y = 0; y < height; y++)
                     {
                        byte* pScanline = pData;
                        for (int x = 0; x < stride; x++)
                        {
                           pScanline[x] = (byte)(0xFF - pScanline[x]);
                        }
                        pData += stride;//increment pointer to next scanline
                     }
                      break;
                  }
               case 32:
                  {
                     for (int y = 0; y < height; y++)
                     {
                        for (int x = 0; x < width; x++)
                        {
                           //negative Blue component
                           *pData = (byte)(0xFF - *pData);
                           pData++;
                           //negative Green component
                           *pData = (byte)(0xFF - *pData);
                           pData++;
                           //negative Red component
                           *pData = (byte)(0xFF - *pData);
                           pData++;
                           //squizz alpha/reserved component
                           pData++;
                        }
                     }
                    
                     break;
                  }
               default:
                  throw new Exception("Pixel format not supported"); 
            }
            oGdPictureImaging.SaveAsTIFF(ImageID, "c:\\negative.tif", TiffCompression.TiffCompressionAUTO);
            oGdPictureImaging.ReleaseGdPictureImage(ImageID);
 
         }
      }
   }

csharptuts
Posts: 1
Joined: Thu Aug 05, 2021 10:54 am
Location: India
Contact:

Re: [Tutorial] How to negative a bitmap in C# with low-level API

Post by csharptuts » Thu Aug 05, 2021 11:00 am

The C language offers several low-level APIs to manipulate bits. These APIs include read and write macros and arithmetic operations.
One of the most widely known low-level APIs is bitwise operations. The bitwise operators in C are worth a closer look when you need to work with binary data structures such as bitmaps or arrays of bits.

Bitwise operators in C allow you to perform mathematical and logical operations on bits, which are represented by set bits with values of 0 or 1. Before you can use these operators, we'll first need to understand the representation of a bitmap in memory and how it's stored in low-level structures.

Most of the low-level APIs will use bitmaps to represent their data. Bitmaps are a series of consecutive 0s and 1s that are encoded with a color depth and depth order.

Bitmaps cannot be negative, so how does the low-level API deal with negatives?

The most common solution is to use a bitmap mask for negative values. The image below shows this in action when it masks out all the red pixels for black:
To create this effect, the bitmap is first inverted and then used as a mask for black on white pixels. This means that if we draw all white pixels on black, it will have no effect on the original image.

Have you learn C# Tutorial?

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest