Rank: Member
Groups: Member
Joined: 3/22/2004(UTC) Posts: 18
|
I cannot find info in help file of sample code on this topic:
How can I set up a user-defined palette in VB6? The background is this: In our 8bpp greyscale images, higher pixel intensities code for some property (let's say, temperature). We need to display these images with a lookup-table (LUT) that actually represents the temperature like 'glowing' steel: 'cold' pixels are black, then for increasing temperature, pixels get red, orange, yellow, greenish, blueish, and white is hottest.
e.g., palette(0)=RGB(0,0,0) .......... palette(20)=RGB(100,0,0) palete(21)=RGB(110,0,0) .......... palette(255)=RGB(255,255,255)
It is however unclear how I define a custom palette.
Any help appreciated. Kees Jalink
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration, Moderator Joined: 7/28/2003(UTC) Posts: 1,660
Thanks: 5 times Was thanked: 76 time(s) in 74 post(s)
|
Hello Kees, Do you need to colorize following image: to If yes, then it can be done using following code: Code:BitmapViewer1.Bitmap.Data.ConvertTo8bppIndexed DitheringTypeNone, , , , , PaletteTypeGrayscale
BitmapViewer1.AutoUpdate = False
With BitmapViewer1.Bitmap
For I = 0 To 255
.Data.Palette.Entries(I) = .Color.CreateArgbFromHsl(I / 255 * 240, 255, 128)
Next
End With
BitmapViewer1.AutoUpdate = True
>>It is however unclear how I define a custom palette. You can create Palette object and get access to it via Entries property. You can also create palette from array using ImportFromArray method. ============================= Edited by Fedor at August 17, 2004 =============================Edited by user Monday, December 21, 2009 3:24:36 AM(UTC)
| Reason: Not specified Fedor attached the following image(s): |
Best regards, Fedor Skvortsov
|
|
|
|
Rank: Member
Groups: Member
Joined: 3/22/2004(UTC) Posts: 18
|
It is that last possibility that you mention: importfromArray.
If I want to do that I need to know how the data in the array must be presented. Can I put RGB values in the palette?
Can you provide me with a simple example in VB6: make a palette (256 values) that contains Green for '0' value RGB (0,255,0);
grey for intermediate values: palette(i)=RGB(i,i,i)
Red for '255' ; RGB(255,0,0)
thanks.
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
Hello Kees, Array entries representation for the ImportFromArray method depends on ColorTreatment property of the bitmap. To use standard VB6 RGB color representation, specify it to ColorTreatmentColorref. However if you already have indexed bitmap, and palette indices are sorted by brightness (as far as I understood from the original post), you need not have to use ImportFromArray method. Palette property is already initialized and you can modify its values here. Here is a code sample: Code:Dim objBitmap As New Bitmap
' For brevity let's assume that image.tif is a 8-bit palette image.
objBitmap.LoadFromFile "c:\image.tif"
objBitmap.ColorTreatment = ColorTreatmentColorref
objBitmap.Data.Palette(0) = vbGreen
Dim I As Integer
For I = 1 To objBitmap.Data.Palette.Count - 2
objBitmap.Data.Palette(i) = RGB(i,i,i)
Next
objBitmap.Data.Palette(objBitmap.Data.Palette.Count - 1) = vbRed
Hope this helps. Edited by user Monday, December 24, 2007 5:03:27 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Member
Groups: Member
Joined: 3/22/2004(UTC) Posts: 18
|
Thanks. I tried it and a problem pops up. Since I need to apply this palette many times to many images in different viewers, I tried to replace your code with: Code:BitmapViewer1.Bitmap.ColorTreatment = ColorTreatmentColorref
BitmapViewer1.Bitmap.Data.Palette(0) = vbGreen
Dim I As Integer
For I = 1 To 254
BitmapViewer1.Bitmap.Data.Palette(I) = RGB(I, I, 0)
Next
BitmapViewer1.Bitmap.Data.Palette(255) = vbRed
It works for some images but it produces an error (out of range) when the actual picture that is loaded only has (e.g.)209 grey values: the assignment when I=210 produces the error. Does this mean that you can only assign as many entries as there are grey levels in the actual image? If so, this would mean that I have to regenerate the palette for every new image and for each bitmapviewer that I use because bitmapviewers each display different pictures). Is that true? This is goin to be too slow. The idea behind a Color Lookup Table as the one I suggested is to warn the user when his data are 'underflow' (green) or 'overflow' (Red). This means that the data from the A/D convertor that puts them in the picture cannot be trusted (because smaller than 0 or larger than 255 is not possible with the A/D convertor). Any faster way to set '0' pixels to Green and '255' pixels to Red? Thanks, Kees Edited by user Monday, December 24, 2007 5:03:49 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: Member, Administration Joined: 8/2/2003(UTC) Posts: 876
Thanks: 2 times Was thanked: 27 time(s) in 27 post(s)
|
Well... Could you tell more detailed how do you convert 8-bit grayscale image to indexed bitmap? Anyway, more correct would be Code:Dim I As Integer
For I = 1 To BitmapViewer1.Bitmap.Data.Palette.Count - 2
BitmapViewer1.Bitmap.Data.Palette(I) = RGB(I, I, 0)
Next
BitmapViewer1.Bitmap.Data.Palette(BitmapViewer1.Bitmap.Data.Palette.Count - 1) = vbRed
This way you ensure that you are always inside range. By the way, as soon as you process bitmap in BitmapViewer, I would recommend you to disable AutoUpdate property while you are updating the palette (otherwise it will be redrawing each time you change a palette entry): Code:Dim I As Integer
BitmapViewer1.AutoUpdate = False
For I = 1 To BitmapViewer1.Bitmap.Data.Palette.Count - 2
BitmapViewer1.Bitmap.Data.Palette(I) = RGB(I, I, 0)
Next
BitmapViewer1.Bitmap.Data.Palette(BitmapViewer1.Bitmap.Data.Palette.Count - 1) = vbRed
BitmapViewer1.AutoUpdate = True
BitmapViewer1.Refresh
Edited by user Monday, December 24, 2007 5:04:05 PM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Member
Groups: Member
Joined: 3/22/2004(UTC) Posts: 18
|
Andrew, Fedor,
problem solved. thanks very much for the fast help all the time.
Sincerely, Kees Jalink :D
|
|
|
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.