September 21, 2020, 03:15:18 am

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Changing brightness of a background image???

Started by Egil, March 11, 2015, 05:42:05 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Egil

I have been trying  for a while to change the brightness of an image I use for client area background, but so far I have achieved... nothing.
Does anyone know how to do this?

I have a shareware doing this, there they call it "Change Transparency", but I suspect what they do is adjusting the client area brightness.
Support Amateur Radio  -  Have a ham  for dinner!

Brian

Egil,

I use XnView for manipulating / viewing all my pictures. It has the facility to change
the brightness of an image, which is real easy to use

Brian

Egil

Thanks Brian,
I do the same myself, using PhotoFiltre.
I was not clear enough in my question, I want to do it inside my own program. So the contrast between background and what is drawn in the foreground can be adjusted to the personal taste of the user.


Support Amateur Radio  -  Have a ham  for dinner!

Andy

Hi Egil,

Could you try the background image as one image on it's own, and the foreground image as another.

Load the images, background first, then the foreground on top, use showimage to display them.

After that, use the SetControlColor command for the inital colour.

You could add a scrollbar which would send a message to Redraw either the foreground or background
depending on the value / position of the scrollbar?

Could that work?

Have a look at the ColorControls.iwb example - maybe that will give you a pointer?
or of course I could be completely wrong!

Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

LarryMc

Egil,
Can you post me a screen shot of what you are wanting it to look like.
(Your actual program  with the image(s) would be better if that is possible)
If I'm understanding you correctly I've got an idea that might work for you.

Scheme didn't work.
I was trying to use a child window covering the client area and fading it in and ou but it also fades the client area in and out - so that's a no go.

You might have to look into something like the GDI+ library and modify your background during the WM_PAINT cycle.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Brian

Fletchie's control pack has a WinAlpha command, which, according to his blurb:

Blends a window to the given Alpha level, and optionally sets a transparent RGB value for the window

Don't know what kind of window, though...

Brian

Egil

Thanks for trying Larry,

what I would like to do, is load an image into the client area and manipulate it to appear darker than the original, whithout changing the original image.
I will then print characters on top of this, using different colors. If you study the last image I uploaded in this thread: http://www.ionicwind.com/forums/index.php?topic=5609.msg41378#msg41378,  you'll se what I am going to use it for. In the posted example I use a shareware called PlanePLotter. One of the Map Options is called "Transparency". There I can make the image darker or lighter to make the contrast better between the text and the image. PlanePlotter is an excellent piece of work, and I have used it for around 15 years. But I need additional functionality, and do not need most of the the other functions, so I have started doing my own plotter program. This is something I have been working on for short periods at the time over the last couple of years, but haven't really needed it till now.

Just finished converting the CB code to IWB, and attach an archived version of what I have so far.  Still have to make code for reading several instances of the PC-HFDL decoder, and input the paths to the logfiles. I also plan to make the program able to send and receive data via UDP, to be able to easily exchange data with other decoding "freaks". (like me...  :D)

I can use an image editing program to manipulate the images before, loading them into my program, but if I manage to build that functionality into the program, it will be easier in the future to download special images from any map server.

Brian:
Sound like you have found the right way to do it. Why do I never remember that control pack?
I'll look into it right away.

Regards,
Egil


Support Amateur Radio  -  Have a ham  for dinner!

Egil

The WinAlpha command changes transparency level of the whole window. But this must still be the way to go. I just have to find a way to limit the transparency to only include the client area.


Regards.
Egil
Support Amateur Radio  -  Have a ham  for dinner!

Brian

Egil,

Just downloaded your program, and straight away I noticed you could have just used:
GETBITMAPSIZE(handle,mapwidth,mapheight)
instead of the calculations you are doing

Brian

Egil

March 11, 2015, 01:26:30 pm #9 Last Edit: March 11, 2015, 01:30:38 pm by Egil
I use it for the calculation of the scaling factor. Makes life easier when the picture is drawn full size after doubleclicking.
Later on I wil use scalefactors for both height and width, so "regular" resizing of the window can be used. I block this now just to get the logic right.

Regards,
Egil
Support Amateur Radio  -  Have a ham  for dinner!

Brian

Rearrange these words: In. Mouth. Foot.

I realised that after I sent the message - sorry!

Brian

Egil

No need to be sorry. I do it all the time...
But I blaim the dog.

Egil
Support Amateur Radio  -  Have a ham  for dinner!

LarryMc

I'm looking into another idea Egil.  don't give up on me quite yet.

As a last resort you could create a dozen different versions of your image in an external editor and let your user pick which image they wanted to use.  If you make a project out of it then all the images are inside the exe and are never seen by the user as separate image files.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

LarryMc

Another possibility is to use 2d graphics and use your globe image as the background and all your planes as sprites.  Note sure about drawing your lines and such though..
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Egil

Using an external image editor is one solution.
But since what I'm thinking of can be done with such an editor, I reckon it is possible to such do the same functionality using iwb.

I appreciate your efforts Larry, but please don't overdo it. It was never my intention to give anyone extra work.
But then, isn't challenges like this what our great hobby is all about? I just love it!


Egil


Support Amateur Radio  -  Have a ham  for dinner!

GWS

March 12, 2015, 10:24:39 am #15 Last Edit: March 12, 2015, 10:28:21 am by GWS
Hi,

This is very crude ..  ::) and I'm not very used to EBasic/IWB ..  This is currently in EBasic 'cos that's what I've got loaded.

It shows an image displayed as a sprite on a DX screen.

This is overlaid with an almost black sprite that can be alpha-blended with the background image.

Setting the dark sprite's alpha to 255 gives fully transparent, and the background image is bright.

Setting the alpha to a low value (say 20) blends the dark sprite fully to give a pretty near black image.

Use the Up and Down arrows to change the lightness (Up arrow) and darkness (Down Arow).

Don't know if this could be a starting point of what you need Egil ..  :)

Code Select

' Test of darkening an image using an alpha blended black sprite
' Uses Direct X
'

DEF pBuffer,background as POINTER
def wW,wH,bval:int

wW = 1024
wH = 768

CREATESCREEN(wW,wH,32)

'Set the drawing mode for text
DRAWMODE BackBuffer,@TRANSPARENT
FRONTPEN BackBuffer,RGB(255,255,0)

background = LOADSPRITE(GETSTARTPATH + "a1.jpg")
spritedrawmode(background,@SCALED)
SpriteScaleFactor background,2.0

' load the dark sprite ..
dark = LOADSPRITE(GETSTARTPATH + "dark.jpg")

SpriteDrawMode dark, @alpha
bval = 255
SpriteAlpha dark, bval  :' set to max brightness initially

DO

if getkeystate(0x26) :' up arrow pressed - so increase brightness
bval = bval + 20
if bval > 255 then bval =255
endif

if getkeystate(0x28) :' down arrow pressed - so decrease brightness
bval = bval - 20
if bval < 20 then bval = 20  :' not lower than 20
endif

FILLSCREEN 0
WRITETEXT 0,0,"Press ESC to close"
WRITETEXT 300,0,"Use Up and Down Arrows to vary Darkness"

' Draw sprites
  DRAWSPRITEXY background,350,250

SpriteAlpha dark, bval
DRAWSPRITEXY dark,0,0

FLIP

UNTIL KEYDOWN(0x01)


FREESPRITE background
FREESPRITE dark

CLOSESCREEN
END


Using Direct X windows is a bit tricky ..

Best wishes, :)

Graham


Tomorrow may be too late ..

Egil

Quote from: GWS on March 12, 2015, 10:24:39 am
... Don't know if this could be a starting point of what you need Egil ..  :)


It is, and a very good one too, Thanks Graham!
I did not know what this method was called in english, but AlphaBlend was the word I needed. Have found a lot of text on MSDN on the subject, and a couple of useful API's. I will study it all carefully to see if I can find a way to use the information in my program.


Regards,
Egil


P.S.
Right now I am babysitting a little lady, who never stops to be impressed when I get messages from Winnie....  ;D


Support Amateur Radio  -  Have a ham  for dinner!

fasecero

I have checked some old code from myself and found the ImageFilter(...) function detailed below. Works only for bmp's with 24 or 32 bits and, in no way, is optimized for larger images.

The function works directly on the bitmap, so it's a good idea not to pass the original bitmap, but a copy instead. Calling the function more than one time with the same bitmap will lose color information.

The delta argument adds hue, saturation or brightness to positive values and removes these properties for negative values.

For example to add some brightess: ImageFilter(bitmap, 3, 0.10)

Code Select


' property: 1 hue, 2 saturation, 3 value
' delta: (-359 to 359) for hue, (-1 to 1) for saturation or value
SUB ImageFilter(uint hbitmap, INT property, float delta)
BITMAP bmp
pointer buffer
float r, g, b
float h, s, v

' get the bitmap info
_GetObject(hbitmap, LEN(bmp), &bmp)

' get the pixels
int datalen = bmp.bmWidthBytes * bmp.bmHeight
buffer=AllocHeap(datalen)
_GetBitmapBits(hbitmap, datalen, buffer)

' do something with the pixels
pointer pixel = buffer
int pixelcount = bmp.bmWidth * bmp.bmHeight

float fWidth = 1.0/bmp.bmWidth

if (bmp.bmBitsPixel = 32)
settype pixel, RGBQUAD
int x = 0, i = 0

FOR i=1 TO pixelcount

b = *pixel.rgbBlue / 255.0
g = *pixel.rgbGreen / 255.0
r = *pixel.rgbRed / 255.0

RGBtoHSV(r, g, b, h, s, v)

SELECT property
CASE 1 : ModifyHueValue(h, delta)
CASE 2 : ModifyPixelValue(s, delta)
CASE 3 : ModifyPixelValue(v, delta)
ENDSELECT

HSVtoRGB(r, g, b, h, s, v)

*pixel.rgbBlue  = b *255
*pixel.rgbGreen = g *255
*pixel.rgbRed   = r *255

pixel += len(RGBQUAD)
' this is to keep track for bright
x++
if (x = bmp.bmWidth)
x = 0
endif
NEXT i

elseif (bmp.bmBitsPixel = 24)

settype pixel, RGBTRIPLE
x = 0

FOR i=1 TO pixelcount

b = *pixel.rgbtBlue / 255.0
g = *pixel.rgbtGreen / 255.0
r = *pixel.rgbtRed / 255.0

RGBtoHSV(r, g, b, h, s, v)

SELECT property
CASE 1 : ModifyHueValue(h, delta)
CASE 2 : ModifyPixelValue(s, delta)
CASE 3 : ModifyPixelValue(v, delta)
ENDSELECT

HSVtoRGB(r, g, b, h, s, v)

*pixel.rgbtBlue  = b *255
*pixel.rgbtGreen = g *255
*pixel.rgbtRed   = r *255

pixel += len(RGBTRIPLE)
x++
if (x = bmp.bmWidth)
x = 0
' next line aligned to WORD
pixel += (pixel & 1)
endif
NEXT i

else
endif
' set the bitmap info
_SetBitmapBits(hbitmap, datalen, buffer)
FreeHeap(buffer)
ENDSUB

' r,g,b values are from 0 to 1
' h = [0,360], s = [0,1], v = [0,1]
' if s == 0, then h = -1 (undefined)
sub RGBtoHSV( float r, float g, float b, float h BYREF, float s BYREF, float v BYREF)
float min, max, delta

IF r < g THEN min = r ELSE min = g
IF min  < b THEN min = min ELSE min = b

IF r > g THEN max = r ELSE max = g
IF max  > b THEN max = max ELSE max = b

v = max ' v
delta = max - min
if( max <> 0 ) then
s = delta / max ' s
else
' r = g = b = 0 ' s = 0, v is undefined
s = 0
h = -1
return
ENDIF

if( r = max ) THEN
h = ( g - b ) / delta ' between yellow & magenta
elseif( g = max ) THEN
h = 2 + ( b - r ) / delta ' between cyan & yellow
else
h = 4 + ( r - g ) / delta ' between magenta & cyan
endif

h *= 60 ' degrees
if( h < 0 ) THEN h += 360
endsub

sub HSVtoRGB( float r byref, float g byref, float b byref, float h, float s, float v )
int i
float f, p, q, t
if( s = 0 ) then
' achromatic (grey)
r = g = b = v
return
endif

h /= 60 ' sector 0 to 5
i = floor( h )
f = h - i ' factorial part of h
p = v * ( 1 - s )
q = v * ( 1 - s * f )
t = v * ( 1 - s * ( 1 - f ) )
select( i )
case 0:
r = v
g = t
b = p
case 1:
r = q
g = v
b = p
case 2:
r = p
g = v
b = t
case 3:
r = p
g = q
b = v
case 4:
r = t
g = p
b = v
default: ' case 5:
r = v
g = p
b = q
endselect
endsub

SUB ModifyPixelValue(float value BYREF, float delta)
value = value + delta

IF value > 1 THEN value = 1
IF value < 0 THEN value = 0
ENDSUB

SUB ModifyHueValue(float value BYREF, float delta)
value = value + delta

IF value > 359 THEN value = 359
IF value < 0 THEN value = 0
ENDSUB




Egil

March 13, 2015, 03:40:17 am #18 Last Edit: March 13, 2015, 03:42:09 am by Egil
Thanks fasecero for the great code snippet!

I have noticed that most maps available for download from the present mapservers, are either in the different GPS manufacturer's proprietary formats, or in the PNG or JPG formats.
But I guess that if the structures of the different pictorial formats are taken into account, your technique should also work in other formats than BMP.

When I started this part of my project, I knew absolutely nothing about graphic formats and how to manipulate pictures, but now I am beginning to understand that it is a large, and complex, theme. So I have still much to learn....

But think I'll take a break this weekend, and watch TV with some of my friends. Just to "clean" my poor old brain. There is biathlon in Finland, skijumping in Trondheim (Norway), and cycling (Paris - Nice) in France. You know, all these important things I really can't afford to miss... :D ;D ;D

Support Amateur Radio  -  Have a ham  for dinner!

GWS

Hi,

Here's the direct way of darkening an image  ;D - pixel by pixel .. :P

It's a bit crude, and does have the snag that it takes a second or two ..  ::)

The original image is loaded offscreen top right, to maintain it's original brightness.

Maybe the darkening could be speeded up a bit, or perhaps processed offscreen - and then presented onscreen when ready.

Anyway, here's my rough and ready code, and the image ..

Code Select

' Test for Image Brightness

def w:window
def wstyle:int

wstyle = @minbox
wW = 600
wH = 400

' open a window ..
openwindow w,-wW,0,wW,wH,wstyle,0,"IWBasic Test",&messages
setwindowcolor w,rgb(5,60,90)
centerwindow w

control w,@BUTTON,"Exit",(wW-70)/2,310,70,30,0,1
control w,@BUTTON,"Darker",150,270,70,30,0,2
control w,@BUTTON,"Lighter",380,270,70,30,0,3

pic = LOADIMAGE(GETSTARTPATH + "a1.jpg", 4)
'save original image off screen to the top right.. the image is 300 by 200 pixels.
showimage w,pic,4,wW,0
' show working image on screen ..
showimage w,pic,4,150,50

light = 1.0 :' full brightness

run = 1

WAITUNTIL run = 0
deleteimage pic,4
CLOSEWINDOW w
END

SUB messages(),int
select @class
case @idclosewindow
run = 0
case @idcontrol
select @controlID
case 1
run = 0
case 2 :' Darker

light = light - 0.2
if (light < 0.0) then light = 0.0

for i = wW to wW + 300
for j = 0 to 200
clr = GETPIXEL(w, i ,j)
' Extract red, green and blue
pr = clr & 0x0000FF
pg = (clr & 0x00FF00) / 256
pb = (clr & 0xFF0000) / 65536

pr = pr*light
if pr < 10 then pr = 10
pg = pg*light
if pg < 10 then pg = 10
pb = pb*light
if pb < 10 then pb = 10
' show darker image on screen
pset w,i-wW+150,j+50,rgb(pr,pg,pb)
next j
next i

case 3 :' Lighter

light = light + 0.2
if (light > 1.0) then light = 1.0

for i = wW to wW + 300
for j = 0 to 200
clr = GETPIXEL(w, i ,j)
' Extract red, green and blue
pr = clr & 0x0000FF
pg = (clr & 0x00FF00) / 256
pb = (clr & 0xFF0000) / 65536

pr = pr*light
if pr > 255 then pr = 255
pg = pg*light
if pg > 255 then pg = 255
pb = pb*light
if pb > 255 then pb = 255

pset w,i-wW+150,j+50,rgb(pr,pg,pb)
next j
next i

endselect

endselect
RETURN(0)
endsub



Best wishes, :)

Graham

Tomorrow may be too late ..

Egil

Graham, you never stop to amaze me!

This is excactly what I needed. And since once the darkening level is found, it can be saved in a cfg file, and then use this value to process the map offscreen before showing it next time the program is started.
And off course make it possible to change later, via a menu...

This is an excellent way to demonstrate how simple, yet effective, things can be done. You really did it again!

Thank you very much.


Regards,
Egil


Support Amateur Radio  -  Have a ham  for dinner!

ckoehn

One more thing...

This is based on Grahams code with saperos speed trick..
Code Select
' Test for Image Brightness

def w:window
def wstyle:int

wstyle = @minbox
wW = 600
wH = 400

' open a window ..
openwindow w,-wW,0,wW,wH,wstyle,0,"IWBasic Test",&messages
setwindowcolor w,rgb(5,60,90)
centerwindow w

control w,@BUTTON,"Exit",(wW-70)/2,310,70,30,0,1
control w,@BUTTON,"Darker",150,270,70,30,0,2
control w,@BUTTON,"Lighter",380,270,70,30,0,3

pic = LOADIMAGE(GETSTARTPATH + "a1.jpg", 4)
'save original image off screen to the top right.. the image is 300 by 200 pixels.
showimage w,pic,4,wW,0
' show working image on screen ..
showimage w,pic,4,150,50

light = 1.0 :' full brightness

run = 1

WAITUNTIL run = 0
deleteimage pic,4
CLOSEWINDOW w
END

SUB messages(),int
select @class
case @idclosewindow
run = 0
case @idcontrol
int hdcOld = w.m_hPrintDC
w.m_hPrintDC = GETHDC(w)

select @controlID
case 1
run = 0
case 2 :' Darker

light = light - 0.2
if (light < 0.0) then light = 0.0

For i = wW to wW + 300
for j = 0 to 200
clr = GETPIXEL(w, i ,j)
' Extract red, green and blue
pr = (clr & 0x0000FF)
pg = ((clr & 0x00FF00) / 256)
pb = ((clr & 0xFF0000) / 65536)

pr = pr*light
if pr < 10 then pr = 10
pg = pg*light
if pg < 10 then pg = 10
pb = pb*light
if pb < 10 then pb = 10
' show darker image on screen
pset w,i-wW+150,j+50,rgb(pr,pg,pb)

next j
next i

case 3 :' Lighter

light = light + 0.2
if (light > 1.0) then light = 1.0

for i = wW to wW + 300
for j = 0 to 200
clr = GETPIXEL(w, i ,j)
' Extract red, green and blue
pr = clr & 0x0000FF
pg = (clr & 0x00FF00) / 256
pb = (clr & 0xFF0000) / 65536

pr = pr*light
if pr > 255 then pr = 255
pg = pg*light
if pg > 255 then pg = 255
pb = pb*light
if pb > 255 then pb = 255

pset w,i-wW+150,j+50,rgb(pr,pg,pb)
next j
next i
endselect
int hdc = w.m_hPrintDC
w.m_hPrintDC = hdcOld
RELEASEHDC(w, hdc)

endselect
RETURN(0)
endsub


Later,
Clint

GWS

Oh, thanks Clint .. that makes a huge difference  ;D ;D

I'd remembered there was such a method, but I'd forgotten how it was done.

Excellent ..  :)

Graham
Tomorrow may be too late ..

Egil

Thanks Clint!

When I think of it, sapero always had a trick up his sleeve, and it alway made a difference. I really miss him!

And when you guys have been twisting your brains to help me out, we have been out fishing.  Some old navy friends and me usually get together a weekend this time of the year for an "all boys - all sports" event. The activities are strictly sports related... fishing, diving, watching sports events on the TV, telling all sorts of stories ( and some of the wifes would add: "drinking too much beer" ...hehe).
The sea was a little rough for diving along the reef, but we  got a lot of fish. Cod, saithe and redfish. I just put the redfish in a tray with plenty of salt, so tomorrow we'll have salted redfish for dinner.

So the rest of the weekend will be of the "no computers" kind.


Regards,
Egil

Support Amateur Radio  -  Have a ham  for dinner!

LarryMc

Clint:
I had been playing with what you are doing. It works pretty good with a small window.
Your example was with a 600x400 window.

When I use Egil's 3600x1800 jpg image of the world with a full screen window on my 1920x1080 display it takes about 2 secs for the image to change colors after you click the button.
And the other drawback I see to that scheme is that the incremental change is fixed in the program but it could be made as a user defined parameter.
==========================
With my current effort I've got something working that changes the color, for all practical purposes, instantly, and, can be implemented with a slider control for very fine shade control.  Also, zoom control can be multi-level with double-clicking left/right mouse buttons or context menu or a slider control like the shade control.  All of this impacts only the image; there's no alpha blending etc.

Whatever scaling/zoom methods are used will have to be applied to plane placement within the client area.

Right now I'm modifying Egil's Mapview app he posted to incorporate my efforts.  Oh, did I forget to mention that, as usual, I'm using something somebody else adapted about 7 years ago and not something I created(and Sapero and PT weren't involved  ;) )
==========================

Egil:
I'm pretty sure this is exactly what you were looking for.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library