'--- 1st show the original image ---
_TITLE "The original Image, press any key..." ihan&
= _LOADIMAGE("AnyImage.jpg", 32) 'any image file (at least 1024x768)
'--- now make a darken cascade ---
_TITLE "Darken Cascade, press any key..." dark& = ModifyBrightness&(ihan&, -0.15, 250, 70, 820, -1)
darker& = ModifyBrightness&(dark&, -0.15, 275, 95, 795, -1)
'--------------------------------------------------------------------
'--- With activated _FREEIMAGE the freed handle is re-used by the
'--- next _COPYIMAGE call done in ModifyBrightness&() called right
'--- below the _FREEIMAGE, but the handle in question obviously gets
'--- not correctly initialized by _COPYIMAGE, as the _MEM function(s)
'--- used later to iterate through the image do fail to work on it
'--- (Critical Error #308 (memory was freed)).
'-----
'--- If otherwise the _FREEIMAGE call is commented out, then it works
'--- with _COPYIMAGE, as a new clean handle must be used instead.
'-----
'--- In alternative it also works with activated _FREEIMAGE, but change
'--- the _COPYIMAGE in FUNCTION ModifyBrightness&() into a combination
'--- of _NEWIMAGE and _PUTIMAGE instead to make the image copy.
'--------------------------------------------------------------------
darkest& = ModifyBrightness&(darker&, -0.15, 300, 120, 770, -1)
'--------------------------------------------------------------------
'--- done ---
'This function is a cutout from 'QB64Library\IMG-Support\imageprocess.bm',
'which is part of my Libraries Collection. It takes a source image handle
'and returns a modified image in a new handle usually made with _COPYIMAGE.
'This seems to fail, if for the copy an old (freed) handle is re-used.
'---------------------------------------------------------------------
'NAME
' ModifyBrightness -- Brightness adjustment, keep alpha
'
'TEMPLATE
' newImg& = ModifyBrightness& (shan&, change#, minX%, minY%, maxX%, maxY%)
'
' change# -- must be between -1.0 and 1.0 (clipped)
'
' DESCRIPTION
' This changes the general brightness of the image. The smaller the
' value is the darker the image becomes. 0.0 means no change.
' The alpha channel of the source image is retained and copied 1:1.
'---------------------------------------------------------------------
FUNCTION ModifyBrightness&
(shan&
, change#
, minX%
, minY%
, maxX%
, maxY%
) ModifyBrightness& = -1 'so far return invalid handle
'--- get source image size and a copy of the image ---
'--------------------------------------------------
'--- This works only, if a new handle must be used,
'--- and this works for both re-used and new handles
'nhan& = _NEWIMAGE(swid%, shei%, 32)
'_PUTIMAGE ,shan&, nhan&
'--------------------------------------------------
'--- check selected processing area ---
IF minX%
< 0 OR minX%
>= swid%
THEN minX%
= 0 IF maxX%
< 0 OR maxX%
>= swid%
THEN maxX%
= swid%
- 1 IF minY%
< 0 OR minY%
>= shei%
THEN minY%
= 0 IF maxY%
< 0 OR maxY%
>= shei%
THEN maxY%
= shei%
- 1 '--- process copied image ---
'--- build histogram transformation table ---
IF change#
< -1.0#
THEN change#
= -1.0 IF change#
> 1.0#
THEN change#
= 1.0 v%
= FIX(i%
* (change#
+ 1.0#
)) hist%(i%) = v%
'--- for speed we do direct memory access ---
'--- iterate through pixels ---
noff%& = nbuf.OFFSET + (y% * swid% * 4) + (minX% * 4)
'--- get pixel ARGB value from source ---
'--- modify pixel components ---
newA%
= _ALPHA32(orgb~&
) 'ignored (put through) '--- put new pixel ARGB value to dest ---
nrgb~&
= _RGBA32(newR%
, newG%
, newB%
, newA%
) '--- set next pixel offset ---
noff%& = noff%& + 4
'--- cleanup ---
'--- set result ---
ModifyBrightness& = nhan&