Strange behavior while creating and saving a large BITMAP PowerBASIC Peer Support Community (original) (raw)


I'm making a large BITMAP (10000 pixels by 9000 pixels) and coloring each pixel according to some function. The complete code is NOT shown because it uses a color function that has many, many dependencies. However, I can give some basic pseudo-code:
Code:
FUNCTION PBMAIN () AS LONG
LOCAL y AS LONG
LOCAL x AS LONG
LOCAL xPixel AS LONG
LOCAL yPixel AS LONG
LOCAL hBMP_big AS DWORD
LOCAL HexColor AS DWORD
x = 32000 : y = 0
GRAPHIC BITMAP NEW 10000, 9000 TO hBMP_big

FOR yPixel=0 TO 8999
FOR xPixel=0 TO 9999
HexColor = SomeUnshownFunction that uses xPixel and yPixel, returns an RGB color value in DWORD
' attach to the big target bitmap each time because the HexColor
' function above attaches to other bitmaps
GRAPHIC ATTACH hBMP_big, 0, REDRAW
GRAPHIC SET PIXEL (xPixel, yPixel), HexColor
IF RND(1, 1400) = 4 THEN GRAPHIC REDRAW
IF ERR > 0 THEN PRINT ERR; ERROR$(ERR)
NEXT xPixel
NEXT yPixel
GRAPHIC ATTACH hBMP_big, 0
GRAPHIC REDRAW
GRAPHIC SAVE "+." + DEC$(x, 5) + "." + DEC$(y, 5) + ".bmp"
GRAPHIC BITMAP END
END FUNCTION
Here's what's happening: I first started with smaller bitmaps (e.g.: 1000 by 1000 pixels) and everything was fine. Everything ran as expected. But as I increased the size of the bitmap, two things happened. First, I would get no error, but the bitmap would not fully render. The saved .BMP file would be the right size, but it looks like PowerBasic stopped plotting pixels about 1/3 the way thru (and stopped mid-loop). I tried inserting a periodic REDRAW, but it made no difference. Secondly, bizarrely, the saved .BMP file has the filename:
Yes, really, with that crazy Chinese Unicode character. But when I scale it back down to a smaller bitmap, all these problems go away.
I'm guessing that I'm overflowing something. But what? And why no error message? How is the output filename getting corrupted?
Am I just pushing the limits of PB's BITMAP functions too hard?
Christopher P. Becker
signal engineer in the defense industry
Abu Dhabi, United Arab Emirates * Borje Hagsten
Member
* Join Date: Jan 2000
* Posts: 8891

What happens if you increase digits from 5 to 6 or 7? (DEC$(x, 5))
## Comment * Patrice Terrier
Member
* Join Date: Aug 1998
* Posts: 4703

I suspect you are running out of memory
Note REDRAW does nothiing with a Bitmap, it is only relevant to a Graphic Window (or a PBWin Graphic Control)
And it was very slow even without complicated HexColor calculations (about 1.5 minutes)
(90 million iterations.)
Take a look at GRAPHIC GET BITS, GRAPHIC SET BITS and pointers.
Last edited by Stuart McLachlan; 17 Jan 2025, 05:01 PM.
=========================
https://camtech.com.pg
=========================
## Comment * Christopher Becker
Member
* Join Date: Jun 2005
* Posts: 412

Originally posted by Borje Hagsten View Post
What happens if you increase digits from 5 to 6 or 7? (DEC$(x, 5))
This filename...
Christopher P. Becker
signal engineer in the defense industry
Abu Dhabi, United Arab Emirates
## Comment * Stuart McLachlan
Member
* Join Date: Mar 2000
* Posts: 11080

About 1 second including 90 000 000 function calls versus 1.5 minutes with no function calls. And no issue with filename:
'
Code:
#COMPILE EXE
#DIM ALL
' #OPTION LARGEMEM32 may be needed if the rest of the application uses a lot of memory.
FUNCTION PBMAIN () AS LONG
LOCAL y AS LONG
LOCAL x AS LONG
LOCAL xPixel AS LONG
LOCAL yPixel AS LONG
LOCAL hBMP_big AS DWORD
LOCAL HexColor AS DWORD
LOCAL t AS DOUBLE
LOCAL PixelPtr AS LONG PTR
LOCAL i AS LONG
LOCAL sBmp AS STRING
x = 32000 : y = 0
t = TIMER
GRAPHIC BITMAP NEW 10000,9000 TO hBMP_big
GRAPHIC ATTACH hBMP_big, 0
GRAPHIC GET BITS TO sBmp
GRAPHIC BITMAP END ' free up hBmp_big memory for whatever Hexcolor() does
PixelPtr = STRPTR(sBmp) + 8
FOR ypixel = 0 TO 8999
FOR xpixel = 0 TO 9999
HexColor = TestFunction(xPixel,ypixel)
@PixelPtr = BGR(Hexcolor)
INCR PixelPtr
NEXT
NEXT
GRAPHIC BITMAP NEW 10000,9000 TO hBMP_big 'get the bits back into a bitmap.
GRAPHIC ATTACH hBMP_big, 0
GRAPHIC SET BITS sBmp
RESET sBmp
GRAPHIC SAVE "+." & DEC$(x, 5) & "." & DEC$(y, 5) & ".bmp"
GRAPHIC BITMAP END
? FORMAT$(TIMER -t,"0.00"); " seconds"
WAITKEY$
END FUNCTION
FUNCTION Testfunction(x AS LONG,y AS LONG) AS LONG
FUNCTION = (x * y) MOD &H00FFFFFF
END FUNCTION
'
Last edited by Stuart McLachlan; 18 Jan 2025, 12:30 AM.
=========================
https://camtech.com.pg
=========================
## Comment * Stuart McLachlan
Member
* Join Date: Mar 2000
* Posts: 11080

Is that info in this thread? I must be missing something. I thought the OP was the only person seeing an error.
"Not my circus, not my monkeys."
## Comment * Stuart McLachlan
Member
* Join Date: Mar 2000
* Posts: 11080

Originally posted by Eric Pearson View Post
Is that info in this thread? I must be missing something. I thought the OP was the only person seeing an error.
Testing,,I got the same strange file name at one stage, but I can'r reproduce it (which is why I've edited my last post, making your comment moot )
In my recycle bin:
Click image for larger version Name:	image.png Views:	231 Size:	17.9 KB ID:	838589
=========================
https://camtech.com.pg
=========================
## Comment * Stuart McLachlan
Member
* Join Date: Mar 2000
* Posts: 11080

Just dug up the relevant backup. The messed up filename was the result of an error on my behalf
Trying to speed up by commenting out unneeded operations, I got it when I commented out the ATTACH in the loop without placing it before the loop :
Result was GRAPHIC SET PIXEL with no attached bitmap!
Possibly a similar case in Christopher's function.
Code:
GRAPHIC BITMAP NEW 10000, 9000 TO hBMP_big
'should have moved the ATTACH to here!!!
FOR yPixel=0 TO 8999
FOR xPixel=0 TO 9999
HexColor = %RGB_RED
'SomeUnshownFunction that uses xPixel and yPixel, returns an RGB color value in DWORD
' attach to the big target bitmap each time because the HexColor
' function above attaches to other bitmaps
'GRAPHIC ATTACH hBMP_big, 0, REDRAW 'commented out but did not move it higher!!!
GRAPHIC SET PIXEL (xPixel, yPixel), HexColor
'IF RND(1, 1400) = 4 THEN GRAPHIC REDRAW
'IF ERR > 0 THEN PRINT ERR; ERROR$(ERR)
NEXT xPixel
NEXT yPixel
=========================
https://camtech.com.pg
=========================
## Comment * Dale Yarker
Member
* Join Date: Mar 2004
* Posts: 6249

a tid bit
character should be 0
0 is &h0030
Asian character showing is &h8030, so 1 bit corrupted
I have no theory why this particular error. I just wonder how more of the file name wasn't corrupted.
something I did not post yesterday because smaller bitmaps do not cause the problem
+ is not a legal file name character (from what I found on Google)
period is legal, but multiple periods could confuse where extension starts. (search left from end - OK, search right from last \ - not so much
(added - just searched again, + not listed as illegal either!)
suggest, repeat just suggesting
lose the +, use hyphen instead of period (one period for extension only)
((+ subject to added comment))
(edit - late to the party again. but above still applies )
Last edited by Dale Yarker; 18 Jan 2025, 11:21 PM.
Dale
## Comment * Stuart McLachlan
Member
* Join Date: Mar 2000
* Posts: 11080

Originally posted by Dale Yarker View Post
+ is not a legal file name character (from what I found on Google)
According to MS, it is OK in Windows file systems: https://learn.microsoft.com/en-us/wi.../naming-a-file
(if you are targeting another file system that doesn't allow it, it would be problematic.)
* Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
* The following reserved characters:
* < (less than)
* > (greater than)
* : (colon)
* " (double quote)
* / (forward slash)
* \ (backslash)
* | (vertical bar or pipe)
* ? (question mark)
* * (asterisk)
* Integer value zero, sometimes referred to as the ASCII NUL character.
* Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.
* Any other character that the target file system does not allow.
But I agree wholeheartedly that, as a rule, you should avoid anything other than alphanumerics, hyphens and underscores in file names and only use a single period to designate an extension. ( Spaces in file names are the bane of my life).
Hmmm, Everything tells me I have 3 513 objects starting with a period . Most of them are connected with Firefox and most of the rest are either web related (under WAMPServer ) or applications that have both Windows and Linux versions)
=========================
https://camtech.com.pg
=========================
## Comment * Dale Yarker
Member
* Join Date: Mar 2004
* Posts: 6249

Most of them are connected with Firefox ...
relative paths maybe "period backslash" ?
((forum removes the backslash, so spelled it out))
Dale
## Comment * Stuart McLachlan
Member
* Join Date: Mar 2000
* Posts: 11080

Originally posted by Dale Yarker View Post
relative paths maybe "period backslash" ?
((forum removes the backslash, so spelled it out))
Nope, FF creates a profile folder for every site visited and each one contains , inter alia, a .metadata-v2 file and a ls folder that contains a small sqlite file
=========================
https://camtech.com.pg
=========================
## Comment * Eric Pearson
Member
* Join Date: Oct 1987
* Posts: 10807

I stand by my previous guess.
"Not my circus, not my monkeys."
## Comment * Michael Mattias
Member
* Join Date: Aug 1998
* Posts: 43772

Just had a thought. If using the much more efficient pointer to a string and GRAPHIC SET BITS method in Post #6, there's no need to create the bitmap initially, just create the string directly
'
Code:
' GRAPHIC BITMAP NEW 10000,9000 TO hBMP_big
' GRAPHIC ATTACH hBMP_big, 0
' GRAPHIC GET BITS TO sBmp
' GRAPHIC BITMAP END ' free up hBmp_big memory for whatever Hexcolor() does
sBmp= MKL$(10000) & MKL$(9000) & NUL$(10000*9000 * 4)
'
=========================
https://camtech.com.pg
=========================
## Comment * Eric Pearson
Member
* Join Date: Oct 1987
* Posts: 10807

Nah. You don't even need graphics for this.
Use MS Paint to create and save a "template" bitmap of the correct size, all black.
Dim a 2D DWORD array, fill it with numbers as needed.
Open the bitmap file, SEEK to the end of the header (LOF minus array data length), PUT the array into the file. Done. Bitmap headers contain only _meta_data; size, color depth, etc. The pixels don't matter; you can simply replace them without changing the header.
All numeric, tiny program, no strings or graphics. Fast.

"Not my circus, not my monkeys."

Comment