PDA

View Full Version : Manipulating Y8 image files with QBASIC


alexh110
6th February 2012, 23:49
Hi all,
I'm having some trouble reading raw Y8 image data into a QBASIC programme, in order to manipulate an image.

I'm using OPEN AS BINARY, along with GET to read the data from the file one byte at a time, and PUT to write out the manipulated data to a new image file.

Since Y8 is a simple planar format with 8-bit pixel depth, I expected the data to contain values from 0 to 255, representing each pixel's brightness.
However the values seem to be in a much wider range: perhaps ranging as high as 10,000! They also seem to be negative on occasion.

Obviously I've misunderstood something here; but I'm not sure what?

LoRd_MuldeR
7th February 2012, 00:17
I haven't used QBASIC for a decade, but probably GET doesn't read a single byte (value 0-255), but a 16- or 32-Bit word (value 0-65535 or 0-4294967295). Might even be interpreted as signed!

Also "BINARY" mode usually doesn't set the word-size for read/write operation, it just disables certain text-specific translations...

alexh110
7th February 2012, 00:32
Thanks, I guess that would make sense; though if that's true it should be trying to read data beyond the end of the file, so I would've expected an error.
Seems to be correctly reading in 720X576 items of data (corresponding to the dimensions of the source image), and writing out an image of identical file-size. The output image can be opened by PaintshopPro, and is displayed correctly.

Problem is, the manipulation I want to do will not work without the expected pixel-depth!

LoRd_MuldeR
7th February 2012, 00:38
After a quick Google search, it seems the "INTEGER" type of QBASIC is 16-Bit signed, the "LONG" type is 32-Bit signed.
http://www.svatopluk.com/qbtutor/tut16.htm

So that's probably also the word size that the GET operation will use and how it will interpret the data.

As far as I can see, GET only has a parameter for the offset (in bytes for binary-mode files), but not for the length to read :rolleyes:

Question: Why do you use something as antiqued and limited as QBASIC?

[EDIT]

See also here:
http://www.petesqbsite.com/sections/tutorials/tuts/loading_bmps.html

DIM Pixel AS STRING * 1 ' Our pixel "byte".
GET #1, , Pixel ' read pixel ' Read one pixel (byte)

So "AS STRING * 1" obviously does the trick to get a 1-Byte unsigned(?) variable in QBASIC ;)

alexh110
7th February 2012, 01:39
Aha, thanks!
I'll try that then.
I must've been reading overlapping data before, since my GET statement was:
GET #1, x, f1(x)
So that's why it didn't try to read beyond the end of the file.

To manipulate the pixel data I guess I'll have to use VAL(Pixel), then use CHR$(modified pixel data) to write the data back out to a file?

I use QBASIC because it's what I'm used to. Have tried to learn C++, but quickly gave up as I found it almost impenetrable!

LoRd_MuldeR
7th February 2012, 02:33
I use QBASIC because it's what I'm used to. Have tried to learn C++, but quickly gave up as I found it almost impenetrable!

I can only recommend to learn some C/C++, because you'll encounter it everywhere ;)

Anyway, if all you did before is old QBASIC, you maybe want to have a look at VisualBasic.NET or C# first:
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express

Alternatively Java (http://www.oracle.com/technetwork/java/javase/downloads/index.html) (preferably with Eclipse IDE (http://www.eclipse.org/downloads/packages/eclipse-ide-java-developers/indigosr1)) and Python (http://python.org/) are definitely worth a look...

alexh110
12th February 2012, 11:31
Why learn a new language when I can use this:
http://sourceforge.net/projects/qbasictoc/

:-)

LoRd_MuldeR
12th February 2012, 14:01
Why learn a new language when I can use this:
http://sourceforge.net/projects/qbasictoc/

If for no other reason, than at least for your own educational purpose :)

"Hardwork...Determination...I gotta keep pushing myself" (http://www.youtube.com/watch?v=_Z1rFzxPIz8)

amtm
13th February 2012, 17:25
Why learn a new language when I can use this:
http://sourceforge.net/projects/qbasictoc/

:-)

A hammer can also drive in screws. Doesn't mean it's the ideal tool for the job. ;)

jmac698
21st February 2012, 02:15
Use freebasic, it's qbasic compatible but compiled and as fast as C in some tests. Yep, I'm in the same boat, and I managed to compile my 10 year old programs.
And yes I had the get problem before, dim char as string 1 or something is the answer. There's an example image reader for freebasic.

DECLARE FUNCTION getchar! ()

PRINT "Enter the name of a Y8 file to convert:"
INPUT filename$
IF LEN(filename$) = 0 THEN END
IF LCASE$(RIGHT$(filename$, 3)) <> ".y8" THEN filename$ = filename$ + ".y8"
OPEN filename$ FOR INPUT AS #1

PRINT "Width?"
INPUT biWidth
PRINT "Height?"
INPUT biHeight

'get bitmap
FOR y = TO to biHeight
' PRINT y
FOR x = 0 TO biWidth - 1
t = getchar
NEXT x
NEXT y
CLOSE 1

FUNCTION getchar
t$ = INPUT$(1, 1)
getchar = ASC(t$ + CHR$(0))
END FUNCTION

alexh110
27th February 2012, 10:47
Thanks.
I've actually converted my QBASIC program into an AviSynth script now; but the freebasic compiler will definitely be useful for other QBASIC programs I've written in the past.