QB / QB64 Discussion Forum     RULES     Other Subforums, Links and Downloads    Index of Threads

 

 Return to Index  

Scanning a sprite and compressing to a CSV file or DATA field.

July 31 2009 at 10:10 PM
Clippy  (Login burger2227)
R


Response to Clipster

 
The following SUB scans an image area with POINT and compesses repeated pixel attributes into count and color file data. Actually creates or appends to a file! See below for how to translate the data back to the screen later:

'If Dtype$ = "C" then it creates a CSV data file exactly like WRITE would do.
'If Dtype$ = "D" it will create a DATA field that can be placed directly into a BAS file.

'Be sure to enter the correct image coodinates and Dtype$ = "C" or "D"!

SUB CSVData (Filename$, Dtype$, MinX, MinY, MaxX, MaxY)
x = MinX '(default = 0)
y = MinY '(default = 0)

OPEN filename$ FOR APPEND AS #1 'DATA fields need append for BAS files.
IF Dtype$ = "D" THEN
PRINT #1, : PRINT #1, : PRINT #1, "' " + TIME$ + ": " + DATE$
PRINT #1, "NewData: 'use a DATA fieldname to RESTORE fieldname"
END IF
'place the 4 minimum and maximum image coordinates to first line of data if needed later.
IF Dtype$ = "D" THEN PRINT #1, "DATA ";
PRINT #1, "LTRIM$(STR$(MinX)) + "," + LTRIM$(STR$(MinY)) + "," + LTRIM$(STR$(MaxX)) + "," + LTRIM$(STR$(MaxY))

D = 0
DO: attr = POINT(x, y) ' scan image pixels
IF Patt = attr THEN
Count = Count + 1
ELSE
IF Count THEN 'delay first attribute data write one loop
IF D MOD 30 = 0 THEN
IF D THEN PRINT #1, "" 'moves to next print row
IF Dtype$ = "D" THEN PRINT #1, "DATA ";
PRINT #1, LTRIM$(STR$(Count)) + "," + LTRIM$(STR$(Patt));
ELSE PRINT #1, "," + LTRIM$(STR$(Count)) + "," + LTRIM$(STR$(Patt));
END IF
D = D + 2
END IF
Count = 1
END IF
Patt = attr 'previous attribute
IF x < MaxX THEN x = x + 1 ELSE x = MinX: y = y + 1
LOOP UNTIL y > MaxY
IF D MOD 30 = 0 THEN 'start a new data line every 30
PRINT #1, ""
IF Dtype$ = "D" THEN PRINT #1, "DATA ";
PRINT #1, LTRIM$(STR$(Count)) + "," + LTRIM$(STR$(Patt))
ELSE PRINT #1, "," + LTRIM$(STR$(Count)) + "," + LTRIM$(STR$(Patt))
END IF
IF Dtype$ = "D" THEN PRINT #1, ""
CLOSE #1
END SUB

The above code can also be adapted for using a smaller sized array.
Calculation of array size is at least half using xpixels * ypixels \ 2.

'------------------------------------------------------------------
'HOW TO READ the DATA: adapted from a routine by Bob Seguin

SUB ReadData (Dtype$) ' assumes file is opened as filenumber #1

IF Dtype$ = "D" THEN READ MinX, MinY, MaxX, MaxY 'if DATA field used
IF Dtype$ = "C" THEN INPUT #1, MinX, MinY, MaxX, MaxY 'if CSV file is used
'otherwise just use your own adjusted coordinates. MUST be same pixel sizes as original!

x = MinX
y = MinY

DO
IF Dtype$ = "D" THEN READ Count, Colr 'for a DATA field READ
IF Dtype = "C" THEN INPUT #1, Count, Colr'for a CSV file INPUT read
FOR reps = 1 TO Count
PSET (x, y), Colr
IF x < MaxX THEN x = x + 1 ELSE x = MinX: y = y + 1
NEXT reps
IF Dtype$ = "C" AND EOF(1) THEN EXIT DO
LOOP UNTIL y > MaxY

END SUB

'can also be adapted to read data from an array, 2 integers at a time such as Array(D) and Array(D + 1).

Ted



    
This message has been edited by burger2227 on Sep 8, 2009 2:50 PM


 
 Respond to this message   
 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement