Storing Data in Images Part 2

Post Reply
User avatar
rbytes
Posts: 1798
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Storing Data in Images Part 2

Post by rbytes » Sat Sep 08, 2018 2:12 pm

Here is the code to store a file as a pixeldata image.

Code: Select all

/*Pixel Data Store V2 by rbytes, September 2018
based on Pixel Party by Henko, October 2016
Reads a file and stores its data
within the color pixels of a PNG image.
Use program Pixel Data Read V2 to read the data
from the PNG and save a clone of the original file.

V2. Added the feature of writing the filesize
and output filename within the PNG data image.
Also the filename is now printed at the bottom of the
data image.
*/

set toolbar off
get screen size sw,sh
dim cl(4)
rw=sw/1024 ! rh=sh/768
graphics


' load a program file into array M

V$="IMG_5808.GIF"
FILE V$ READDIM M, N


' create the buttons

set buttons font size 18
button "1" text k at 40*rw,200*rh size 120,40
button "stop" text "X" at sw-50,20 size 40,40


' store the file size and filename on line 0
' 20 characters for the file size, 80 characters for the filename

space$="                                         "
space$&="                                         "
tekst$=STR$(N)&left$(space$,20-LEN(STR$(N)))
tekst$&=V$&left$(space$,100-LEN(V$))


' write the file size and name data to the screen pixels

lt=99 ! xpix=100 ! k=0 ! xo=0 ! x=xo ! yo=0 ! y=yo
while k<lt
  for i=1 to xpix
    for j=1 to 3
      if k<=lt then cl(j)=asc(mid$(tekst$,k,1))
      k+=1
      next j
    draw_pixel(x,y,cl)
    if k>lt then break
    x+=1
    if x-xo>xpix then
        x=xo ! y+=1
        button "1" text k
        if button_pressed("stop") then end
    end if
    next i
  end while

lt=N-1 ! xpix=1000


' write the file data to the screen pixels

k=0 ! xo=0 ! x=xo ! yo=1 ! y=yo
while k<lt
  for i=1 to xpix
    for j=1 to 3
      if k<=lt then cl(j)=m(k)
      k+=1
      next j
    draw_pixel(x,y,cl)
    if k>lt then break
    x+=1
    if x-xo>xpix then
       x=xo ! y+=1
       button "1" text k
       if button_pressed("stop") then end
    end if
    next i
   
  end while


' print a label on the data image

draw text V$ at 180,380


' save the data image

GRAPHICS SAVE 0,0,sw/2+2,400 to "pixdata.png"


' export the data image (you may want to comment out this line)

album export "pixdata.png"

end



' draw a pixel using values 0-255 for r,g,b and 1 for alpha
'
def draw_pixel(x,y,cl())
r=cl(1)/255 ! g=cl(2)/255 ! b=cl(3)/255 ! a=1    ' alpha value must always be 1!
draw pixel x,y color r,g,b,a
end def

' get pixel values 0-255 for r,g,b in array cl(1) - cl(3)
'
def get_pixel(x,y,cl())
get pixel x,y color cl(1),cl(2),cl(3)
for i=1 to 3 ! cl(i)=floor(cl(i)*255) ! next i
end def
####### Living the colorful life #######

Post Reply