Branch: Tag:

2015-08-08

2015-08-08 19:27:52 by Martin Nilsson <nilsson@fastmail.com>

Some refactoring.

40:    return c;   }    - protected int(0..65535) read_2_bytes_intel(Stdio.File f) - { -  int c; -  sscanf( f->read(2), "%-2c", c); -  return c; - } +     -  +    /*    * Read the initial marker, which should be SOI.    * For a JFIF file, the first two bytes of the file should be literally
332:    if(stringp(file))    file = Stdio.FakeFile(file);    -  switch(file->read(6)) { -  -  case "GIF87a": -  case "GIF89a": +  array ret; +  switch(file->read(2)) +  { +  case "GI": +  if( (< "F87a", "F89a" >)[file->read(4)] )    return array_sscanf(file->read(4), "%-2c%-2c") + ({ "gif" }); -  +  break;    -  case "\x89PNG\r\n": -  file->read(6+4); // offset+IHDR +  case "\x89P": +  if(file->read(4)=="NG\r\n") +  { +  file->read(6+4);    return array_sscanf(file->read(8), "%4c%4c") + ({ "png" }); -  +  } +  break;    -  case "8BPS\0\1": -  // Photoshop PSD -  // -  // 4 bytes signature + 2 bytes version -  // 6 bytes reserved -  // 2 bytes channel count -  file->read(6 + 2); -  -  // 4 bytes height, 4 bytes width (big-endian) +  case "8B": +  if(file->read(4)=="PS\0\1") +  { +  file->read(6+2);    return reverse(array_sscanf(file->read(8), "%4c%4c")) + ({ "psd" }); -  +  } +  break;    -  default: -  string buf; -  array ret; -  file->seek(file->tell()-6); -  buf = file->read(2); +  case "II": +  case "MM":    file->seek(file->tell()-2); -  if(buf == "II" || buf== "MM") -  { +     ret = get_TIFF(file);    if(ret) return ret+({ "tiff" }); -  +  break;    -  } -  else -  { +  case "\xff\xd8": +  file->seek(file->tell()-2);    ret = get_JPEG(file);    if(ret) return ret+({ "jpeg" }); -  +  break;    }    -  return 0; +    } - } +