home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / programs / amiga / pastex / pastex14-beta-6 / specialhost / source / readimage.c < prev    next >
C/C++ Source or Header  |  1994-05-27  |  48KB  |  2,275 lines

  1. /*
  2. **    SpecialHost for PasTeX
  3. **
  4. **    Copyright © by Olaf Barthel & Georg Heßmann
  5. */
  6.  
  7. #include "Global.h"
  8.  
  9. enum    {    CLUT_MONO,CLUT_RED,CLUT_GREEN,CLUT_BLUE,CLUT_HUE,CLUT_SAT };
  10.  
  11. #define ID_CLUT            MAKE_ID('C','L','U','T')
  12. #define ID_DPI            MAKE_ID('D','P','I',' ')
  13.  
  14. #define CACHE_SIZE        16384
  15.  
  16. struct DPIHeader
  17. {
  18.     UWORD            DPI_X,
  19.                 DPI_Y;
  20. };
  21.  
  22. struct CLUT
  23. {
  24.     ULONG            Type;
  25.     ULONG            Reserved;
  26.     UBYTE            LUT[256];
  27. };
  28.  
  29. struct BufferHandle
  30. {
  31.     struct IFFHandle    *Handle;
  32.     UBYTE            *Index,
  33.                  Buffer[CACHE_SIZE];
  34.     LONG             Length;
  35.     BOOL             FromClip;
  36. };
  37.  
  38. struct PictureInfo
  39. {
  40.     struct BitMapHeader     BitMapHeader;
  41.     ULONG            *ColourMap,
  42.                  ViewModes;
  43.     LONG             NumColours;
  44.     struct CLUT        *CLUT[CLUT_HUE];
  45.     struct BitMap        *BitMap[3];
  46.     UBYTE            *TempLine[3];
  47.     struct GreyImage    *GreyImage;
  48.     LONG            *Error;
  49.     struct RastPort        *TempRPort,
  50.                  DummyRPort;
  51.     BOOL             GotCLUT;
  52. };
  53.  
  54. extern LONG                HookEntry();
  55.  
  56. STATIC BOOL __regargs            GetClipID(STRPTR Name,LONG *ID);
  57. STATIC LONG __saveds            StreamHandler(struct Hook *Hook,struct IFFHandle *Handle,struct IFFStreamCmd *ActionPkt);
  58.  
  59. STATIC LONG __regargs            BufferFill(struct BufferHandle *Handle);
  60. STATIC LONG __regargs            BufferGet(struct BufferHandle *Handle);
  61. STATIC LONG __regargs            BufferRead(struct BufferHandle *Handle,UBYTE *Buffer,LONG Size);
  62. STATIC LONG __regargs            BufferSeek(struct BufferHandle *Handle,LONG Size);
  63. STATIC VOID __regargs            BufferClose(struct BufferHandle *Handle);
  64. STATIC struct BufferHandle * __regargs    BufferOpen(STRPTR Name);
  65.  
  66. STATIC struct GreyImage * __regargs    CreateImage(LONG Width,LONG Height);
  67. STATIC UBYTE * __regargs        AddLine(struct GreyImage *Image);
  68. STATIC struct BufferHandle * __regargs    ReadImageHeader(STRPTR Name,BOOL PatchColour,struct PictureInfo *PictureInfo);
  69. STATIC VOID __regargs            ReadHAM6Data(struct BufferHandle *Handle,struct PictureInfo *PictureInfo);
  70. STATIC VOID __regargs            ReadHAM8Data(struct BufferHandle *Handle,struct PictureInfo *PictureInfo);
  71. STATIC VOID __regargs            ReadVanillaData(struct BufferHandle *Handle,struct PictureInfo *PictureInfo);
  72. STATIC VOID __regargs            ReadDeepData(struct BufferHandle *Handle,struct PictureInfo *PictureInfo);
  73. STATIC VOID __regargs            ConvertHAM6Data(struct PictureInfo *PictureInfo);
  74. STATIC VOID __regargs            ConvertHAM8Data(struct PictureInfo *PictureInfo);
  75. STATIC VOID __regargs            ConvertVanillaData(struct PictureInfo *PictureInfo);
  76. STATIC struct GreyImage * __regargs    ReadPicture(STRPTR Name,BOOL PatchColours,LONG *Error);
  77. STATIC struct GreyImage * __regargs    ReadILBM(STRPTR Name,BOOL PatchColours,LONG *Error);
  78. STATIC BOOL __regargs            GetPictureSize(STRPTR Name,LONG *Width,LONG *Height,LONG *Error);
  79. STATIC BOOL __regargs            GetILBMSize(STRPTR Name,LONG *Width,LONG *Height,LONG *Error);
  80. STATIC BOOL __regargs            GetPicture_DPI(STRPTR Name,LONG *DPI_X,LONG *DPI_Y,LONG *Error);
  81. STATIC BOOL __regargs            GetILBM_DPI(STRPTR Name,LONG *DPI_X,LONG *DPI_Y,LONG *Error);
  82.  
  83. STATIC struct Hook StreamHook =
  84. {
  85.     {NULL},
  86.     (ULONG (*)())HookEntry,
  87.     (ULONG (*)())StreamHandler,
  88.     NULL
  89. };
  90.  
  91. STATIC BOOL __regargs
  92. GetClipID(STRPTR Name,LONG *ID)
  93. {
  94.     if(!Strnicmp(Name,"CLIPBOARD:",10))
  95.     {
  96.         if(ID)
  97.             StrToLong(Name + 10,ID);
  98.  
  99.         return(TRUE);
  100.     }
  101.     else
  102.         return(FALSE);
  103. }
  104.  
  105. STATIC LONG __saveds
  106. StreamHandler(struct Hook *Hook,struct IFFHandle *Handle,struct IFFStreamCmd *ActionPkt)
  107. {
  108.     struct AsyncFile    *Stream    = (struct AsyncFile *)Handle -> iff_Stream;
  109.     LONG             Bytes    = ActionPkt -> sc_NBytes;
  110.  
  111.     switch(ActionPkt -> sc_Command)
  112.     {
  113.         case IFFCMD_READ:
  114.  
  115.             return(ReadAsync(Stream,ActionPkt -> sc_Buf,Bytes) != Bytes);
  116.  
  117.         case IFFCMD_WRITE:
  118.  
  119.             return(WriteAsync(Stream,ActionPkt -> sc_Buf,Bytes) != Bytes);
  120.  
  121.         case IFFCMD_SEEK:
  122.  
  123.             return(SeekAsync(Stream,Bytes,MODE_CURRENT) == -1);
  124.  
  125.         default:
  126.  
  127.             return(0);
  128.     }
  129. }
  130.  
  131. STATIC LONG __regargs
  132. BufferFill(struct BufferHandle *Handle)
  133. {
  134.     Handle -> Index = Handle -> Buffer;
  135.  
  136.     return(Handle -> Length = ReadChunkBytes(Handle -> Handle,Handle -> Buffer,CACHE_SIZE));
  137. }
  138.  
  139. STATIC LONG __regargs
  140. BufferGet(struct BufferHandle *Handle)
  141. {
  142.     if(!Handle -> Length)
  143.     {
  144.         if(BufferFill(Handle) < 1)
  145.             return(-1);
  146.     }
  147.  
  148.     Handle -> Length--;
  149.  
  150.     return(*Handle -> Index++);
  151. }
  152.  
  153. STATIC LONG __regargs
  154. BufferRead(struct BufferHandle *Handle,UBYTE *Buffer,LONG Size)
  155. {
  156.     LONG BytesRead = 0,Count;
  157.  
  158.     while(Size)
  159.     {
  160.         if(!Handle -> Length)
  161.         {
  162.             if(BufferFill(Handle) < 1)
  163.                 return(BytesRead);
  164.         }
  165.  
  166.         if(Size > Handle -> Length)
  167.             Count = Handle -> Length;
  168.         else
  169.             Count = Size;
  170.  
  171.         CopyMem(Handle -> Index,Buffer,Count);
  172.  
  173.         BytesRead        += Count;
  174.         Size            -= Count;
  175.         Buffer            += Count;
  176.         Handle -> Index        += Count;
  177.         Handle -> Length    -= Count;
  178.     }
  179.  
  180.     return(BytesRead);
  181. }
  182.  
  183. STATIC LONG __regargs
  184. BufferSeek(struct BufferHandle *Handle,LONG Size)
  185. {
  186.     LONG BytesRead = 0,Count;
  187.  
  188.     while(Size)
  189.     {
  190.         if(!Handle -> Length)
  191.         {
  192.             if(BufferFill(Handle) < 1)
  193.                 return(BytesRead);
  194.         }
  195.  
  196.         if(Size > Handle -> Length)
  197.             Count = Handle -> Length;
  198.         else
  199.             Count = Size;
  200.  
  201.         BytesRead        += Count;
  202.         Size            -= Count;
  203.         Handle -> Index        += Count;
  204.         Handle -> Length    -= Count;
  205.     }
  206.  
  207.     return(BytesRead);
  208. }
  209.  
  210. STATIC VOID __regargs
  211. BufferClose(struct BufferHandle *Handle)
  212. {
  213.     CloseIFF(Handle -> Handle);
  214.  
  215.     if(Handle -> FromClip)
  216.         CloseClipboard((struct ClipboardHandle *)Handle -> Handle -> iff_Stream);
  217.     else
  218.         CloseAsync((struct AsyncFile *)Handle -> Handle -> iff_Stream);
  219.  
  220.     FreeIFF(Handle -> Handle);
  221.  
  222.     FreeVecPooled(Handle);
  223. }
  224.  
  225. STATIC struct BufferHandle * __regargs
  226. BufferOpen(STRPTR Name)
  227. {
  228.     struct BufferHandle    *Handle;
  229.     LONG             Error;
  230.  
  231.     if(Handle = (struct BufferHandle *)AllocVecPooled(sizeof(struct BufferHandle),MEMF_ANY | MEMF_CLEAR))
  232.     {
  233.         if(Handle -> Handle = AllocIFF())
  234.         {
  235.             LONG ClipID;
  236.  
  237.             if(GetClipID(Name,&ClipID))
  238.             {
  239.                 if(Handle -> Handle -> iff_Stream = (ULONG)OpenClipboard(ClipID))
  240.                 {
  241.                     InitIFFasClip(Handle -> Handle);
  242.  
  243.                     if(!(Error = OpenIFF(Handle -> Handle,IFFF_READ)))
  244.                     {
  245.                         Handle -> Length    = 0;
  246.                         Handle -> FromClip    = TRUE;
  247.  
  248.                         return(Handle);
  249.                     }
  250.  
  251.                     CloseClipboard((struct ClipboardHandle *)Handle -> Handle -> iff_Stream);
  252.                 }
  253.                 else
  254.                     Error = IoErr();
  255.             }
  256.             else
  257.             {
  258.                 if(Handle -> Handle -> iff_Stream = (ULONG)OpenAsync(Name,MODE_READ,2 * CACHE_SIZE))
  259.                 {
  260.                     InitIFF(Handle -> Handle,IFFF_FSEEK | IFFF_RSEEK,&StreamHook);
  261.  
  262.                     if(!(Error = OpenIFF(Handle -> Handle,IFFF_READ)))
  263.                     {
  264.                         Handle -> Length    = 0;
  265.                         Handle -> FromClip    = FALSE;
  266.  
  267.                         return(Handle);
  268.                     }
  269.  
  270.                     CloseAsync((struct AsyncFile *)Handle -> Handle -> iff_Stream);
  271.                 }
  272.                 else
  273.                     Error = IoErr();
  274.             }
  275.  
  276.             FreeIFF(Handle -> Handle);
  277.         }
  278.         else
  279.             Error = ERROR_NO_FREE_STORE;
  280.  
  281.         FreeVecPooled(Handle);
  282.     }
  283.     else
  284.         Error = ERROR_NO_FREE_STORE;
  285.  
  286.     if(Error)
  287.         SetIoErr(Error);
  288.  
  289.     return(NULL);
  290. }
  291.  
  292. STATIC struct GreyImage * __regargs
  293. CreateImage(LONG Width,LONG Height)
  294. {
  295.     struct GreyImage *Image;
  296.  
  297.     if(Image = (struct GreyImage *)AllocVecPooled(sizeof(struct GreyImage) + sizeof(UBYTE *) * Height,MEMF_ANY | MEMF_CLEAR))
  298.     {
  299.         LONG Size = (Width < 16384 ? 16384 : Width);
  300.  
  301.         Image -> Width    = Width;
  302.         Image -> Height    = Height;
  303.         Image -> Lines    = (UBYTE **)(Image + 1);
  304.  
  305.         if(!(Image -> Pool = LibCreatePool(MEMF_ANY,Size,Size)))
  306.         {
  307.             FreeVecPooled(Image);
  308.  
  309.             return(NULL);
  310.         }
  311.     }
  312.  
  313.     return(Image);
  314. }
  315.  
  316. STATIC UBYTE * __regargs
  317. AddLine(struct GreyImage *Image)
  318. {
  319.     if(Image -> Index < Image -> Height)
  320.     {
  321.         UBYTE *Line;
  322.  
  323.         if(Line = (UBYTE *)LibAllocPooled(Image -> Pool,(Image -> Width + 3) & ~3))
  324.             Image -> Lines[Image -> Index++] = Line;
  325.  
  326.         return(Line);
  327.     }
  328.     else
  329.         return(NULL);
  330. }
  331.  
  332. STATIC struct BufferHandle * __regargs
  333. ReadImageHeader(STRPTR Name,BOOL PatchColours,struct PictureInfo *PictureInfo)
  334. {
  335.     struct BufferHandle *Handle;
  336.  
  337.     *PictureInfo -> Error = 0;
  338.  
  339.     if(Handle = BufferOpen(Name))
  340.     {
  341.         STATIC LONG Stops[] =
  342.         {
  343.             ID_ILBM,ID_BMHD,
  344.             ID_ILBM,ID_CMAP,
  345.             ID_ILBM,ID_CAMG,
  346.             ID_ILBM,ID_CLUT,
  347.             ID_ILBM,ID_BODY
  348.         };
  349.  
  350.         LONG Error;
  351.  
  352.         if(!(Error = StopChunks(Handle -> Handle,Stops,5)))
  353.         {
  354.             struct ContextNode    *Chunk;
  355.             BOOL             GotBody    = FALSE,
  356.                          Terminated    = FALSE;
  357.             struct CLUT         LocalCLUT;
  358.  
  359.             while(!Terminated && !(Error = ParseIFF(Handle -> Handle,IFFPARSE_SCAN)))
  360.             {
  361.                 Chunk = CurrentChunk(Handle -> Handle);
  362.  
  363.                 switch(Chunk -> cn_ID)
  364.                 {
  365.                     case ID_BMHD:
  366.  
  367.                         if(ReadChunkByte