How do I set my palette to grayscale in c++ - visual-studio-2010

I'm reading live feed from a mono camera and need to take a snapshot when pressing a button.
So when I convert the pointer the camera passes me to a bitmap (8 bpp) for further image processing,
the colors get all weird like this
I'm guessing it's because I didn't set the palette of the bitmap correctly, so I googled some and
came across some code in VB, which sets the color palette of a bitmap to gray scale.
I want to do the same in C++ under Visual Studio 2010, only in C++ the constructor of color palette is sealed,thus I have no way of declaring a new grayscale palette and then assign it to my bitmap.
Below is the example code I found written in Visual Basics :
Dim bmpobj As Bitmap
Dim pal As System.Drawing.Imaging.ColorPalette
pal = bmpobj.Palette
For i = 0 To 255
pal.Entries(i) = Color.FromArgb(i, i, i)
Next
bmpobj.Palette = pal
So what I'm asking is this : Is there anyway to set my bitmap's palette to grayscale ?

I think, just by changing the color palette, your image won't change to gray scale.
You will need to change each color pixel in the image to gray scale. The algorithms are here.

Related

Overlay text image on scene image in PIL

I have a text image and I want to overlay it over a scene image, both having the same size and in RGB mode. I have tried the ImageChops OR operation but it seems to only work on binary (mode '1'). ImageChops.add_modulo and blend functions also did not help at all. What I need is to replace the pixel values in the scene image with those from the text image. The text image has 0 (black background) and the text has the value of 255. So, the overlay should be crystal clear, although scene images are in color.
I would appreciate any help on this issue.
I managed to do it as follows:
scene_image.paste(text_img, box=None, mask=text_img.convert('1'))

How to add an image with transparency to a TDbgrid column in delphi?

I am trying to add an image to a column on a TDbgrid that takes transparency into account. When drawing the image from a TImageList on the canvas in the DBGridDrawColumnCell procedure, I need the background of the image (the same color as the pixel in the lower left corner) to take on transparency. I want this transparency area to show the highlight color or non-highlight color, especially when themes are used, such as Aero. I have been able to accomplish this in older versions of Windows with color values of clHighlight or clWindow as the background color. But with Aero themes, it always paints a box behind the non-transparent part of the image instead of the gradient blue highlight color that Aero uses. How can I accomplish this?
I believe I am supposed to use alpha channel but I'm not sure how to do this from a TImageList to a canvas. I believe the cell is painted completely with the actual highlight color before I start drawing on the canvas in the cell. I just want to draw the non-transparency part of the image and leave the background.
I was able to finally determine how to display images on a dbgrid with transparency even if themes, such as Aero is used.
I used a regular TImageList and loaded the images that I needed to display on the dbgrid. In my case there were two and they were in icon (ico) format. Instead of transferring the image to a bitmap and then drawing it to the dbgrid canvas as most old code recommends, I simply used the following simple code in the DBGridDrawColumnCell procedure:
if DataCol=0 then
begin
if (MApptsConflict.Value='<none>') then
ImageIndex := 0
else
ImageIndex := 1;
ImageList.Draw(TDBGrid(Sender).Canvas,Rect.Left+2,Rect.Top+2,ImageIndex,True);
end;
This will draw directly to the dbgrid canvas from the TImageList which will give the desired transparency.
UPDATE: I tried it with bmp's loaded in the Timagelist and it worked too.

Applying tint() on a BufferedImage in processing

I'm grabbing visual screen contents with the Robot Class which I'm then trying to set to half transparency. I'm following the reference for the tint command like so:
screenshot = robot.createScreenCapture(new Rectangle(0,42,scrdim.width,36));
shot = new PImage(screenshot);
shot.resize(32,1);
tint(255,127);
image(shot,5,5,64-5,4);
But as soon as I use the tint command the image disappears completely, regardless of the actual aplha parameter value. Is it, because it because I'm dealing with a BufferedImage? I've been trying to figure out how to set the alpha of that directly, but with no luck.
Your transparenvcy currently is 100%.
The syntax for tint is:
tint(color, transparency);
Simply invert your tint color for 50% transparency:
tint(255, 127);

SetMenuItemBitmaps BMP transparent colour

How do I set the transparent colour of a BMP image for SetMenuItemBitmaps, I'm currently using LoadImage with uType as IMAGE_BITMAP and fuLoad as LR_LOADFROMFILE + LR_LOADTRANSPARENT?
LoadImage documents the LR_LOADTRANSPARENT flag as:
Retrieves the color value of the first pixel in the image and replaces the corresponding entry in the color table with the default window color (COLOR_WINDOW). All pixels in the image that use that entry become the default window color.
Choose a color for the first pixel that isn't used anywhere else, and paint all portions of the bitmap that are supposed to be transparent using that color.
Keep in mind that this flag is not meaningful for bitmaps with a color depth above 8bpp.

How to draw ARGB bitmap using GDI+?

I have valid HBITMAP handle of ARGB type. How to draw it using GDI+?
I've tried method:
graphics.DrawImage(Bitmap::FromHBITMAP(m_hBitmap, NULL), 0, 0);
But it doesn't use alpha channel.
I've got working sample:
Get info using bitmap handle: image size, bits
BITMAP bmpInfo;
::GetObject(m_hBitmap, sizeof(BITMAP), &bmpInfo);
int cxBitmap = bmpInfo.bmWidth;
int cyBitmap = bmpInfo.bmHeight;
void* bits = bmpInfo.bmBits;
Create & draw new GDI+ bitmap using bits with pixel format PixelFormat32bppARGB
Gdiplus::Graphics graphics(dcMemory);
Gdiplus::Bitmap bitmap(cxBitmap, cyBitmap, cxBitmap*4, PixelFormat32bppARGB, (BYTE*)bits);
graphics.DrawImage(&bitmap, 0, 0);
I had similar issues getting my transparent channel to work. In my case, I knew what the background color should be used for the transparent area (it was solid). I used the Bitmap.GetHBITMAP(..) method and passed in the background color to be used for the transparent area. This was a much easier solution that other attempts I was trying using LockBits and re-creating the Bitmap with PixelFormat32bppARGB, as well as cloning. In my case, the Bitmap was ignoring the alpha channel since it was created from Bitmap.FromStream.
I also had some very strange problems with the background area of my image being changed slightly. For example, instead of pure white, it was off white like 0xfff7f7. This was the case whether I was using JPG (with blended colors) or with PNG and transparent colors.
See my question and solution at
GDI+ DrawImage of a JPG with white background is not white
Ah... but .Net doesn't use HBITMAP and GDI+ is a C++ library atop the basic Windows GDI, so I'm assuming you're using non-.Net C++.
GDI+ has a Bitmap class, which has a FromHBITMAP() method.
Once you have the GDI+ Bitmap instance, you can use it with the GDI+ library.
Of course, if you can write your program in C# using .Net it will be a lot easier.

Resources