The QB64 Edition

Full Version: Basic Dithering/Convert to BW
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is my simple method to convert an Image to B/W. Can someone help me doing Bayer Dithering?

_FULLSCREEN: _MOUSEHIDE: IF COMMAND$ = "" THEN Parameter$ = "earth.jpg" ELSE Parameter$ = COMMAND$
DIM SHARED VideoWidth: VideoWidth = 800
DIM SHARED VideoHeight: VideoHeight = 600
DIM SHARED PointReturn AS _UNSIGNED LONG
SCREEN _NEWIMAGE(VideoWidth, VideoHeight, 32)
Image = _LOADIMAGE(Parameter$, 32)
_PUTIMAGE (0, 0), Image: SLEEP: RenderDithering

SUB RenderDithering 'Actual Rendering!
DO
DO
x = x + 1
PointReturn = POINT(x, y)
IF PointReturn < 4289243304 THEN PSET (x, y), _RGB32(0, 0, 0) ELSE PSET (x, y), _RGB32(255, 255, 255)
IF x = VideoWidth THEN EXIT DO
LOOP
IF y = VideoHeight THEN EXIT DO
y = y + 1
x = 0
LOOP
END SUB
Aureal,

That is a really cool threshold demo. Thank you for sharing.

There is a code box feature you can use to place any source code in. This helps in not only viewing the code better since it uses a source code font, but it allows quick and easy selecting of all the code. You can see this feature below.

Since you did not share any images to use with the source code, I had to use my own, which is fine, but many people will not take the time or effort to try out a demo, which is sad, but I have seen it.

I found the perfect image to us in your demo that contains lighting from light to dark. It is an photo of a cat I had a few years back until she passed away. This was one of the best pictures I had taken of her. Her name was Halo.

I decided to share my picture so it could be used in your demo, and since I did, I went a step further. I modified your demo a slight bit to show how rotating through some threshold values would look animated.

You can download the source code and photo below in the *.ZIP file, but here is the modified code so you can quickly see it with downloading it first:

Code Snippet: [Select]
_FULLSCREEN
_MOUSEHIDE

'IF COMMAND$ = "" THEN Parameter$ = "earth.jpg" ELSE Parameter$ = COMMAND$

DIM SHARED VideoWidth
DIM SHARED VideoHeight
DIM SHARED PointReturn AS _UNSIGNED LONG
REDIM MyCat(-1) AS _UNSIGNED LONG
DIM Threshold AS _FLOAT
DIM NumberOfPics AS _UNSIGNED INTEGER
DIM MaxThreshold AS _UNSIGNED INTEGER
DIM Red AS _UNSIGNED _BYTE
DIM Green AS _UNSIGNED _BYTE
DIM Blue AS _UNSIGNED _BYTE
DIM Alpha AS _UNSIGNED _BYTE

VideoWidth = 800
VideoHeight = 600
NumberOfPics = 60 '32
MaxThreshold = 256

REDIM MyCat(NumberOfPics)

MyImage$ = "MyCat.jpg"

SCREEN _NEWIMAGE(VideoWidth, VideoHeight, 32)
Image = _LOADIMAGE(MyImage$, 32)

_SOURCE Image

Threshold = MaxThreshold / NumberOfPics

' Create offscreen images
FOR i = 0 TO NumberOfPics - 1

   _DEST 0

   LOCATE 18, 35
   PRINT "Rendering MyCat Image:"; i; " / "; NumberOfPics

   MyCat(i) = _NEWIMAGE(800, 600, 32)

   Red = (NumberOfPics - 1 - i + 1) * Threshold - 1
   Green = (NumberOfPics - 1 - i + 1) * Threshold - 1
   Blue = (NumberOfPics - 1 - i + 1) * Threshold - 1
   Alpha = 255

   _DEST MyCat(i)
   RenderDithering Red, Green, Blue, Alpha

   _DISPLAY


NEXT

_PRINTMODE _KEEPBACKGROUND

_DEST 0

_PUTIMAGE (0, 0), Image

LOCATE 15, 4: PRINT "PRESS ANY KEY TO ITERATE"
LOCATE 16, 4: PRINT "THROUGH"; NumberOfPics; "FRAMES"

_DISPLAY

SLEEP

pic = 0
picIterate = 1

_PRINTMODE _KEEPBACKGROUND


DO
   _LIMIT 10

   _PUTIMAGE (0, 0), Image
   _PUTIMAGE (0, 0), MyCat(pic)

   LOCATE 15, 4: PRINT "PRESS <ESC> TO EXIT";
   LOCATE 16, 4: PRINT "Pic #:"; pic; "/"; (NumberOfPics - 1);

   pic = pic + picIterate
   IF pic < 1 OR pic > NumberOfPics - 2 THEN picIterate = -picIterate

   _DISPLAY

LOOP UNTIL _KEYHIT = 27

SYSTEM


SUB RenderDithering (Red AS _UNSIGNED _BYTE, Green AS _UNSIGNED _BYTE, Blue AS _UNSIGNED _BYTE, Alpha AS _UNSIGNED _BYTE) 'Actual Rendering!

DO
   DO
       x = x + 1
       PointReturn = POINT(x, y)

       'IF PointReturn < 4289243304 THEN PSET (x, y), _RGB32(0, 0, 0) ELSE PSET (x, y), _RGB32(255, 255, 255)
       IF PointReturn < _RGBA32(Red, Green, Blue, Alpha) THEN
           PSET (x, y), _RGBA32(0, 0, 0, 0)
       ELSE
           PSET (x, y), _RGBA32(0, 255, 0, 128)
       END IF

       IF x = VideoWidth THEN EXIT DO
   LOOP
   IF y = VideoHeight THEN EXIT DO
   y = y + 1
   x = 0
LOOP
END SUB

Here is a screenshot of the DEMO:

[attachment=948]

As for the Bayer dithering... Well... That will be for another thread all together.


Walter Whitman
The Joyful Programmer
Say Aureal, are you the same guy who made an interpreter for, what was it? O2 BASIC? RubenDev perhaps? Just checking.
HI STATIC
NO i think is not ,My nick is Aurel
and i am aouthor of AurelBasic and Ruben Interpreter.
I also remember your attempt to interpreter.
Nice to find this topic and you
Reference URL's