Shop OBEX P1 Docs P2 Docs Learn Events
Saving image from PropCam to a BMP-file on SD-card — Parallax Forums

Saving image from PropCam to a BMP-file on SD-card

banjobanjo Posts: 447
edited 2015-05-05 22:43 in Propeller 1
In Spin I'd like to save an acquired image form ProCam as a BMP-file to a SD-card.
So far I've accomplished to save data to a file with the 'SD-MMC_FATEngine' object.
What I'd need some help with now is the BMP-header for the image file. The relevant code I've tried with is below but that is not resulting in a functional BMP-file. By using a HEX-editor and copying from another 4-bit file I've been able to turn the PropCam image into a functional BMP-file but the grayscales are incorrect.

A few questions:
1) With a grayscale image, do I need a color table in the header? In that case, what color table to use for PropCam?
2) After I have a correct header, do I need to pad the file in the end so the total file size would be a factor of e.g. 16?
3) Anything else I'm misunderstanding or should think of? This is my first dive into Spin so still learning...
PRI monitor(gray_buf_addr, hold_addr) | ch, text

  fat0.fatEngineStart( _dopin, _clkpin, _dipin, _cspin, _wppin, _cdpin, { } _rtcres1, _rtcres2, _rtcres3)
  fat0.mountPartition(0)
  fat0.deleteEntry(string("Image2.bmp"))
  fat0.newFile(string("Image2.bmp"))
  fat0.openFile(string("Image2.bmp"), "W")

  'First, write the header 
  biWidth:=128
  biHeight:=96
  biWidth*biHeight*3*4+54
  fat0.writeData(@bfType,2)
  fat0.writeData(@bfSize,4)   
  fat0.writeData(@bfReserved1,2)   
  fat0.writeData(@bfReserved2,2)   
  fat0.writeData(@bfOffBits,4)   
  fat0.writeData(@biSize,4)   
  fat0.writeData(@biWidth,4)   
  fat0.writeData(@biHeight,4)   
  fat0.writeData(@biPlanes,2)   
  fat0.writeData(@biBitCount,2)   
  fat0.writeData(@biCompression,4)   
  fat0.writeData(@biSizeImage,4)   
  fat0.writeData(@biXPelsPerMeter,4)   
  fat0.writeData(@biYPelsPerMeter,4)
  fat0.writeData(@biClrUsed,4)   
  fat0.writeData(@biClrImportant,4)

  'Writing pixel data to file
  repeat ch from 4 to 6147  
    fat0.writeByte(byte[gray_buf_addr][ch])

  fat0.unmountPartition

  waitcnt(clkfreq + cnt)
  statusLED(4) ' Make the status LED blink quickly.
  fat0.fatEngineStop ' Give the block driver a second to finish up.

'' This is the original code that is sending the image to a browser session initiated with PropCam_server.exe
  sio.start(31, 30, 0, 115200)
  repeat
    if ((ch := sio.rxcheck) => 0)
      if (ch == "I")
        sio.str(string("PropCAM"))
      elseif (ch == "S")
        if (hold_addr)
          byte[hold_addr]~~

        repeat ch from 4 to 6147
          sio.tx(byte[gray_buf_addr][ch])
        
        if (hold_addr)
          byte[hold_addr]~

DAT
BMPHeader  'Mostly using info from here:  http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
bfType byte "B","M"  ' 19778  
bfSize long 0
bfReserved1 word 0
bfReserved2 word 0
bfOffBits long 54
biSize long 40
biWidth long 0
biHeight long 0
biPlanes word 1
biBitCount word 4
biCompression long 0
biSizeImage long 0
biXPelsPerMeter long 0
biYPelsPerMeter long 0
biClrUsed long 0
biClrImportant long 0    

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-05-04 11:13
    This zip contains code to turn a 6-bit PropCAM image into a BMP file.

    -Phil
  • banjobanjo Posts: 447
    edited 2015-05-04 11:59
    This zip contains code to turn a 6-bit PropCAM image into a BMP file.

    -Phil

    Thx, I'm trying to understand this but not really getting it all
    With the 4-bit 128x96 image, is PIXELS_PER_ROW then 96? What about ROWS_PER_FRAME?
    ...
      srv.array(bmp.header(cam#PIXELS_PER_ROW, cam#ROWS_PER_FRAME, cam#BITS_PER_PIXEL, 0), bmp.header_size)
    ...
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-05-04 15:01
    PIXELS_PER_ROW = 128
    ROWS_PER_FRAME= 96
    BITS_PER_PIXEL = 4 or 6, depending upon which driver you're using.

    -Phil
  • banjobanjo Posts: 447
    edited 2015-05-04 21:49
    Got it, will try with these tonight and revert if I have more questions (as said, new to Spin...).
    Great that this is also supporting 6 bit images if I'd like to have more gray scale values
  • banjobanjo Posts: 447
    edited 2015-05-05 22:43
    Tried with Phil's code but without success as I got all the http 'stuff' as well in the header. Tried to get rid of it but got into further issues.
    I was however able to hard code header data so now I'm at least able to save a BMP-file to the SD-card. The resolution however seems to be lower than in the PropWebcam_demo so I need to check my header once again.
Sign In or Register to comment.