--
--	rasterio: save and load rasters
--	Copyright (C) 2007  Adam Sampson <ats@offog.org>
--
--	This library is free software; you can redistribute it and/or
--	modify it under the terms of the GNU Lesser General Public
--	License as published by the Free Software Foundation, either
--	version 2 of the License, or (at your option) any later version.
--
--	This library is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--	Lesser General Public License for more details.
--
--	You should have received a copy of the GNU Lesser General Public
--	License along with this library.  If not, see
--	<http://www.gnu.org/licenses/>.
--

--** Support for saving and loading rasters as graphics files.
-- @module rasterio

#INCLUDE "course.module"
#INCLUDE "raster.module"

#IF DEFINED (HAVE.LIBPNG)
#USE "png.tce"
#ENDIF
#USE "ppmread.tce"
#USE "ppmwrite.tce"

--{{{  PROC read.raster
--* Load an image file as a raster, with the format determined automatically
-- based on the extension.
--
-- The following extensions are currently supported:
-- [@item] [@text .png] (if your occam system was built with PNG support)
-- [@item] [@text .ppm]
PROC read.raster (VAL []BYTE filename, RESULT RASTER raster)
  IF
    #IF DEFINED (HAVE.LIBPNG)
    ends.with.string (".png", filename)
      read.raster.png (filename, raster)
    #ENDIF
    ends.with.string (".ppm", filename)
      read.raster.ppm (filename, raster)
    TRUE
      CAUSEERROR ()
:
--}}}

--{{{  PROC write.raster
--* Write a raster to a file, with the format determined automatically based on
-- the extension.
--
-- The following extensions are currently supported:
-- [@item] [@text .png] (if your occam system was built with PNG support)
-- [@item] [@text .ppm]
PROC write.raster (VAL []BYTE filename, VAL [][]INT raster)
  IF
    #IF DEFINED (HAVE.LIBPNG)
    ends.with.string (".png", filename)
      write.raster.png (filename, raster)
    #ENDIF
    ends.with.string (".ppm", filename)
      write.raster.ppm (filename, raster)
    TRUE
      CAUSEERROR ()
:
--}}}

--{{{  PROC write.rasters
--* Write rasters to files.
--
-- As each raster passes through, it's written to a file named [@text
-- prefix000000suffix], with the number incrementing by one for each frame.
-- The files can be in any format supported by [@ref write.raster].
--
-- You can convert the resulting files into a video using [@text mencoder]
-- or a similar tool; see [@link
-- http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-enc-images.html the mencoder
-- manual] for more details.
PROC write.rasters (VAL []BYTE prefix, suffix, CHAN RASTER in?, out!)
  VAL INT lp IS SIZE prefix:
  VAL INT DIGITS IS 6:
  INITIAL MOBILE []BYTE name IS MOBILE [lp + (DIGITS + (SIZE suffix))]BYTE:
  SEQ
    [name FOR lp] := prefix
    [name FROM lp + DIGITS] := suffix
    INITIAL INT n IS 0:
    WHILE TRUE
      RASTER r:
      SEQ
        in ? r
        --{{{  fill in frame number
        INITIAL INT v IS n:
        SEQ i = 0 FOR DIGITS
          SEQ
            name[lp + ((DIGITS - 1) - i)] := '0' + (BYTE (v \ 10))
            v := v / 10
        --}}}
        write.raster (name, r)
        out ! r
        n := n + 1
:
--}}}
