95 lines · 3.8 KB · Text
RawDownload
= imagefile: readgif, readjpg, readpicfile, readpng, readxbitmap, remap: processing external image file formats     include "imagefile.m";     gifreader  := load RImagefile RImagefile->READGIFPATH;    jpgreader  := load RImagefile RImagefile->READJPGPATH;    xbmreader  := load RImagefile RImagefile->READXBMPATH;    picreader  := load RImagefile RImagefile->READPICPATH;    pngreader := load RImagefile RImagefile->READPNGPATH;     imageremap := load Imageremap Imageremap->PATH;     Rawimage: adt    {        r:         Draw->Rect;        cmap:      array of byte;        nchans:    int;        chans:     array of array of byte;        chandesc:  int;            init:      fn(bufio: Bufio);        read:      fn(fd: ref Bufio->Iobuf): (ref Rawimage, string);        readmulti: fn(fd: ref Bufio->Iobuf):                    (array of ref Rawimage, string);    };        init:          fn(bufio: Bufio);    writeimage:    fn(fd: ref Bufio->Iobuf, image: ref Draw->Image)                                    : string;        remap:         fn(i: ref RImagefile->Rawimage, d: ref Draw->Display,                    errdiff: int): (ref Draw->Image, string); The Rawimage adt of module  RImagefile  defines  an  internalrepresentation  and  routines  for reading images such as GIFand JPEG files.  To read a set of files of  a  given  format,load the appropriate module, pass its init function an imple‐mentation of the Bufio module, and pass read an  Bufio->Iobuffor  each  file.   Read  returns a tuple: a ref Rawimage thatholds the image and an error string.  If the Rawimage is nil,the error string will report the reason.  Files (particularlyGIF files) are often incorrectly  encoded  but  yield  usablepictures, so even if a Rawimage is returned, the error stringmay still report problems. Some image file types (eg, GIF) support having several imagesin  a  single  file.   They  can be read in all at once usingreadmulti, which returns a tuple with the  array  of  images,and an error string as above. The  Rawimage  is  always  defined  as  one or more bytes perpixel, with nchans channels  of  data  stored  in  the  arraychans.   The  chandesc  field, described below, specifies thecontents of chans.  The rectangle r describes  the  shape  ofthe picture. The  Rawimage  type  can be converted to a regular Image (seedraw-image(2)) by calling module Imageremap's function remap.Remap  is  passed  the Rawimage, a Display on which to createthe image, and a flag that specifies whether to apply  Floyd-Steinberg  error  diffusion  code to the result, for smootherrendering of colours at the cost of some noise in the image. How to remap is defined by the RImagefile itself:  the  fieldchandesc  specifies  the  type  of the various chans of data:RImagefile->CRGB specifies  a  3-colour  RGB  image  with  nocolour  map; RImagefile->CY a monotone (luminance-only, grey-scale) image with no colour map; and RImagefile->CRGB1 a sin‐gle-channel  image  with  RGB  colour  map in cmap.  The filereaders set chandesc as appropriate for  the  format  of  thefile. The  writing  of image files is defined by the module WImage‐file.  To initialize the module, call its init function  withan instance of the Bufio module and pass its writeimage func‐tion a Bufio->Iobuf representing the  output  stream  and  animage of type Draw->Image. These  functions  are  split  into  separate  modules to giveapplications control over the memory  they  need  to  processimages. == source/appl/lib/readgif.b/appl/lib/readjpg.b/appl/lib/readxbitmap.b/appl/lib/readpicfile.b/appl/lib/readpng.b == notesThe  JPEG  reader handles only the Baseline sequential formatas defined by the JFIF 1.02 file exchange format. Functions to write these formats are as yet unimplemented.