#pragma debug 0x8000#pragma optimize -1#pragma lint -1#include <gsos.h>#include <Memory.h>#include <texttool.h>#include <stdlib.h>#include <string.h>#include <gno/gno.h>extern int ConvertSHR(int, const char *);extern int orca_sprintf(char *, const char *, ...);char buffer[128];Word fDither;Word fSmall;void close(Word fd){Word closeDCB[2];  closeDCB[0] = 1;  closeDCB[1] = fd;  CloseGS(closeDCB);}long write(Word fd, const void *data, LongWord size){static IORecGS ioDCB = { 4, 0, 0, 0};  ioDCB.refNum = fd;  ioDCB.dataBuffer = (Pointer)data;  ioDCB.requestCount = size;  WriteGS(&ioDCB);  return _toolErr ? -1 : ioDCB.transferCount;}long read(Word fd, const void *data, LongWord size){static IORecGS ioDCB = { 4, 0, 0, 0};  ioDCB.refNum = fd;  ioDCB.dataBuffer = (Pointer)data;  ioDCB.requestCount = size;  ReadGS(&ioDCB);  return _toolErr ? -1 : ioDCB.transferCount;}#if 0long lseek(Word fd, LongWord offset, Word whence){static SetPositionRecGS markDCB = { 3, 0, 0, 0};static PositionRecGS positionDCB = {2, 0, 0};  markDCB.refNum = fd;  markDCB.base = whence;  markDCB.displacement = offset;  SetMarkGS(&markDCB);  if (_toolErr) return -1;  if (whence == startPlus) return offset;  positionDCB.refNum = fd;  GetMarkGS(&positionDCB);  return _toolErr ? -1 : positionDCB.position;}#endifGSString255Ptr c2gsmalloc(const char *str){GSString255Ptr g;word i;  if (!str) return NULL;  i = strlen(str);  g = (GSString255Ptr)malloc(i + 3);  g->length = i;  strcpy(g->text, str);  return g;}static OpenRecGS openDCB;static CreateRecGS createDCB;static NameRecGS destroyDCB;void main(int argc, char **argv){Word MyID;Handle h;char *cp;Word fd;Word err;Word i;  _beginStackCheck();  MyID = MMStartUp();  SetInputDevice(0, 3);  SetInGlobals(0xff, 0x80);  fSmall = 0;  fDither = 0;  argv++;  argc--;  while (argc)  {    if (stricmp("--dither", *argv) == 0)    {      fDither = 1;      argc--;      argv++;      continue;    }    if (stricmp("--small", *argv) == 0)    {      fSmall = 1;      argc--;      argv++;      continue;    }    break;  }  if (argc != 2)  {    WriteLine("\pPNG Floyd converts shr screen images to png format.");    WriteLine("\pUsage: pngfloyd [options] file.shr file.png");    WriteLine("\poptions");    WriteLine("\p  --dither   dither 640 mode colors");    WriteLine("\p  --small    create 320 x 200 image");    return;  }  openDCB.pCount = 15;  openDCB.pathname = c2gsmalloc(argv[0]);  openDCB.requestAccess = readEnable;  openDCB.resourceNumber = 0;  openDCB.optionList = NULL;  OpenGS(&openDCB);  err = _toolErr;  free(openDCB.pathname);  if (err)  {    WriteCString("Unable to open input file ");    WriteCString(argv[0]);    WriteLine("\p.");    return;  }  fd = openDCB.refNum;  if (openDCB.eof != 0x8000)  {    WriteLine("\pNot a SHR screen image!");    close(fd);  }  h = NewHandle(openDCB.eof, MyID, attrFixed | attrLocked | attrNoSpec, 0);  if (_toolErr)  {    WriteLine("\pMemory Allocation error.");    close(fd);    return;  }  cp = *h;  if (read(fd, cp, openDCB.eof) != openDCB.eof)  {    WriteLine("\pError reading input file.");    DisposeHandle(h);    close(fd);  }  close(fd);  destroyDCB.pCount = 1;  destroyDCB.pathname = c2gsmalloc(argv[1]);  createDCB.pathname = destroyDCB.pathname;  openDCB.pathname = destroyDCB.pathname;  DestroyGS(&destroyDCB);  // ignore any errors.  createDCB.pCount = 4;  createDCB.access = 0xe3;  createDCB.fileType = 0x0000;  createDCB.auxType = 0x0000;  CreateGS(&createDCB);  if (_toolErr)  {    WriteCString("Unable to create output file ");    WriteCString(argv[1]);    WriteLine("\p.");  }  else  {    openDCB.requestAccess = writeEnable;    OpenGS(&openDCB);    if (_toolErr)    {      WriteCString("Unable to open output file ");      WriteCString(argv[1]);      WriteLine("\p.");    }    else    {      fd = openDCB.refNum;      ConvertSHR(fd, cp);      close(fd);    }  }  DisposeHandle(h);  free(openDCB.pathname);  i = _endStackCheck();  write(2, buffer, orca_sprintf(buffer, "stack: %d\r", i));}
