Bitmap Algorithm C language, Bitmap Algorithm

Source: Internet
Author: User

Bitmap Algorithm C language, Bitmap Algorithm

#include <stdio.h>  void set_bit(void *base, unsigned long n)  {      unsigned long *m = (unsigned long*) base;    unsigned long size = sizeof(unsigned long) * 8;    m[ n / size ] |= 1UL << (n % size);}  void clear_bit(void *base, unsigned long n)  {      unsigned long *m = (unsigned long*) base;    unsigned long size = sizeof(unsigned long) * 8;    m[ n / size ] &= ~(1UL << (n % size));}  int main()  {      unsigned long value[2] = {0};    set_bit(value, 0);      set_bit(value, 63);      printf("Set:\n");    printf("0x%016lx, 0x%016lx\n", value[0], value[1]);      clear_bit(value, 0);      clear_bit(value, 63);      printf("Clear:\n");    printf("0x%016lx, 0x%016lx\n", value[0], value[1]);      return 0;  }  


Bitmap operations in C Language

For a 24-bit color chart, you can simply store the int (32-bit machine, 32-bit length) two-dimensional array. The 24-digit number is certainly saved, and there is no need to separate the colors. If you store them separately, use the following struct array method. It is recommended for reference and is very convenient and practical.

RGB24 uses 24 bits to represent a pixel, and RGB is represented by 8 bits. The value range is 0-255. Note that the order of RGB components in the memory is BGR .... Generally, you can use the RGBTRIPLE data structure to operate a pixel, which is defined:
Typedef struct tagRGBTRIPLE
{BYTE rgbtBlue; // blue weight
BYTE rgbtGreen; // green component
BYTE rgbtRed; // red weight
} RGBTRIPLE;

Bitmap Algorithm

The color space is a three-dimensional space (R, G, B ). The gray space is a one-dimensional space (a straight line ).

The conversion method from a color image to a grayscale image is actually to define a ing from the (R, G, B) space to the grayscale space. Generally, its reflection is a little bit to multiple points, and there is no uniqueness.

In order to ensure the uniqueness of the reflection, a restriction must be imposed on the value range of each pixel in the color image space.
For example, a monochrome graph. If it is only a Blue single-tone diagram (R = 0, G = 0 ),
The blue value (B) is simply converted to the GRAY value. The inverse transformation is simply to convert the gray value to the blue value (B), and the R and G are zero.

If there are other complex constraints, I think the simplest thing is to put the color image sample image (the left image in your example) in each pixel (R, G, B) and the GRAY value of the corresponding vertex after it is transformed to a GRAY image (the right image in your example) to form a conversion table. Some GRAY images do not contain the GRAY value, it may need to be filled by interpolation. If it is one-to-one, the inverse transformation can also be computed by querying this table.

I will give you a specific transformation method, as added by the landlord.

Grayscale 0 --- given color (R0, G0, B0)
Gray Scale 255 -- white (255,255,255)

Then any gray value M

Change to (R0 + (255-R0)/255 * M, G0 + (255-G0)/255 * M, B0 + (255-B0)/255 * M.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.