Read Sony Mavica preview images
Done- here's how it works internally:
You pass it a filename, vref, and x, y, coordinates where you want it to display. It checks the file size to see if it matches a certain length. If so, it creates a pointer the size of the file and pops the contents of the file in there.(The files are a little bit bigger than 4k) Then it traverses the file byte by byte, as if it were reading it from the file itself. It basically does the same thing as reading a file byte by byte, only when you load it into RAM, it's much faster.
So then it does it's little calculating routine, and makes the R,G,B values from them. Then I turn these colors into "real" RGB colors(Rick Brown taught me how to do this a long time back), then I plot the point, and increment the X%,Y% coordinates. (And do a little cleanup afterwards)
Best part is that you can make it an include file if you so wish- no globals!
Ok, here's the program to decode Mavica files:
'Mavica 411 decoder for Mel Patrick 'By TJ Grant(email@example.com) COMPILE 0,_dimmedVarsOnly _strResource END GLOBALS GOTO "Mavica:End" LOCAL FN loadFile(fName$,vRef%,hndl&) DIM sz& DIM ex& DIM Oserr% OPEN "ID",#1,fName$,1,vRef% sz& = LOF(1,1) LONG IF sz&<>0 LONG IF hndl& READ FILE #1,hndl&,sz& END IF END IF CLOSE #1 END FN LOCAL FN getNextByte(currentPointer&) DIM byte% byte% = PEEK(currentPointer&) END FN = byte% LOCAL FN MavicaDecode(file1$, vref1%,x%,y%) DIM sz1& DIM hndl1& DIM counter1& DIM Oserr1% DIM K%, J%, I% DIM y%(4), cb%, cr% DIM r%, g%, b% DIM myRGB.6 DIM pixelX% DIM pixelY% DIM result% LONG IF file1$ = "" GOTO "Can't Decode" END IF OPEN "ID",#1,file1$,1,vref1% sz1& = LOF(1,1) 'get size of file CLOSE #1 'Size of the data fork must be width*height + extra ' 64x48 + 1536 extra bytes = 4608 IF sz1& <> 4608 THEN GOTO "Can't Decode" 'Create the pointer to hold the file hndl1& = FN NEWPTR _clear(sz1&) 'Load file1$ into hndl1& FN loadFile(file1$,vref1%,hndl1&) counter1& = hndl1& pixelX% = x% pixelY% = y% FOR K% = 0 TO 47 STEP 1 'Height FOR J% = 0 TO 15 STEP 1 'Width/4 y%(0) = FN getNextByte(counter1&) :INC(counter1&) y%(1) = FN getNextByte(counter1&) :INC(counter1&) y%(2) = FN getNextByte(counter1&) :INC(counter1&) y%(3) = FN getNextByte(counter1&) :INC(counter1&) cb% = FN getNextByte(counter1&) - 128 :INC(counter1&) cr% = FN getNextByte(counter1&) - 128 :INC(counter1&) FOR I% = 0 TO 3 STEP 1 r% = y%(I%) + 1.40200 * cr% g% = y%(I%) - 0.34414 * cb% - 0.71414 * cr% b% = y%(I%) + 1.77200 * cb% IF r%<0 THEN r% = 0 IF g%<0 THEN g% = 0 IF b%<0 THEN b% = 0 IF r%>255 THEN r% = 255 IF g%>255 THEN g% = 255 IF b%>255 THEN b% = 255 myRGB.red% = r%*255+r% myRGB.green% = g%*255+g% myRGB.blue% = b%*255+b% CALL RGBFORECOLOR(#@myRGB) CALL MOVETO(pixelX%,pixelY%) CALL LINETO(pixelX%,pixelY%) pixelX% = pixelX% + 1 LONG IF pixelX% = 64 + x% pixelY% = pixelY% + 1 pixelX% = x% END IF NEXT I% NEXT J% NEXT K% 'Dispose of the pointer Oserr1% = FN DISPOSPTR(hndl1&) result% = _true GOTO "Done Decoding" "Can't Decode" result% = _false "Done Decoding" REM Nothin' END FN LOCAL FN daemo DIM xFile$,vref% xFile$ = FILES$(_fOpen,,,vref%) WINDOW #1,"Mavica!",(0,0)-(320,200),_dialogPlain LONG IF FN MavicaDecode(xFile$, vref%,0,0) 'yea! decoded ok! XELSE 'better luck next time BEEP END IF DO UNTIL FN BUTTON WINDOW CLOSE #1 FLUSHEVENTS END END FN "Mavica:End" 'Cut this next line out to make it an include FN daemo IF 0 THEN RETURN