home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / texmf / source / SpecHost / 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(ReadChunkBytes(Handle -> Handle,&PictureInfo -> BitMapHeader,sizeof(struct BitMapHeader)) != sizeof(struct BitMapHeader))
  368.                         {
  369.                             *PictureInfo -> Error = ERR_READ_ERROR;
  370.  
  371.                             Terminated = TRUE;
  372.                         }
  373.                         else
  374.                         {
  375.                             if(PictureInfo -> BitMapHeader . bmh_Pad & BMHDF_CMAPOK)
  376.                                 PatchColours = FALSE;
  377.  
  378.                             if(PictureInfo -> BitMapHeader . bmh_Depth == 6)
  379.                                 PictureInfo -> ViewModes = HAM;
  380.                             else
  381.                                 PictureInfo -> ViewModes = NULL;
  382.  
  383.                             if(PictureInfo -> BitMapHeader . bmh_Width > 320)
  384.                                 PictureInfo -> ViewModes |= HIRES;
  385.  
  386.                             switch(GfxBase -> DisplayFlags & (PAL | NTSC))
  387.                             {
  388.                                 case PAL:
  389.  
  390.                                     if(PictureInfo -> BitMapHeader . bmh_Height > 256)
  391.                                         PictureInfo -> ViewModes |= LACE;
  392.  
  393.                                     break;
  394.  
  395.                                 case NTSC:
  396.  
  397.                                     if(PictureInfo -> BitMapHeader . bmh_Height > 200)
  398.                                         PictureInfo -> ViewModes |= LACE;
  399.  
  400.                                     break;
  401.                             }
  402.  
  403.                             if(PictureInfo -> BitMapHeader . bmh_Compression > 1)
  404.                             {
  405.                                 *PictureInfo -> Error = ERR_WEIRD_COMPRESSION;
  406.  
  407.                                 Terminated = TRUE;
  408.                             }
  409.                         }
  410.  
  411.                         break;
  412.  
  413.                     case ID_CMAP:
  414.  
  415.                         if(PictureInfo -> NumColours = Chunk -> cn_Size / 3)
  416.                         {
  417.                             if(PictureInfo -> ColourMap = (ULONG *)AllocVecPooled(sizeof(ULONG) * PictureInfo -> NumColours,MEMF_ANY))
  418.                             {
  419.                                 ULONG    RGB;
  420.                                 WORD    i;
  421.  
  422.                                 for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  423.                                 {
  424.                                     if(ReadChunkBytes(Handle -> Handle,&RGB,3) != 3)
  425.                                     {
  426.                                         *PictureInfo -> Error = ERR_READ_ERROR;
  427.  
  428.                                         Terminated = TRUE;
  429.  
  430.                                         break;
  431.                                     }
  432.                                     else
  433.                                         PictureInfo -> ColourMap[i] = RGB >> 8;
  434.                                 }
  435.  
  436.                                 if(PatchColours)
  437.                                 {
  438.                                     BOOL Patch = TRUE;
  439.  
  440.                                     for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  441.                                     {
  442.                                         if(PictureInfo -> ColourMap[i] & 0x0F0F0F)
  443.                                         {
  444.                                             Patch = FALSE;
  445.  
  446.                                             break;
  447.                                         }
  448.                                     }
  449.  
  450.                                     if(Patch)
  451.                                     {
  452.                                         for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  453.                                             PictureInfo -> ColourMap[i] |= (PictureInfo -> ColourMap[i] >> 4);
  454.                                     }
  455.                                 }
  456.                             }
  457.                             else
  458.                             {
  459.                                 *PictureInfo -> Error = ERR_NO_MEM;
  460.  
  461.                                 Terminated = TRUE;
  462.                             }
  463.                         }
  464.  
  465.                         break;
  466.  
  467.                     case ID_CAMG:
  468.  
  469.                         if(ReadChunkBytes(Handle -> Handle,&PictureInfo -> ViewModes,sizeof(ULONG)) != sizeof(ULONG))
  470.                         {
  471.                             *PictureInfo -> Error = ERR_READ_ERROR;
  472.  
  473.                             Terminated = TRUE;
  474.                         }
  475.                         else
  476.                         {
  477.                             if(!(PictureInfo -> ViewModes & MONITOR_ID_MASK) || ((PictureInfo -> ViewModes & EXTENDED_MODE) && !(PictureInfo -> ViewModes & 0xFFFF0000)))
  478.                                 PictureInfo -> ViewModes &= ~(EXTENDED_MODE | SPRITES | VP_HIDE | GENLOCK_AUDIO | GENLOCK_VIDEO);
  479.                         }
  480.  
  481.                         break;
  482.  
  483.                     case ID_CLUT:
  484.  
  485.                         if(ReadChunkBytes(Handle -> Handle,&LocalCLUT,sizeof(struct CLUT)) != sizeof(struct CLUT))
  486.                             Terminated = TRUE;
  487.                         else
  488.                         {
  489.                             if(LocalCLUT . Type < CLUT_HUE)
  490.                             {
  491.                                 BOOL Blank = TRUE;
  492.                                 WORD i;
  493.  
  494.                                 for(i = 0 ; i < 256 ; i++)
  495.                                 {
  496.                                     if(LocalCLUT . LUT[i] != i)
  497.                                     {
  498.                                         Blank = FALSE;
  499.  
  500.                                         break;
  501.                                     }
  502.                                 }
  503.  
  504.                                 if(!Blank)
  505.                                 {
  506.                                     if(!PictureInfo -> CLUT[LocalCLUT . Type])
  507.                                     {
  508.                                         if(!(PictureInfo -> CLUT[LocalCLUT . Type] = (struct CLUT *)AllocVecPooled(sizeof(struct CLUT),MEMF_ANY)))
  509.                                             Terminated = TRUE;
  510.                                     }
  511.  
  512.                                     if(PictureInfo -> CLUT[LocalCLUT . Type])
  513.                                     {
  514.                                         CopyMem(&LocalCLUT,PictureInfo -> CLUT[LocalCLUT . Type],sizeof(struct CLUT));
  515.  
  516.                                         PictureInfo -> GotCLUT = TRUE;
  517.                                     }
  518.                                 }
  519.                             }
  520.                         }
  521.  
  522.                         break;
  523.  
  524.                     case ID_BODY:
  525.  
  526.                         GotBody = Terminated = TRUE;
  527.                         break;
  528.                 }
  529.             }
  530.  
  531.             if(!PictureInfo -> CLUT[CLUT_MONO] && PictureInfo -> ColourMap)
  532.             {
  533.                 if(!(PictureInfo -> CLUT[CLUT_MONO] = (struct CLUT *)AllocVecPooled(sizeof(struct CLUT),MEMF_ANY)))
  534.                     *PictureInfo -> Error = ERR_NO_MEM;
  535.                 else
  536.                 {
  537.                     ULONG    RGB;
  538.                     WORD    i;
  539.  
  540.                     PictureInfo -> CLUT[CLUT_MONO] -> Type        = CLUT_MONO;
  541.                     PictureInfo -> CLUT[CLUT_MONO] -> Reserved    = 0;
  542.  
  543.                     for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  544.                     {
  545.                         RGB = PictureInfo -> ColourMap[i];
  546.  
  547.                         PictureInfo -> CLUT[CLUT_MONO] -> LUT[i] = Luminance((RGB >> 16) & 0xFF,(RGB >> 8) & 0xFF,RGB & 0xFF);
  548.                     }
  549.  
  550.                     if(PictureInfo -> ViewModes & EXTRA_HALFBRITE)
  551.                     {
  552.                         for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  553.                             PictureInfo -> CLUT[CLUT_MONO] -> LUT[i + 32] = PictureInfo -> CLUT[CLUT_MONO] -> LUT[i] / 2;
  554.                     }
  555.                 }
  556.             }
  557.  
  558.             if(Error && !(*PictureInfo -> Error))
  559.                 *PictureInfo -> Error = Error;
  560.  
  561.             if(!GotBody)
  562.                 *PictureInfo -> Error = ERR_FILE_FORMAT_ERROR;
  563.             else
  564.             {
  565.                 if(!(*PictureInfo -> Error))
  566.                     return(Handle);
  567.             }
  568.         }
  569.  
  570.         if(Error && !(*PictureInfo -> Error))
  571.             *PictureInfo -> Error = Error;
  572.  
  573.         BufferClose(Handle);
  574.     }
  575.     else
  576.         *PictureInfo -> Error = IoErr();
  577.  
  578.     return(NULL);
  579. }
  580.  
  581. STATIC VOID __regargs
  582. ReadHAM6Data(struct BufferHandle *Handle,struct PictureInfo *PictureInfo)
  583. {
  584.     WORD     Scale[16];
  585.     UBYTE    *Line;
  586.     LONG     i,k;
  587.     BOOL     Error = FALSE;
  588.  
  589.     for(i = 0 ; i < 16 ; i++)
  590.         Scale[i] = 17 * i;
  591.  
  592.     SetMaxProgress(PictureInfo -> BitMapHeader . bmh_Height - 1);
  593.  
  594.     for(i = 0 ; i < PictureInfo -> BitMapHeader . bmh_Height ; i++)
  595.     {
  596.         if(Line = AddLine(PictureInfo -> GreyImage))
  597.         {
  598.             for(k = 0 ; k < PictureInfo -> BitMapHeader . bmh_Depth ; k++)
  599.             {
  600.                 if(PictureInfo -> BitMapHeader . bmh_Compression == 1)
  601.                 {
  602.                     register BYTE Char;
  603.                     register UBYTE *Destination = (UBYTE *)PictureInfo -> BitMap[0] -> Planes[k];
  604.                     register WORD Count,LineBytes = PictureInfo -> BitMap[0] -> BytesPerRow;
  605.  
  606.                     do
  607.                     {
  608.                         if((Count = (BYTE)BufferGet(Handle)) >= 0)
  609.                         {
  610.                             Count++;
  611.  
  612.                             if((LineBytes -= Count) < 0)
  613.                             {
  614.                                 Error = TRUE;
  615.  
  616.                                 break;
  617.                             }
  618.                             else
  619.                             {
  620.                                 if(BufferRead(Handle,(UBYTE *)Destination,Count) == Count)
  621.                                     Destination += Count;
  622.                                 else
  623.                                 {
  624.                                     Error = TRUE;
  625.  
  626.                                     break;
  627.                                 }
  628.                             }
  629.                         }
  630.                         else
  631.                         {
  632.                             if(Count != (WORD)(-128))
  633.                             {
  634.                                 Count = -Count + 1;
  635.  
  636.                                 if((LineBytes -= Count) < 0)
  637.                                 {
  638.                                     Error = TRUE;
  639.  
  640.                                     break;
  641.                                 }
  642.                                 else
  643.                                 {
  644.                                     Char = BufferGet(Handle);
  645.  
  646.                                     do
  647.                                         *Destination++ = Char;
  648.                                     while(--Count);
  649.                                 }
  650.                             }
  651.                         }
  652.                     }
  653.                     while(LineBytes > 0);
  654.  
  655.                     if(Error)
  656.                         break;
  657.                 }
  658.                 else
  659.                 {
  660.                     if(BufferRead(Handle,PictureInfo -> BitMap[0] -> Planes[k],PictureInfo -> BitMap[0] -> BytesPerRow) != PictureInfo -> BitMap[0] -> BytesPerRow)
  661.                     {
  662.                         Error = TRUE;
  663.  
  664.                         break;
  665.                     }
  666.                 }
  667.             }
  668.  
  669.             if(PictureInfo -> BitMapHeader . bmh_Masking == 1 && !Error)
  670.             {
  671.                 if(PictureInfo -> BitMapHeader . bmh_Compression == 1)
  672.                 {
  673.                     register WORD Count,LineBytes = PictureInfo -> BitMap[0] -> BytesPerRow;
  674.  
  675.                     do
  676.                     {
  677.                         if((Count = (BYTE)BufferGet(Handle)) >= 0)
  678.                         {
  679.                             Count++;
  680.  
  681.                             if((LineBytes -= Count) < 0)
  682.                             {
  683.                                 Error = TRUE;
  684.  
  685.                                 break;
  686.                             }
  687.                             else
  688.                             {
  689.                                 if(BufferSeek(Handle,Count) != Count)
  690.                                 {
  691.                                     Error = TRUE;
  692.  
  693.                                     break;
  694.                                 }
  695.                             }
  696.                         }
  697.                         else
  698.                         {
  699.                             if(Count != (WORD)(-128))
  700.                             {
  701.                                 Count = -Count + 1;
  702.  
  703.                                 if((LineBytes -= Count) < 0)
  704.                                 {
  705.                                     Error = TRUE;
  706.  
  707.                                     break;
  708.                                 }
  709.                                 else
  710.                                     BufferGet(Handle);
  711.                             }
  712.                         }
  713.                     }
  714.                     while(LineBytes > 0);
  715.                 }
  716.                 else
  717.                 {
  718.                     if(BufferSeek(Handle,PictureInfo -> BitMap[0] -> BytesPerRow) != PictureInfo -> BitMap[0] -> BytesPerRow)
  719.                     {
  720.                         *PictureInfo -> Error = ERR_READ_ERROR;
  721.  
  722.                         break;
  723.                     }
  724.                 }
  725.             }
  726.  
  727.             if(Error)
  728.             {
  729.                 *PictureInfo -> Error = ERR_READ_ERROR;
  730.  
  731.                 break;
  732.             }
  733.             else
  734.             {
  735.                 register UBYTE    Value;
  736.                 register WORD    R = 0,G = 0,B = 0;
  737.                 register ULONG    Colour;
  738.  
  739.                 (* ReadLine)(&PictureInfo -> DummyRPort,0,PictureInfo -> GreyImage -> Width,PictureInfo -> TempLine[0],PictureInfo -> TempRPort);
  740.  
  741.                 for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  742.                 {
  743.                     switch((Value = PictureInfo -> TempLine[0][k]) & 0x30)
  744.                     {
  745.                         case 0x00:
  746.  
  747.                             Colour = PictureInfo -> ColourMap[Value];
  748.  
  749.                             R = (Colour >> 16) & 0xFF;
  750.                             G = (Colour >> 8) & 0xFF;
  751.                             B = Colour & 0xFF;
  752.  
  753.                             break;
  754.  
  755.                         case 0x10:
  756.  
  757.                             B = Scale[Value & 0x0F];
  758.                             break;
  759.  
  760.                         case 0x20:
  761.  
  762.                             R = Scale[Value & 0x0F];
  763.                             break;
  764.  
  765.                         case 0x30:
  766.  
  767.                             G = Scale[Value & 0x0F];
  768.                             break;
  769.                     }
  770.  
  771.                     *Line++ = Luminance(R,G,B);
  772.                 }
  773.  
  774.                 ShowProgress(i);
  775.             }
  776.         }
  777.         else
  778.         {
  779.             *PictureInfo -> Error = ERR_NO_MEM;
  780.  
  781.             break;
  782.         }
  783.     }
  784. }
  785.  
  786. STATIC VOID __regargs
  787. ConvertHAM6Data(struct PictureInfo *PictureInfo)
  788. {
  789.     WORD     Scale[16];
  790.     UBYTE    *Line;
  791.     LONG     i,k;
  792.  
  793.     for(i = 0 ; i < 16 ; i++)
  794.         Scale[i] = 17 * i;
  795.  
  796.     SetMaxProgress(PictureInfo -> BitMapHeader . bmh_Height - 1);
  797.  
  798.     for(i = 0 ; i < PictureInfo -> BitMapHeader . bmh_Height ; i++)
  799.     {
  800.         if(Line = AddLine(PictureInfo -> GreyImage))
  801.         {
  802.             register UBYTE    Value;
  803.             register WORD    R = 0,G = 0,B = 0;
  804.             register ULONG    Colour;
  805.  
  806.             (* ReadLine)(&PictureInfo -> DummyRPort,i,PictureInfo -> GreyImage -> Width,PictureInfo -> TempLine[0],PictureInfo -> TempRPort);
  807.  
  808.             for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  809.             {
  810.                 switch((Value = PictureInfo -> TempLine[0][k]) & 0x30)
  811.                 {
  812.                     case 0x00:
  813.  
  814.                         Colour = PictureInfo -> ColourMap[Value];
  815.  
  816.                         R = (Colour >> 16) & 0xFF;
  817.                         G = (Colour >> 8) & 0xFF;
  818.                         B = Colour & 0xFF;
  819.  
  820.                         break;
  821.  
  822.                     case 0x10:
  823.  
  824.                         B = Scale[Value & 0x0F];
  825.                         break;
  826.  
  827.                     case 0x20:
  828.  
  829.                         R = Scale[Value & 0x0F];
  830.                         break;
  831.  
  832.                     case 0x30:
  833.  
  834.                         G = Scale[Value & 0x0F];
  835.                         break;
  836.                 }
  837.  
  838.                 *Line++ = Luminance(R,G,B);
  839.             }
  840.  
  841.             ShowProgress(i);
  842.         }
  843.         else
  844.         {
  845.             *PictureInfo -> Error = ERR_NO_MEM;
  846.  
  847.             break;
  848.         }
  849.     }
  850. }
  851.  
  852. STATIC VOID __regargs
  853. ReadHAM8Data(struct BufferHandle *Handle,struct PictureInfo *PictureInfo)
  854. {
  855.     WORD     Scale[64];
  856.     UBYTE    *Line;
  857.     LONG     i,k;
  858.     BOOL     Error = FALSE;
  859.  
  860.     for(i = 0 ; i < 64 ; i++)
  861.         Scale[i] = (255 * i) / 63;
  862.  
  863.     SetMaxProgress(PictureInfo -> BitMapHeader . bmh_Height - 1);
  864.  
  865.     for(i = 0 ; i < PictureInfo -> BitMapHeader . bmh_Height ; i++)
  866.     {
  867.         if(Line = AddLine(PictureInfo -> GreyImage))
  868.         {
  869.             for(k = 0 ; k < PictureInfo -> BitMapHeader . bmh_Depth ; k++)
  870.             {
  871.                 if(PictureInfo -> BitMapHeader . bmh_Compression == 1)
  872.                 {
  873.                     register BYTE Char;
  874.                     register UBYTE *Destination = (UBYTE *)PictureInfo -> BitMap[0] -> Planes[k];
  875.                     register WORD Count,LineBytes = PictureInfo -> BitMap[0] -> BytesPerRow;
  876.  
  877.                     do
  878.                     {
  879.                         if((Count = (BYTE)BufferGet(Handle)) >= 0)
  880.                         {
  881.                             Count++;
  882.  
  883.                             if((LineBytes -= Count) < 0)
  884.                             {
  885.                                 Error = TRUE;
  886.  
  887.                                 break;
  888.                             }
  889.                             else
  890.                             {
  891.                                 if(BufferRead(Handle,(UBYTE *)Destination,Count) == Count)
  892.                                     Destination += Count;
  893.                                 else
  894.                                 {
  895.                                     Error = TRUE;
  896.  
  897.                                     break;
  898.                                 }
  899.                             }
  900.                         }
  901.                         else
  902.                         {
  903.                             if(Count != (WORD)(-128))
  904.                             {
  905.                                 Count = -Count + 1;
  906.  
  907.                                 if((LineBytes -= Count) < 0)
  908.                                 {
  909.                                     Error = TRUE;
  910.  
  911.                                     break;
  912.                                 }
  913.                                 else
  914.                                 {
  915.                                     Char = BufferGet(Handle);
  916.  
  917.                                     do
  918.                                         *Destination++ = Char;
  919.                                     while(--Count);
  920.                                 }
  921.                             }
  922.                         }
  923.                     }
  924.                     while(LineBytes > 0);
  925.  
  926.                     if(Error)
  927.                         break;
  928.                 }
  929.                 else
  930.                 {
  931.                     if(BufferRead(Handle,PictureInfo -> BitMap[0] -> Planes[k],PictureInfo -> BitMap[0] -> BytesPerRow) != PictureInfo -> BitMap[0] -> BytesPerRow)
  932.                     {
  933.                         Error = TRUE;
  934.  
  935.                         break;
  936.                     }
  937.                 }
  938.             }
  939.  
  940.             if(PictureInfo -> BitMapHeader . bmh_Masking == 1 && !Error)
  941.             {
  942.                 if(PictureInfo -> BitMapHeader . bmh_Compression == 1)
  943.                 {
  944.                     register WORD Count,LineBytes = PictureInfo -> BitMap[0] -> BytesPerRow;
  945.  
  946.                     do
  947.                     {
  948.                         if((Count = (BYTE)BufferGet(Handle)) >= 0)
  949.                         {
  950.                             Count++;
  951.  
  952.                             if((LineBytes -= Count) < 0)
  953.                             {
  954.                                 Error = TRUE;
  955.  
  956.                                 break;
  957.                             }
  958.                             else
  959.                             {
  960.                                 if(BufferSeek(Handle,Count) != Count)
  961.                                 {
  962.                                     Error = TRUE;
  963.  
  964.                                     break;
  965.                                 }
  966.                             }
  967.                         }
  968.                         else
  969.                         {
  970.                             if(Count != (WORD)(-128))
  971.                             {
  972.                                 Count = -Count + 1;
  973.  
  974.                                 if((LineBytes -= Count) < 0)
  975.                                 {
  976.                                     Error = TRUE;
  977.  
  978.                                     break;
  979.                                 }
  980.                                 else
  981.                                     BufferGet(Handle);
  982.                             }
  983.                         }
  984.                     }
  985.                     while(LineBytes > 0);
  986.                 }
  987.                 else
  988.                 {
  989.                     if(BufferSeek(Handle,PictureInfo -> BitMap[0] -> BytesPerRow) != PictureInfo -> BitMap[0] -> BytesPerRow)
  990.                     {
  991.                         *PictureInfo -> Error = ERR_READ_ERROR;
  992.  
  993.                         break;
  994.                     }
  995.                 }
  996.             }
  997.  
  998.             if(Error)
  999.             {
  1000.                 *PictureInfo -> Error = ERR_READ_ERROR;
  1001.  
  1002.                 break;
  1003.             }
  1004.             else
  1005.             {
  1006.                 register UBYTE    Value;
  1007.                 register WORD    R = 0,G = 0,B = 0;
  1008.                 register ULONG    Colour;
  1009.  
  1010.                 (* ReadLine)(&PictureInfo -> DummyRPort,0,PictureInfo -> GreyImage -> Width,PictureInfo -> TempLine[0],PictureInfo -> TempRPort);
  1011.  
  1012.                 for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1013.                 {
  1014.                     switch((Value = PictureInfo -> TempLine[0][k]) & 0xC0)
  1015.                     {
  1016.                         case 0x00:
  1017.  
  1018.                             Colour = PictureInfo -> ColourMap[Value];
  1019.  
  1020.                             R = (Colour >> 16) & 0xFF;
  1021.                             G = (Colour >> 8) & 0xFF;
  1022.                             B = Colour & 0xFF;
  1023.  
  1024.                             break;
  1025.  
  1026.                         case 0x40:
  1027.  
  1028.                             B = Scale[Value & 0x3F];
  1029.                             break;
  1030.  
  1031.                         case 0x80:
  1032.  
  1033.                             R = Scale[Value & 0x3F];
  1034.                             break;
  1035.  
  1036.                         case 0xC0:
  1037.  
  1038.                             G = Scale[Value & 0x3F];
  1039.                             break;
  1040.                     }
  1041.  
  1042.                     *Line++ = Luminance(R,G,B);
  1043.                 }
  1044.  
  1045.                 ShowProgress(i);
  1046.             }
  1047.         }
  1048.         else
  1049.         {
  1050.             *PictureInfo -> Error = ERR_NO_MEM;
  1051.  
  1052.             break;
  1053.         }
  1054.     }
  1055. }
  1056.  
  1057. STATIC VOID __regargs
  1058. ConvertHAM8Data(struct PictureInfo *PictureInfo)
  1059. {
  1060.     WORD     Scale[64];
  1061.     UBYTE    *Line;
  1062.     LONG     i,k;
  1063.  
  1064.     for(i = 0 ; i < 64 ; i++)
  1065.         Scale[i] = (255 * i) / 63;
  1066.  
  1067.     SetMaxProgress(PictureInfo -> BitMapHeader . bmh_Height - 1);
  1068.  
  1069.     for(i = 0 ; i < PictureInfo -> BitMapHeader . bmh_Height ; i++)
  1070.     {
  1071.         if(Line = AddLine(PictureInfo -> GreyImage))
  1072.         {
  1073.             register UBYTE    Value;
  1074.             register WORD    R = 0,G = 0,B = 0;
  1075.             register ULONG    Colour;
  1076.  
  1077.             (* ReadLine)(&PictureInfo -> DummyRPort,i,PictureInfo -> GreyImage -> Width,PictureInfo -> TempLine[0],PictureInfo -> TempRPort);
  1078.  
  1079.             for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1080.             {
  1081.                 switch((Value = PictureInfo -> TempLine[0][k]) & 0xC0)
  1082.                 {
  1083.                     case 0x00:
  1084.  
  1085.                         Colour = PictureInfo -> ColourMap[Value];
  1086.  
  1087.                         R = (Colour >> 16) & 0xFF;
  1088.                         G = (Colour >> 8) & 0xFF;
  1089.                         B = Colour & 0xFF;
  1090.  
  1091.                         break;
  1092.  
  1093.                     case 0x40:
  1094.  
  1095.                         B = Scale[Value & 0x3F];
  1096.                         break;
  1097.  
  1098.                     case 0x80:
  1099.  
  1100.                         R = Scale[Value & 0x3F];
  1101.                         break;
  1102.  
  1103.                     case 0xC0:
  1104.  
  1105.                         G = Scale[Value & 0x3F];
  1106.                         break;
  1107.                 }
  1108.  
  1109.                 *Line++ = Luminance(R,G,B);
  1110.             }
  1111.  
  1112.             ShowProgress(i);
  1113.         }
  1114.         else
  1115.         {
  1116.             *PictureInfo -> Error = ERR_NO_MEM;
  1117.  
  1118.             break;
  1119.         }
  1120.     }
  1121. }
  1122.  
  1123. STATIC VOID __regargs
  1124. ReadVanillaData(struct BufferHandle *Handle,struct PictureInfo *PictureInfo)
  1125. {
  1126.     UBYTE    *Line,
  1127.         *LUT;
  1128.     LONG     i,k;
  1129.     WORD     LUTType;
  1130.     BOOL     Error = FALSE;
  1131.  
  1132.     for(i = CLUT_MONO ; i <= CLUT_BLUE ; i++)
  1133.     {
  1134.         if(PictureInfo -> CLUT[i])
  1135.         {
  1136.             LUT    = PictureInfo -> CLUT[i] -> LUT;
  1137.             LUTType    = i;
  1138.  
  1139.             break;
  1140.         }
  1141.     }
  1142.  
  1143.     SetMaxProgress(PictureInfo -> BitMapHeader . bmh_Height - 1);
  1144.  
  1145.     for(i = 0 ; i < PictureInfo -> BitMapHeader . bmh_Height ; i++)
  1146.     {
  1147.         if(Line = AddLine(PictureInfo -> GreyImage))
  1148.         {
  1149.             for(k = 0 ; k < PictureInfo -> BitMapHeader . bmh_Depth ; k++)
  1150.             {
  1151.                 if(PictureInfo -> BitMapHeader . bmh_Compression == 1)
  1152.                 {
  1153.                     register BYTE Char;
  1154.                     register UBYTE *Destination = (UBYTE *)PictureInfo -> BitMap[0] -> Planes[k];
  1155.                     register WORD Count,LineBytes = PictureInfo -> BitMap[0] -> BytesPerRow;
  1156.  
  1157.                     do
  1158.                     {
  1159.                         if((Count = (BYTE)BufferGet(Handle)) >= 0)
  1160.                         {
  1161.                             Count++;
  1162.  
  1163.                             if((LineBytes -= Count) < 0)
  1164.                             {
  1165.                                 Error = TRUE;
  1166.  
  1167.                                 break;
  1168.                             }
  1169.                             else
  1170.                             {
  1171.                                 if(BufferRead(Handle,(UBYTE *)Destination,Count) == Count)
  1172.                                     Destination += Count;
  1173.                                 else
  1174.                                 {
  1175.                                     Error = TRUE;
  1176.  
  1177.                                     break;
  1178.                                 }
  1179.                             }
  1180.                         }
  1181.                         else
  1182.                         {
  1183.                             if(Count != (WORD)(-128))
  1184.                             {
  1185.                                 Count = -Count + 1;
  1186.  
  1187.                                 if((LineBytes -= Count) < 0)
  1188.                                 {
  1189.                                     Error = TRUE;
  1190.  
  1191.                                     break;
  1192.                                 }
  1193.                                 else
  1194.                                 {
  1195.                                     Char = BufferGet(Handle);
  1196.  
  1197.                                     do
  1198.                                         *Destination++ = Char;
  1199.                                     while(--Count);
  1200.                                 }
  1201.                             }
  1202.                         }
  1203.                     }
  1204.                     while(LineBytes > 0);
  1205.  
  1206.                     if(Error)
  1207.                         break;
  1208.                 }
  1209.                 else
  1210.                 {
  1211.                     if(BufferRead(Handle,PictureInfo -> BitMap[0] -> Planes[k],PictureInfo -> BitMap[0] -> BytesPerRow) != PictureInfo -> BitMap[0] -> BytesPerRow)
  1212.                     {
  1213.                         Error = TRUE;
  1214.  
  1215.                         break;
  1216.                     }
  1217.                 }
  1218.             }
  1219.  
  1220.             if(PictureInfo -> BitMapHeader . bmh_Masking == 1 && !Error)
  1221.             {
  1222.                 if(PictureInfo -> BitMapHeader . bmh_Compression == 1)
  1223.                 {
  1224.                     register WORD Count,LineBytes = PictureInfo -> BitMap[0] -> BytesPerRow;
  1225.  
  1226.                     do
  1227.                     {
  1228.                         if((Count = (BYTE)BufferGet(Handle)) >= 0)
  1229.                         {
  1230.                             Count++;
  1231.  
  1232.                             if((LineBytes -= Count) < 0)
  1233.                             {
  1234.                                 Error = TRUE;
  1235.  
  1236.                                 break;
  1237.                             }
  1238.                             else
  1239.                             {
  1240.                                 if(BufferSeek(Handle,Count) != Count)
  1241.                                 {
  1242.                                     Error = TRUE;
  1243.  
  1244.                                     break;
  1245.                                 }
  1246.                             }
  1247.                         }
  1248.                         else
  1249.                         {
  1250.                             if(Count != (WORD)(-128))
  1251.                             {
  1252.                                 Count = -Count + 1;
  1253.  
  1254.                                 if((LineBytes -= Count) < 0)
  1255.                                 {
  1256.                                     Error = TRUE;
  1257.  
  1258.                                     break;
  1259.                                 }
  1260.                                 else
  1261.                                     BufferGet(Handle);
  1262.                             }
  1263.                         }
  1264.                     }
  1265.                     while(LineBytes > 0);
  1266.                 }
  1267.                 else
  1268.                 {
  1269.                     if(BufferSeek(Handle,PictureInfo -> BitMap[0] -> BytesPerRow) != PictureInfo -> BitMap[0] -> BytesPerRow)
  1270.                     {
  1271.                         *PictureInfo -> Error = ERR_READ_ERROR;
  1272.  
  1273.                         break;
  1274.                     }
  1275.                 }
  1276.             }
  1277.  
  1278.             if(Error)
  1279.             {
  1280.                 *PictureInfo -> Error = ERR_READ_ERROR;
  1281.  
  1282.                 break;
  1283.             }
  1284.             else
  1285.             {
  1286.                 (* ReadLine)(&PictureInfo -> DummyRPort,0,PictureInfo -> GreyImage -> Width,PictureInfo -> TempLine[0],PictureInfo -> TempRPort);
  1287.  
  1288.                 if(LUT)
  1289.                 {
  1290.                     switch(LUTType)
  1291.                     {
  1292.                         case CLUT_MONO:
  1293.  
  1294.                             for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1295.                                 *Line++ = LUT[PictureInfo -> TempLine[0][k]];
  1296.  
  1297.                             break;
  1298.  
  1299.                         case CLUT_RED:
  1300.  
  1301.                             for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1302.                                 *Line++ = Luminance(LUT[PictureInfo -> TempLine[0][k]],0,0);
  1303.  
  1304.                             break;
  1305.  
  1306.                         case CLUT_GREEN:
  1307.  
  1308.                             for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1309.                                 *Line++ = Luminance(0,LUT[PictureInfo -> TempLine[0][k]],0);
  1310.  
  1311.                             break;
  1312.  
  1313.                         case CLUT_BLUE:
  1314.  
  1315.                             for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1316.                                 *Line++ = Luminance(0,0,LUT[PictureInfo -> TempLine[0][k]]);
  1317.  
  1318.                             break;
  1319.                     }
  1320.                 }
  1321.                 else
  1322.                     CopyMem(PictureInfo -> TempLine[0],Line,PictureInfo -> GreyImage -> Width);
  1323.  
  1324.                 ShowProgress(i);
  1325.             }
  1326.         }
  1327.         else
  1328.         {
  1329.             *PictureInfo -> Error = ERR_NO_MEM;
  1330.  
  1331.             break;
  1332.         }
  1333.     }
  1334. }
  1335.  
  1336. STATIC VOID __regargs
  1337. ConvertVanillaData(struct PictureInfo *PictureInfo)
  1338. {
  1339.     UBYTE    *Line,
  1340.         *LUT;
  1341.     LONG     i,k;
  1342.     WORD     LUTType;
  1343.  
  1344.     for(i = CLUT_MONO ; i <= CLUT_BLUE ; i++)
  1345.     {
  1346.         if(PictureInfo -> CLUT[i])
  1347.         {
  1348.             LUT    = PictureInfo -> CLUT[i] -> LUT;
  1349.             LUTType    = i;
  1350.  
  1351.             break;
  1352.         }
  1353.     }
  1354.  
  1355.     SetMaxProgress(PictureInfo -> BitMapHeader . bmh_Height - 1);
  1356.  
  1357.     for(i = 0 ; i < PictureInfo -> BitMapHeader . bmh_Height ; i++)
  1358.     {
  1359.         if(Line = AddLine(PictureInfo -> GreyImage))
  1360.         {
  1361.             (* ReadLine)(&PictureInfo -> DummyRPort,i,PictureInfo -> GreyImage -> Width,PictureInfo -> TempLine[0],PictureInfo -> TempRPort);
  1362.  
  1363.             if(LUT)
  1364.             {
  1365.                 switch(LUTType)
  1366.                 {
  1367.                     case CLUT_MONO:
  1368.  
  1369.                         for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1370.                             *Line++ = LUT[PictureInfo -> TempLine[0][k]];
  1371.  
  1372.                         break;
  1373.  
  1374.                     case CLUT_RED:
  1375.  
  1376.                         for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1377.                             *Line++ = Luminance(LUT[PictureInfo -> TempLine[0][k]],0,0);
  1378.  
  1379.                         break;
  1380.  
  1381.                     case CLUT_GREEN:
  1382.  
  1383.                         for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1384.                             *Line++ = Luminance(0,LUT[PictureInfo -> TempLine[0][k]],0);
  1385.  
  1386.                         break;
  1387.  
  1388.                     case CLUT_BLUE:
  1389.  
  1390.                         for(k = 0 ; k < PictureInfo -> GreyImage -> Width ; k++)
  1391.                             *Line++ = Luminance(0,0,LUT[PictureInfo -> TempLine[0][k]]);
  1392.  
  1393.                         break;
  1394.                 }
  1395.             }
  1396.             else
  1397.                 CopyMem(PictureInfo -> TempLine[0],Line,PictureInfo -> GreyImage -> Width);
  1398.  
  1399.             ShowProgress(i);
  1400.         }
  1401.         else
  1402.         {
  1403.             *PictureInfo -> Error = ERR_NO_MEM;
  1404.  
  1405.             break;
  1406.         }
  1407.     }
  1408. }
  1409.  
  1410. STATIC VOID __regargs
  1411. ReadDeepData(struct BufferHandle *Handle,struct PictureInfo *PictureInfo)
  1412. {
  1413.     WORD    Scale[256];
  1414.     UBYTE    *Line;
  1415.     LONG    i,j,k;
  1416.     WORD    NumPlanes = PictureInfo -> BitMapHeader . bmh_Depth / 3;
  1417.     BOOL    Error = FALSE;
  1418.  
  1419.     for(i = 0 ; i < (1 << NumPlanes) ; i++)
  1420.         Scale[i] = (255 * i) / ((1 << NumPlanes) - 1);
  1421.  
  1422.     SetMaxProgress(PictureInfo -> BitMapHeader . bmh_Height - 1);
  1423.  
  1424.     for(i = 0 ; i < PictureInfo -> BitMapHeader . bmh_Height ; i++)
  1425.     {
  1426.         if(Line = AddLine(PictureInfo -> GreyImage))
  1427.         {
  1428.             for(j = 0 ; j < 3 ; j++)
  1429.             {
  1430.                 for(k = 0 ; k < NumPlanes ; k++)
  1431.                 {
  1432.                     if(PictureInfo -> BitMapHeader . bmh_Compression == 1)
  1433.                     {
  1434.                         register BYTE Char;
  1435.                         register UBYTE *Destination = (UBYTE *)PictureInfo -> BitMap[j] -> Planes[k];
  1436.                         register WORD Count,LineBytes = PictureInfo -> BitMap[j] -> BytesPerRow;
  1437.  
  1438.                         do
  1439.                         {
  1440.                             if((Count = (BYTE)BufferGet(Handle)) >= 0)
  1441.                             {
  1442.                                 Count++;
  1443.  
  1444.                                 if((LineBytes -= Count) < 0)
  1445.                                 {
  1446.                                     Error = TRUE;
  1447.  
  1448.                                     break;
  1449.                                 }
  1450.                                 else
  1451.                                 {
  1452.                                     if(BufferRead(Handle,(UBYTE *)Destination,Count) == Count)
  1453.                                         Destination += Count;
  1454.                                     else
  1455.                                     {
  1456.                                         Error = TRUE;
  1457.  
  1458.                                         break;
  1459.                                     }
  1460.                                 }
  1461.                             }
  1462.                             else
  1463.                             {
  1464.                                 if(Count != (WORD)(-128))
  1465.                                 {
  1466.                                     Count = -Count + 1;
  1467.  
  1468.                                     if((LineBytes -= Count) < 0)
  1469.                                     {
  1470.                                         Error = TRUE;
  1471.  
  1472.                                         break;
  1473.                                     }
  1474.                                     else
  1475.                                     {
  1476.                                         Char = BufferGet(Handle);
  1477.  
  1478.                                         do
  1479.                                             *Destination++ = Char;
  1480.                                         while(--Count);
  1481.                                     }
  1482.                                 }
  1483.                             }
  1484.                         }
  1485.                         while(LineBytes > 0);
  1486.  
  1487.                         if(Error)
  1488.                             break;
  1489.                     }
  1490.                     else
  1491.                     {
  1492.                         if(BufferRead(Handle,PictureInfo -> BitMap[j] -> Planes[k],PictureInfo -> BitMap[j] -> BytesPerRow) != PictureInfo -> BitMap[j] -> BytesPerRow)
  1493.                         {
  1494.                             Error = TRUE;
  1495.  
  1496.                             break;
  1497.                         }
  1498.                     }
  1499.                 }
  1500.  
  1501.                 PictureInfo -> DummyRPort . BitMap = PictureInfo -> BitMap[j];
  1502.  
  1503.                 (* ReadLine)(&PictureInfo -> DummyRPort,0,PictureInfo -> GreyImage -> Width,PictureInfo -> TempLine[j],PictureInfo -> TempRPort);
  1504.  
  1505.                 if(Error)
  1506.                     break;
  1507.             }
  1508.  
  1509.             if(Error)
  1510.             {
  1511.                 *PictureInfo -> Error = ERR_READ_ERROR;
  1512.  
  1513.                 break;
  1514.             }
  1515.             else
  1516.             {
  1517.                 if(PictureInfo -> GotCLUT)
  1518.                 {
  1519.                     register WORD R,G,B;
  1520.  
  1521.                     for(j = 0 ; j < PictureInfo -> GreyImage -> Width ; j++)
  1522.                     {
  1523.                         if(PictureInfo -> CLUT[CLUT_RED])
  1524.                             R = Scale[PictureInfo -> CLUT[CLUT_RED] -> LUT[PictureInfo -> TempLine[0][j]]];
  1525.                         else
  1526.                             R = Scale[PictureInfo -> TempLine[0][j]];
  1527.  
  1528.                         if(PictureInfo -> CLUT[CLUT_GREEN])
  1529.                             G = Scale[PictureInfo -> CLUT[CLUT_GREEN] -> LUT[PictureInfo -> TempLine[1][j]]];
  1530.                         else
  1531.                             G = Scale[PictureInfo -> TempLine[1][j]];
  1532.  
  1533.                         if(PictureInfo -> CLUT[CLUT_BLUE])
  1534.                             B = Scale[PictureInfo -> CLUT[CLUT_BLUE] -> LUT[PictureInfo -> TempLine[2][j]]];
  1535.                         else
  1536.                             B = Scale[PictureInfo -> TempLine[2][j]];
  1537.  
  1538.                         *Line++ = Luminance(R,G,B);
  1539.                     }
  1540.                 }
  1541.                 else
  1542.                 {
  1543.                     for(j = 0 ; j < PictureInfo -> GreyImage -> Width ; j++)
  1544.                         *Line++ = Luminance(Scale[PictureInfo -> TempLine[0][j]],Scale[PictureInfo -> TempLine[1][j]],Scale[PictureInfo -> TempLine[2][j]]);
  1545.                 }
  1546.  
  1547.                 ShowProgress(i);
  1548.             }
  1549.         }
  1550.         else
  1551.         {
  1552.             *PictureInfo -> Error = ERR_NO_MEM;
  1553.  
  1554.             break;
  1555.         }
  1556.     }
  1557. }
  1558.  
  1559. STATIC struct GreyImage * __regargs
  1560. ReadPicture(STRPTR Name,BOOL PatchColours,LONG *Error)
  1561. {
  1562.     struct GreyImage    *Image = NULL;
  1563.     Object            *Picture;
  1564.  
  1565.     *Error = 0;
  1566.  
  1567.     if(Picture = NewDTObject(Name,
  1568.         DTA_SourceType,    DTST_FILE,
  1569.         DTA_GroupID,    GID_PICTURE,
  1570.  
  1571.         PDTA_Remap,    FALSE,
  1572.     TAG_DONE))
  1573.     {
  1574.         if(DoMethod(Picture,DTM_PROCLAYOUT,NULL,TRUE))
  1575.         {
  1576.             ULONG             ViewModes = INVALID_ID;
  1577.             LONG             NumColours = 0;
  1578.             struct BitMapHeader    *BitMapHeader = NULL;
  1579.             struct BitMap        *BitMap = NULL;
  1580.             ULONG            *CRegs = NULL;
  1581.  
  1582.             if(GetDTAttrs(Picture,
  1583.                 PDTA_ModeID,        &ViewModes,
  1584.                 PDTA_NumColors,        &NumColours,
  1585.                 PDTA_BitMapHeader,    &BitMapHeader,
  1586.                 PDTA_BitMap,        &BitMap,
  1587.                 PDTA_CRegs,        &CRegs,
  1588.             TAG_DONE))
  1589.             {
  1590.                 if(ViewModes != INVALID_ID && CRegs && NumColours && BitMapHeader && BitMap)
  1591.                 {
  1592.                     struct PictureInfo *PictureInfo;
  1593.  
  1594.                     if(BitMapHeader -> bmh_Pad & BMHDF_CMAPOK)
  1595.                         PatchColours = FALSE;
  1596.  
  1597.                     if(PictureInfo = (struct PictureInfo *)AllocVecPooled(sizeof(struct PictureInfo),MEMF_ANY | MEMF_CLEAR))
  1598.                     {
  1599.                         PictureInfo -> Error        = Error;
  1600.                         PictureInfo -> NumColours    = NumColours;
  1601.                         PictureInfo -> ViewModes    = ViewModes;
  1602.                         PictureInfo -> BitMap[0]    = BitMap;
  1603.  
  1604.                         CopyMem(BitMapHeader,&PictureInfo -> BitMapHeader,sizeof(struct BitMapHeader));
  1605.  
  1606.                         if(PictureInfo -> ColourMap = (ULONG *)AllocVecPooled(sizeof(ULONG) * NumColours,MEMF_ANY))
  1607.                         {
  1608.                             LONG i;
  1609.  
  1610.                             for(i = 0 ; i < NumColours ; i++)
  1611.                                 PictureInfo -> ColourMap[i] = ((CRegs[i * 3] >> 8) & 0xFF0000) | ((CRegs[i * 3 + 1] >> 16) & 0xFF00) | ((CRegs[i * 3 + 2] >> 24) & 0xFF);
  1612.  
  1613.                             if(PatchColours)
  1614.                             {
  1615.                                 BOOL Patch = TRUE;
  1616.  
  1617.                                 for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  1618.                                 {
  1619.                                     if(PictureInfo -> ColourMap[i] & 0x0F0F0F)
  1620.                                     {
  1621.                                         Patch = FALSE;
  1622.  
  1623.                                         break;
  1624.                                     }
  1625.                                 }
  1626.  
  1627.                                 if(Patch)
  1628.                                 {
  1629.                                     for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  1630.                                         PictureInfo -> ColourMap[i] |= (PictureInfo -> ColourMap[i] >> 4);
  1631.                                 }
  1632.                             }
  1633.  
  1634.                             if(PictureInfo -> CLUT[CLUT_MONO] = (struct CLUT *)AllocVecPooled(sizeof(struct CLUT),MEMF_ANY))
  1635.                             {
  1636.                                 ULONG RGB;
  1637.  
  1638.                                 PictureInfo -> CLUT[CLUT_MONO] -> Type        = CLUT_MONO;
  1639.                                 PictureInfo -> CLUT[CLUT_MONO] -> Reserved    = 0;
  1640.  
  1641.                                 for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  1642.                                 {
  1643.                                     RGB = PictureInfo -> ColourMap[i];
  1644.  
  1645.                                     PictureInfo -> CLUT[CLUT_MONO] -> LUT[i] = Luminance((RGB >> 16) & 0xFF,(RGB >> 8) & 0xFF,RGB & 0xFF);
  1646.                                 }
  1647.  
  1648.                                 if(PictureInfo -> ViewModes & EXTRA_HALFBRITE)
  1649.                                 {
  1650.                                     for(i = 0 ; i < PictureInfo -> NumColours ; i++)
  1651.                                         PictureInfo -> CLUT[CLUT_MONO] -> LUT[i + 32] = PictureInfo -> CLUT[CLUT_MONO] -> LUT[i] / 2;
  1652.                                 }
  1653.  
  1654.                                 if(PictureInfo -> GreyImage = CreateImage(PictureInfo -> BitMapHeader . bmh_Width,PictureInfo -> BitMapHeader . bmh_Height))
  1655.                                 {
  1656.                                     if(PictureInfo -> BitMapHeader . bmh_Depth <= 8)
  1657.                                     {
  1658.                                         if(PictureInfo -> TempLine[0] = CreateTempLine(PictureInfo -> BitMapHeader . bmh_Width,1))
  1659.                                         {
  1660.                                             InitRastPort(&PictureInfo -> DummyRPort);
  1661.  
  1662.                                             PictureInfo -> DummyRPort . BitMap = PictureInfo -> BitMap[0];
  1663.  
  1664.                                             if(PictureInfo -> TempRPort = CreateTempRPort(&PictureInfo -> DummyRPort))
  1665.                                             {
  1666.                                                 if(PictureInfo -> ViewModes & EXTRA_HALFBRITE)
  1667.                                                 {
  1668.                                                     if(PictureInfo -> BitMapHeader . bmh_Depth == 6)
  1669.                                                         ConvertVanillaData(PictureInfo);
  1670.                                                     else
  1671.                                                         *Error = ERR_FILE_FORMAT_ERROR;
  1672.                                                 }
  1673.                                                 else
  1674.                                                 {
  1675.                                                     if(PictureInfo -> ViewModes & HAM)
  1676.                                                     {
  1677.                                                         if(PictureInfo -> BitMapHeader . bmh_Depth == 6)
  1678.                                                             ConvertHAM6Data(PictureInfo);
  1679.                                                         else
  1680.                                                         {
  1681.                                                             if(PictureInfo -> BitMapHeader . bmh_Depth == 8)
  1682.                                                                 ConvertHAM8Data(PictureInfo);
  1683.                                                             else
  1684.                                                                 *Error = ERR_FILE_FORMAT_ERROR;
  1685.                                                         }
  1686.                                                     }
  1687.                                                     else
  1688.                                                         ConvertVanillaData(PictureInfo);
  1689.                                                 }
  1690.  
  1691.                                                 DeleteTempRPort(PictureInfo -> TempRPort);
  1692.                                             }
  1693.                                             else
  1694.                                                 *Error = ERR_NO_MEM;
  1695.  
  1696.                                             DeleteTempLine(PictureInfo -> TempLine[0]);
  1697.                                         }
  1698.                                         else
  1699.                                             *Error = ERR_NO_MEM;
  1700.                                     }
  1701.                                     else
  1702.                                         *Error = ERR_NO_MEM;
  1703.  
  1704.                                     if(*Error)
  1705.                                         DeleteImage(PictureInfo -> GreyImage);
  1706.                                     else
  1707.                                         Image = PictureInfo -> GreyImage;
  1708.                                 }
  1709.                                 else
  1710.                                     *Error = ERR_NO_MEM;
  1711.  
  1712.                                 FreeVecPooled(PictureInfo -> CLUT[CLUT_MONO]);
  1713.                             }
  1714.                             else
  1715.                                 *Error = ERR_NO_MEM;
  1716.  
  1717.                             FreeVecPooled(PictureInfo -> ColourMap);
  1718.                         }
  1719.                         else
  1720.                             *Error = ERR_NO_MEM;
  1721.  
  1722.                         FreeVecPooled(PictureInfo);
  1723.                     }
  1724.                     else
  1725.                         *Error = ERR_NO_MEM;
  1726.                 }
  1727.                 else
  1728.                     *Error = ERR_NO_MEM;
  1729.             }
  1730.             else
  1731.                 *Error = ERR_NO_MEM;
  1732.         }
  1733.         else
  1734.             *Error = ERR_NO_MEM;
  1735.  
  1736.         DisposeDTObject(Picture);
  1737.     }
  1738.     else
  1739.         *Error = IoErr();
  1740.  
  1741.     return(Image);
  1742. }
  1743.  
  1744. STATIC struct GreyImage * __regargs
  1745. ReadILBM(STRPTR Name,BOOL PatchColours,LONG *Error)
  1746. {
  1747.     struct PictureInfo    *PictureInfo;
  1748.     struct GreyImage    *Image = NULL;
  1749.  
  1750.     *Error = 0;
  1751.  
  1752.     if(PictureInfo = (struct PictureInfo *)AllocVecPooled(sizeof(struct PictureInfo),MEMF_ANY | MEMF_CLEAR))
  1753.     {
  1754.         struct BufferHandle    *Handle;
  1755.         LONG             i;
  1756.  
  1757.         PictureInfo -> Error = Error;
  1758.  
  1759.         if(Handle = ReadImageHeader(Name,PatchColours,PictureInfo))
  1760.         {
  1761.             if(PictureInfo -> GreyImage = CreateImage(PictureInfo -> BitMapHeader . bmh_Width,PictureInfo -> BitMapHeader . bmh_Height))
  1762.             {
  1763.                 BOOL GotIt = TRUE,Deep;
  1764.  
  1765.                 if(PictureInfo -> BitMapHeader . bmh_Depth <= 8)
  1766.                 {
  1767.                     if(!(PictureInfo -> BitMap[0] = CreateBitMap(PictureInfo -> BitMapHeader . bmh_Width,1,PictureInfo -> BitMapHeader . bmh_Depth,NULL,NULL)))
  1768.                         GotIt = FALSE;
  1769.                     else
  1770.                     {
  1771.                         if(!(PictureInfo -> TempLine[0] = CreateTempLine(PictureInfo -> BitMapHeader . bmh_Width,1)))
  1772.                             GotIt = FALSE;
  1773.                         else
  1774.                             Deep = FALSE;
  1775.                     }
  1776.                 }
  1777.                 else
  1778.                 {
  1779.                     if(PictureInfo -> BitMapHeader . bmh_Depth >= 12 && !(PictureInfo -> BitMapHeader . bmh_Depth % 3))
  1780.                     {
  1781.                         Deep = TRUE;
  1782.  
  1783.                         for(i = 0 ; i < 3 ; i++)
  1784.                         {
  1785.                             if(!(PictureInfo -> BitMap[i] = CreateBitMap(PictureInfo -> BitMapHeader . bmh_Width,1,PictureInfo -> BitMapHeader . bmh_Depth / 3,NULL,NULL)))
  1786.                             {
  1787.                                 GotIt = FALSE;
  1788.  
  1789.                                 break;
  1790.                             }
  1791.                             else
  1792.                             {
  1793.                                 if(!(PictureInfo -> TempLine[i] = CreateTempLine(PictureInfo -> BitMapHeader . bmh_Width,1)))
  1794.                                 {
  1795.                                     GotIt = FALSE;
  1796.  
  1797.                                     break;
  1798.                                 }
  1799.                             }
  1800.                         }
  1801.                     }
  1802.                     else
  1803.                         GotIt = FALSE;
  1804.                 }
  1805.  
  1806.                 if(GotIt)
  1807.                 {
  1808.                     InitRastPort(&PictureInfo -> DummyRPort);
  1809.  
  1810.                     PictureInfo -> DummyRPort . BitMap = PictureInfo -> BitMap[0];
  1811.  
  1812.                     if(PictureInfo -> TempRPort = CreateTempRPort(&PictureInfo -> DummyRPort))
  1813.                     {
  1814.                         if(Deep)
  1815.                         {
  1816.                             if(!PictureInfo -> BitMapHeader . bmh_Masking)
  1817.                                 ReadDeepData(Handle,PictureInfo);
  1818.                             else
  1819.                                 *Error = ERR_FILE_FORMAT_ERROR;
  1820.                         }
  1821.                         else
  1822.                         {
  1823.                             if(PictureInfo -> ViewModes & EXTRA_HALFBRITE)
  1824.                             {
  1825.                                 if(PictureInfo -> BitMapHeader . bmh_Depth == 6)
  1826.                                     ReadVanillaData(Handle,PictureInfo);
  1827.                                 else
  1828.                                     *Error = ERR_FILE_FORMAT_ERROR;
  1829.                             }
  1830.                             else
  1831.                             {
  1832.                                 if(PictureInfo -> ViewModes & HAM)
  1833.                                 {
  1834.                                     if(PictureInfo -> BitMapHeader . bmh_Depth == 6)
  1835.                                         ReadHAM6Data(Handle,PictureInfo);
  1836.                                     else
  1837.                                     {
  1838.                                         if(PictureInfo -> BitMapHeader . bmh_Depth == 8)
  1839.                                             ReadHAM8Data(Handle,PictureInfo);
  1840.                                         else
  1841.                                             *Error = ERR_FILE_FORMAT_ERROR;
  1842.                                     }
  1843.                                 }
  1844.                                 else
  1845.                                     ReadVanillaData(Handle,PictureInfo);
  1846.                             }
  1847.                         }
  1848.  
  1849.                         DeleteTempRPort(PictureInfo -> TempRPort);
  1850.                     }
  1851.                     else
  1852.                         *Error = ERR_NO_MEM;
  1853.                 }
  1854.                 else
  1855.                     *Error = ERR_NO_MEM;
  1856.  
  1857.                 for(i = 0 ; i < 3 ; i++)
  1858.                 {
  1859.                     if(PictureInfo -> BitMap[i])
  1860.                         DeleteBitMap(PictureInfo -> BitMap[i]);
  1861.  
  1862.                     if(PictureInfo -> TempLine[i])
  1863.                         DeleteTempLine(PictureInfo -> TempLine[i]);
  1864.                 }
  1865.  
  1866.                 if(*Error)
  1867.                     DeleteImage(PictureInfo -> GreyImage);
  1868.                 else
  1869.                     Image = PictureInfo -> GreyImage;
  1870.             }
  1871.             else
  1872.                 *Error = ERR_NO_MEM;
  1873.  
  1874.             BufferClose(Handle);
  1875.         }
  1876.  
  1877.         for(i = 0 ; i < CLUT_HUE ; i++)
  1878.         {
  1879.             if(PictureInfo -> CLUT[i])
  1880.                 FreeVecPooled(PictureInfo -> CLUT[i]);
  1881.         }
  1882.  
  1883.         if(PictureInfo -> ColourMap)
  1884.             FreeVecPooled(PictureInfo -> ColourMap);
  1885.  
  1886.         FreeVecPooled(PictureInfo);
  1887.     }
  1888.     else
  1889.         *Error = ERR_NO_MEM;
  1890.  
  1891.     return(Image);
  1892. }
  1893.  
  1894. STATIC BOOL __regargs
  1895. GetPictureSize(STRPTR Name,LONG *Width,LONG *Height,LONG *Error)
  1896. {
  1897.     Object    *Picture;
  1898.     BOOL     Result = FALSE;
  1899.  
  1900.     *Error = 0;
  1901.  
  1902.     if(Picture = NewDTObject(Name,
  1903.         DTA_SourceType,    DTST_FILE,
  1904.         DTA_GroupID,    GID_PICTURE,
  1905.  
  1906.         PDTA_Remap,    FALSE,
  1907.     TAG_DONE))
  1908.     {
  1909.         struct FrameInfo FrameInfo;
  1910.  
  1911.         memset(&FrameInfo,0,sizeof(struct FrameInfo));
  1912.  
  1913.         if(DoMethod(Picture,DTM_FRAMEBOX,NULL,&FrameInfo,&FrameInfo,sizeof(struct FrameInfo),NULL))
  1914.         {
  1915.             if(FrameInfo . fri_Dimensions . Depth)
  1916.             {
  1917.                 *Width    = FrameInfo . fri_Dimensions . Width;
  1918.                 *Height    = FrameInfo . fri_Dimensions . Height;
  1919.  
  1920.                 Result    = TRUE;
  1921.             }
  1922.             else
  1923.                 *Error = ERR_WRONG_IMAGE_FORMAT;
  1924.         }
  1925.         else
  1926.             *Error = ERR_FILE_FORMAT_ERROR;
  1927.  
  1928.         DisposeDTObject(Picture);
  1929.     }
  1930.     else
  1931.         *Error = IoErr();
  1932.  
  1933.     return(Result);
  1934. }
  1935.  
  1936. STATIC BOOL __regargs
  1937. GetILBMSize(STRPTR Name,LONG *Width,LONG *Height,LONG *Error)
  1938. {
  1939.     struct IFFHandle    *Handle;
  1940.     BOOL             Result = FALSE;
  1941.     LONG             ClipID;
  1942.  
  1943.     *Error = 0;
  1944.  
  1945.     if(Handle = AllocIFF())
  1946.     {
  1947.         if(GetClipID(Name,&ClipID))
  1948.         {
  1949.             if(Handle -> iff_Stream = (ULONG)OpenClipboard(ClipID))
  1950.             {
  1951.                 InitIFFasClip(Handle);
  1952.  
  1953.                 if(!(*Error = OpenIFF(Handle,IFFF_READ)))
  1954.                 {
  1955.                     if(!(*Error = StopChunk(Handle,ID_ILBM,ID_BMHD)))
  1956.                     {
  1957.                         if(!(*Error = ParseIFF(Handle,IFFPARSE_SCAN)))
  1958.                         {
  1959.                             struct BitMapHeader BitMapHeader;
  1960.  
  1961.                             if(ReadChunkBytes(Handle,&BitMapHeader,sizeof(struct BitMapHeader)) == sizeof(struct BitMapHeader))
  1962.                             {
  1963.                                 *Width    = BitMapHeader . bmh_Width;
  1964.                                 *Height    = BitMapHeader . bmh_Height;
  1965.  
  1966.                                 Result    = TRUE;
  1967.                             }
  1968.                             else
  1969.                                 *Error = ERROR_SEEK_ERROR;
  1970.                         }
  1971.  
  1972.                         if(*Error == IFFERR_EOF || *Error == IFFERR_EOC)
  1973.                             *Error = 0;
  1974.                     }
  1975.  
  1976.                     CloseIFF(Handle);
  1977.                 }
  1978.  
  1979.                 CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
  1980.             }
  1981.             else
  1982.                 *Error = ERR_NO_MEM;
  1983.         }
  1984.         else
  1985.         {
  1986.             if(Handle -> iff_Stream = (ULONG)Open(Name,MODE_OLDFILE))
  1987.             {
  1988.                 InitIFFasDOS(Handle);
  1989.  
  1990.                 if(!(*Error = OpenIFF(Handle,IFFF_READ)))
  1991.                 {
  1992.                     if(!(*Error = StopChunk(Handle,ID_ILBM,ID_BMHD)))
  1993.                     {
  1994.                         if(!(*Error = ParseIFF(Handle,IFFPARSE_SCAN)))
  1995.                         {
  1996.                             struct BitMapHeader BitMapHeader;
  1997.  
  1998.                             if(ReadChunkBytes(Handle,&BitMapHeader,sizeof(struct BitMapHeader)) == sizeof(struct BitMapHeader))
  1999.                             {
  2000.                                 *Width    = BitMapHeader . bmh_Width;
  2001.                                 *Height    = BitMapHeader . bmh_Height;
  2002.  
  2003.                                 Result    = TRUE;
  2004.                             }
  2005.                             else
  2006.                                 *Error = IoErr();
  2007.                         }
  2008.  
  2009.                         if(*Error == IFFERR_EOF || *Error == IFFERR_EOC)
  2010.                             *Error = 0;
  2011.                     }
  2012.  
  2013.                     CloseIFF(Handle);
  2014.                 }
  2015.  
  2016.                 Close((BPTR)Handle -> iff_Stream);
  2017.             }
  2018.             else
  2019.                 *Error = IoErr();
  2020.         }
  2021.  
  2022.         FreeIFF(Handle);
  2023.     }
  2024.     else
  2025.         *Error = ERR_NO_MEM;
  2026.  
  2027.     return(Result);
  2028. }
  2029.  
  2030. STATIC BOOL __regargs
  2031. GetPicture_DPI(STRPTR Name,LONG *DPI_X,LONG *DPI_Y,LONG *Error)
  2032. {
  2033.     Object    *Picture;
  2034.     BOOL     Result = FALSE;
  2035.  
  2036.     *Error = 0;
  2037.  
  2038.     if(Picture = NewDTObject(Name,
  2039.         DTA_SourceType,    DTST_FILE,
  2040.         DTA_GroupID,    GID_PICTURE,
  2041.  
  2042.         PDTA_Remap,    FALSE,
  2043.     TAG_DONE))
  2044.     {
  2045.         if(DoMethod(Picture,DTM_PROCLAYOUT,NULL,TRUE))
  2046.         {
  2047.             struct BitMapHeader *BitMapHeader = NULL;
  2048.  
  2049.             if(GetDTAttrs(Picture,PDTA_BitMapHeader,&BitMapHeader,TAG_DONE))
  2050.             {
  2051.                 if(BitMapHeader)
  2052.                 {
  2053.                     if(BitMapHeader -> bmh_XAspect && BitMapHeader -> bmh_YAspect)
  2054.                         *DPI_Y = (*DPI_X * BitMapHeader -> bmh_XAspect) / BitMapHeader -> bmh_YAspect;
  2055.                 }
  2056.             }
  2057.         }
  2058.         else
  2059.             *Error = ERR_FILE_FORMAT_ERROR;
  2060.  
  2061.         DisposeDTObject(Picture);
  2062.     }
  2063.     else
  2064.         *Error = IoErr();
  2065.  
  2066.     return(Result);
  2067. }
  2068.  
  2069. STATIC BOOL __regargs
  2070. GetILBM_DPI(STRPTR Name,LONG *DPI_X,LONG *DPI_Y,LONG *Error)
  2071. {
  2072.     STATIC LONG Stops[] =
  2073.     {
  2074.         ID_ILBM,ID_BMHD,
  2075.         ID_ILBM,ID_DPI
  2076.     };
  2077.  
  2078.     struct IFFHandle    *Handle;
  2079.     BOOL             Result = FALSE;
  2080.     LONG             ClipID;
  2081.  
  2082.     *Error = 0;
  2083.  
  2084.     if(Handle = AllocIFF())
  2085.     {
  2086.         if(GetClipID(Name,&ClipID))
  2087.         {
  2088.             if(Handle -> iff_Stream = (ULONG)OpenClipboard(ClipID))
  2089.             {
  2090.                 InitIFFasClip(Handle);
  2091.  
  2092.                 if(!(*Error = OpenIFF(Handle,IFFF_READ)))
  2093.                 {
  2094.                     if(!(*Error = StopChunks(Handle,Stops,2)))
  2095.                     {
  2096.                         struct ContextNode    *Chunk;
  2097.                         struct BitMapHeader     BitMapHeader;
  2098.                         struct DPIHeader     DPIHeader;
  2099.  
  2100.                         while(!(*Error = ParseIFF(Handle,IFFPARSE_SCAN)))
  2101.                         {
  2102.                             Chunk = CurrentChunk(Handle);
  2103.  
  2104.                             switch(Chunk -> cn_ID)
  2105.                             {
  2106.                                 case ID_BMHD:
  2107.  
  2108.                                     if(ReadChunkBytes(Handle,&BitMapHeader,sizeof(struct BitMapHeader)) == sizeof(struct BitMapHeader))
  2109.                                     {
  2110.                                         if(BitMapHeader . bmh_XAspect && BitMapHeader . bmh_YAspect)
  2111.                                             *DPI_Y = (*DPI_X * BitMapHeader . bmh_XAspect) / BitMapHeader . bmh_YAspect;
  2112.                                     }
  2113.                                     else
  2114.                                         *Error = ERROR_SEEK_ERROR;
  2115.  
  2116.                                     break;
  2117.  
  2118.                                 case ID_DPI:
  2119.  
  2120.                                     if(ReadChunkBytes(Handle,&DPIHeader,sizeof(struct DPIHeader)) == sizeof(struct DPIHeader))
  2121.                                     {
  2122.                                         *DPI_X = DPIHeader . DPI_X;
  2123.                                         *DPI_Y = DPIHeader . DPI_Y;
  2124.                                     }
  2125.                                     else
  2126.                                         *Error = ERROR_SEEK_ERROR;
  2127.  
  2128.                                     break;
  2129.                             }
  2130.                         }
  2131.  
  2132.                         if(*Error == IFFERR_EOF || *Error == IFFERR_EOC)
  2133.                             *Error = 0;
  2134.                     }
  2135.  
  2136.                     CloseIFF(Handle);
  2137.                 }
  2138.  
  2139.                 CloseClipboard((struct ClipboardHandle *)Handle -> iff_Stream);
  2140.             }
  2141.             else
  2142.                 *Error = ERR_NO_MEM;
  2143.         }
  2144.         else
  2145.         {
  2146.             if(Handle -> iff_Stream = (ULONG)Open(Name,MODE_OLDFILE))
  2147.             {
  2148.                 InitIFFasDOS(Handle);
  2149.  
  2150.                 if(!(*Error = OpenIFF(Handle,IFFF_READ)))
  2151.                 {
  2152.                     if(!(*Error = StopChunks(Handle,Stops,2)))
  2153.                     {
  2154.                         struct ContextNode    *Chunk;
  2155.                         struct BitMapHeader     BitMapHeader;
  2156.                         struct DPIHeader     DPIHeader;
  2157.  
  2158.                         while(!(*Error = ParseIFF(Handle,IFFPARSE_SCAN)))
  2159.                         {
  2160.                             Chunk = CurrentChunk(Handle);
  2161.  
  2162.                             switch(Chunk -> cn_ID)
  2163.                             {
  2164.                                 case ID_BMHD:
  2165.  
  2166.                                     if(ReadChunkBytes(Handle,&BitMapHeader,sizeof(struct BitMapHeader)) == sizeof(struct BitMapHeader))
  2167.                                     {
  2168.                                         if(BitMapHeader . bmh_XAspect && BitMapHeader . bmh_YAspect)
  2169.                                             *DPI_Y = (*DPI_X * BitMapHeader . bmh_XAspect) / BitMapHeader . bmh_YAspect;
  2170.                                     }
  2171.                                     else
  2172.                                         *Error = ERROR_SEEK_ERROR;
  2173.  
  2174.                                     break;
  2175.  
  2176.                                 case ID_DPI:
  2177.  
  2178.                                     if(ReadChunkBytes(Handle,&DPIHeader,sizeof(struct DPIHeader)) == sizeof(struct DPIHeader))
  2179.                                     {
  2180.                                         *DPI_X = DPIHeader . DPI_X;
  2181.                                         *DPI_Y = DPIHeader . DPI_Y;
  2182.                                     }
  2183.                                     else
  2184.                                         *Error = ERROR_SEEK_ERROR;
  2185.  
  2186.                                     break;
  2187.                             }
  2188.                         }
  2189.  
  2190.                         if(*Error == IFFERR_EOF || *Error == IFFERR_EOC)
  2191.                             *Error = 0;
  2192.                     }
  2193.  
  2194.                     CloseIFF(Handle);
  2195.                 }
  2196.  
  2197.                 Close((BPTR)Handle -> iff_Stream);
  2198.             }
  2199.             else
  2200.                 *Error = IoErr();
  2201.         }
  2202.  
  2203.         FreeIFF(Handle);
  2204.     }
  2205.     else
  2206.         *Error = ERR_NO_MEM;
  2207.  
  2208.     return(Result);
  2209. }
  2210.  
  2211. VOID __regargs
  2212. DeleteImage(struct GreyImage *Image)
  2213. {
  2214.     if(Image)
  2215.     {
  2216.         LibDeletePool(Image -> Pool);
  2217.  
  2218.         FreeVecPooled(Image);
  2219.     }
  2220. }
  2221.  
  2222. struct GreyImage * __regargs
  2223. ReadImage(STRPTR Name,BOOL PatchColours,LONG *Error)
  2224. {
  2225.     struct GreyImage *Image;
  2226.  
  2227.     *Error = 0;
  2228.  
  2229.     if(AP_Application)
  2230.         set(AP_Application,MUIA_Application_Sleep,TRUE);
  2231.  
  2232.     if(!(Image = ReadILBM(Name,PatchColours,Error)))
  2233.     {
  2234.         if(DataTypesBase && !GetClipID(Name,NULL))
  2235.             Image = ReadPicture(Name,PatchColours,Error);
  2236.     }
  2237.  
  2238.     if(AP_Application)
  2239.         set(AP_Application,MUIA_Application_Sleep,FALSE);
  2240.  
  2241.     return(Image);
  2242. }
  2243.  
  2244. BOOL __regargs
  2245. GetImageSize(STRPTR Name,LONG *Width,LONG *Height,LONG *Error)
  2246. {
  2247.     BOOL Result;
  2248.  
  2249.     if(!(Result = GetILBMSize(Name,Width,Height,Error)))
  2250.     {
  2251.         if(DataTypesBase && !GetClipID(Name,NULL))
  2252.             Result = GetPictureSize(Name,Width,Height,Error);
  2253.     }
  2254.  
  2255.     return(Result);
  2256. }
  2257.  
  2258. BOOL __regargs
  2259. GetImageDPI(STRPTR Name,LONG *DPI_X,LONG *DPI_Y,LONG *Error)
  2260. {
  2261.     BOOL Result;
  2262.     LONG LocalError = 0;
  2263.  
  2264.     if(!Error)
  2265.         Error = &LocalError;
  2266.  
  2267.     if(!(Result = GetILBM_DPI(Name,DPI_X,DPI_Y,Error)))
  2268.     {
  2269.         if(DataTypesBase && !GetClipID(Name,NULL))
  2270.             Result = GetPicture_DPI(Name,DPI_X,DPI_Y,Error);
  2271.     }
  2272.  
  2273.     return(Result);
  2274. }
  2275.