Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Why does this output Black Images with memblocks?

Author
Message
Programmer X
18
Years of Service
User Offline
Joined: 14th Nov 2007
Location:
Posted: 15th May 2014 05:22
Rem Project: DepthCube
Rem Created: Tuesday, May 13, 2014

Rem ***** Main Source File *****
remstart
input "input the quadrant of your vector",quad
input "input an Uangle between 0 and 90",Uang
input "input an V angle between 0 and 90",Vang
remend
rem up is minus down is plus
Draw Sprites last
Aratio#=(820.0-150.0)/340
rem sin(u)sin(v)=(820.0-150.0)/340
memIn=1
memOut=2
imgOut=5
sizeA=215
sizeB=94
sizeC=42
sizeD=21
sizeE=10
sizeF=4
SizeG=1
WantY#=42
WantX#=94
Realratio#=1-Aratio#
rem 0.45252955019514786608234241595826
rem 61.46829888860462499768902247141
rem 26 degrees
rem 35,42
rem depth is cross product sin
rem k^x/x*sin*sin*R
img=1
x=1
y=1
width=821
height=342


rem k^x/x-> dx 1/2k k=<1 slope=<.5 or ~30 degrees
AnglestartU#=35
AnglestartV#=42
Minwidth#=((1/8*cos(WantY#/WantX#))^(Realratio#*sqrt(2))/RealRatio#)
wait key
load bitmap "C:\Users\Dan\Pictures\AATexture.bmp",1

CheckY#=sin(AnglestartU#/90*asin(1))*sin(AnglestartV#/90*asin(1))*WantX#

Scalar=Floor(10*(WantY#/CheckY#))

SizePos#=SizeA

rem replace with logarithm later
repeat
a#=sin(AnglestartU#/90*asin(1))*sin(AnglestartV#/90*asin(1))*SizePos#
if a#>WantY#
SizePos#=sin(AnglestartU#/90*asin(1))*sin(AnglestartV#/90*asin(1))*SizePos#
SizeSum#=SizeSum#+SizePos#
endif
until a#<WantY#

MaxY=INT(SizeSum#)
MaxX=411+Floor(WantX#/2)
MinX=411-Floor(WantX#/2)
MinY=MaxY-INT(SizePos#)

Get image 1,MinX,MinY,MaxX,MaxY
delete bitmap 1
gosub subImage
do
paste Image 5,0,0
loop
end
subImage:
CountGX=0
skip=0
rem Create a memblock from the input image
make memblock from image 1, img

rem We need to know the width of the input image in order to address it's pixels by 2d coordinates (X & Y)
baseWidth = memblock dword(memIn, 0)
baseHeight = ((memblock dword(memIn, 4)*Scalar/10)+Scalar) rem Store the height too, so that we will not end up with out of bounds coordinates / invalid sampling if requested to get pixel (48, 10) from a 32x32 image

rem Verify the size of the sub-image
if x + width > baseWidth then width = baseWidth - x rem The sub-image must be rescaled to fit within the main image
if y + height > baseHeight then height = baseHeight - y
rem If the initial X or Y coordinates are beyond the size of the main image the whole operation is illegal
if x >= baseWidth or y >= baseHeight then Fail("Sub-image bounds illegal in this context!")

rem The output memblock must be large enough to hold the image data, that is 4 bytes per pixel + 12 bytes for the header.
memSize = (width * height * 4) + 12
make memblock memOut, memSize
write memblock dword memOut, 0, width
write memblock dword memOut, 4, height
write memblock dword memOut, 8, 32 rem We're assuming that all images are 32-bit here as well
slope#=(((WantX#)-Minwidth#)/(WantY#))
rem Copy pixels from the input to the output memblock
rem Pixel indices start at 0 and range to width/height - 1, since the 0 counts towards the total dimensions
maxuv=(1.0*height*(1-Scalar)/10)-1
for py = 0 to maxuv
countGX=CountGX+1
for px = 0 to width - 1
if CountGX=Scalar and (y + py+skip)<WantY#
CountGX=0
skip=skip+1
endif
rem The (px, py) coordinates are the pixels of the output sub-image.
rem To retrieve the corresponding source image pixel coordinates, we simply need to add the x and y offsets provided as arguments to this function.
agk=(y + py+skip)
pixelOffset = (agk * baseWidth) + x + px rem This is the pixel number in the source image, counted left to right, top to bottom
rem To get the pixel's position in the memory block we multiply its number by 4 (since each pixel is assumed to be stored as a DWORD; 4 bytes)
rem and add the size of the header section (12 bytes) to it.
sourceColour = memblock dword(memIn, 12 + (pixelOffset * 4))
rem The process is the same for the destination image, only that we don't need to add the x and y offsets
pixelOffset = (py * width) + px
write memblock dword memOut, 12 + (pixelOffset * 4), sourceColour
next px
next py

rem When we get here, all pixels in the supplied range (cut-off if the dimensions of the sub-image would have it go beyond the dimensions of the source image)
rem have been copied to memOut.
rem Convert that to an image, clean up the used memblocks and return the created image ID
make image from memblock imgOut, memOut

delete memblock memIn
delete memblock memOut
return


rem This is obviously not a great error-handling function; replace with whatever you require
function Fail(msg as string)
exit prompt msg, "Error"
end
endfunction

XXX
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 15th May 2014 20:05 Edited at: 15th May 2014 20:06
Please use code tags next time... this way the code is very hard to read as all the indentation is lost.

Generally, there are many possibilities why the code doesn't work the way it's supposed to:
1. The "get image" command at line 67 doesn't produce the expected results (you might need to use set current bitmap 1, I'm not sure if load bitmap does that automatically)
2. The pasting of the output image doesn't work properly (you might need to use set current bitmap 0 after deleting bitmap 1... not sure about that one either)
3. The data isn't written to the memblock correctly, because...
3.1 write memblock dword always writes rgb values of 0 to the memblock due to a problem with sourceColor or
3.2 the for px/py loops don't work as expected and hence not all pixels are traversed, leaving them at their default value of black or
3.3 the for loops work as expected but your memblock pointers (pixelOffset) are off

As you haven't given any information other than the code, I can't really tell which of these points causes the undesired behaviour of your program. But most of them are pretty easy to test by changing very few commands temporarily. (For instance, in order to make sure 3.2 or 3.3 isn't the case, replace "sourceColor" in line 116 by rgb(255,0,0) and check whether a red image will be displayed.)

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th May 2014 17:50
The problem seems to be in your calculation of which pixels to copy. The new memblock stays black because no pixels are in fact copied. I suggest you check that part of your code carefully.



Powered by Free Banners

Login to post a reply

Server time is: 2026-07-06 19:40:28
Your offset time is: 2026-07-06 19:40:28