home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / gfx / edit / tsmorph / src / opalload.c < prev    next >
C/C++ Source or Header  |  1994-02-26  |  49KB  |  1,579 lines

  1. // TSMorph - Amiga Morphing program
  2. // Copyright (C) © 1993  Topicsave Limited
  3.  
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // any later version.
  8.  
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. // GNU General Public License for more details.
  13.  
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. // mpaddock@cix.compulink.co.uk
  19.  
  20. // Include precompiled headers if required
  21. #ifndef TSMORPH_H
  22. #include "TSMorph.h"
  23. #endif
  24.  
  25. #include "JPEG_LS/jinclude.h"
  26.  
  27. extern UWORD FileFormat = 0;    // Format of file being loaded
  28.  
  29. /* Loads an image (in various formats)
  30.  * pic     : pointer to Picture structure
  31.  * filename: filename to load
  32.  * Returns : TRUE if loaded OK
  33.  */
  34. BOOL
  35. MyLoadBrush (struct Picture *pic,UBYTE *filename) {
  36.     BOOL OpenILBM = FALSE;    // Are we loading a (<24 plane) ILBM
  37.     char *e = NULL;            // First
  38.     char *e1 = NULL;            // and second part of error message
  39.     long hnum;                    // Help number
  40.     long err;                    // Error flag
  41.     BPTR fh;                        // File handle
  42.     UBYTE buffer[5]="\0\0\0\0";    // First 4 bytes of file for identification
  43.  
  44.     // Try and open file and read 1st 4 bytes
  45.     if (fh = Open(filename,MODE_OLDFILE)) {
  46.         FRead(fh,buffer,4,1);
  47.         // check if a sort of IFF file
  48.         if (!strcmp(buffer,"LIST") ||
  49.              !strcmp(buffer,"CAT ") ||
  50.              !strcmp(buffer,"FORM")) {
  51.             FileFormat = FORMAT_IFF;
  52.         }
  53.         else {
  54.             // check if a JFIF JPEG
  55.             if ((buffer[0] == 0xFF) &&
  56.                  (buffer[1] == 0xD8)) {
  57.                 FileFormat = FORMAT_JPEG;
  58.             }
  59.             else {
  60.                 // check if a GIF
  61.                 if ((buffer[0] == 'G') &&
  62.                      (buffer[1] == 'I') &&
  63.                      (buffer[2] == 'F')) {
  64.                     FileFormat = FORMAT_GIF;
  65.                 }
  66.                 else {
  67.                     // check if a PPM
  68.                     if ((buffer[0] == 'P') &&
  69.                          ((buffer[1] == '2') ||
  70.                           (buffer[1] == '3') ||
  71.                           (buffer[1] == '5') ||
  72.                           (buffer[1] == '6'))) {
  73.                         FileFormat = FORMAT_PPM;
  74.                     }
  75.                     else {
  76.                         // Otherwise default to TARGA (the only other format we load)
  77.                         FileFormat = FORMAT_TARGA;
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.         Close(fh);
  83.     }
  84.     else {
  85.         // error file not found
  86.         FileFormat = 0;
  87.         e = "File does not exist '%s'";
  88.         hnum = HE_NoFile;
  89.         e1 = filename;
  90.     }
  91.     // If the file is and IFF and we only open ILBMs in some cases then perform some checks
  92.     if ((FileFormat == FORMAT_IFF) && ((OpenMode == OPEN_ILBM_IF_ILBM) || (OpenMode == OPEN_ILBM_IF_COLOURS))) {
  93.         if (pic->ilbm->ParseInfo.iff = AllocIFF()) {
  94.             err = queryilbm(pic->ilbm,filename);
  95.             if (!err) {
  96.                 if (OpenMode == OPEN_ILBM_IF_COLOURS) {
  97.                     // if we only open if screen colours then check depth
  98.                     if (!(pic->ilbm->Bmhd.nPlanes > TSMorphWnd->WScreen->BitMap.Depth)) {
  99.                         OpenILBM = TRUE;
  100.                     }
  101.                 }
  102.                 else {    // OpenMode == OPEN_ILBM_IF_ILBM
  103.                     // if open if an ILBM then check < 24 bits
  104.                     if (pic->ilbm->Bmhd.nPlanes < 24) {
  105.                         OpenILBM = TRUE;
  106.                     }
  107.                 }
  108.             }
  109.             FreeIFF(pic->ilbm->ParseInfo.iff);
  110.             pic->ilbm->ParseInfo.iff = NULL;
  111.         }
  112.         else {
  113.             e = "Unable to AllocIFF";
  114.             hnum = HE_AllocIFF;
  115.         }
  116.     }
  117.     // If we always open ILBM or the above checks out OK then try and open
  118.     if (!EGS && (OpenILBM || ((OpenMode == OPEN_ILBM_ALWAYS) && !e))) {
  119.         if (pic->ilbm->ParseInfo.iff = AllocIFF()) {
  120.             // set up IFF stuff and load image
  121.             pic->ilbm->ParseInfo.propchks = props;
  122.             pic->ilbm->ParseInfo.collectchks = nowt;
  123.             pic->ilbm->ParseInfo.stopchks = stops;
  124.             if (loadbrush(pic->ilbm,filename)) {
  125.                 closeifile(&(pic->ilbm->ParseInfo));
  126.                 e = "Failure loading Image '%s'";
  127.                 e1 = filename;
  128.                 hnum = HE_LoadImage;
  129.             }
  130.             else {
  131.                 closeifile(&(pic->ilbm->ParseInfo));
  132.                 FreeIFF(pic->ilbm->ParseInfo.iff);
  133.                 pic->ilbm->ParseInfo.iff = NULL;
  134.                 return TRUE;
  135.             }
  136.         }
  137.         else {
  138.             e = "Unable to AllocIFF";
  139.             hnum = HE_AllocIFF;
  140.         }
  141.     }
  142.     else {
  143.         // otherwise try and load using another format
  144.         if (!e) {
  145.             return OpalLoad(pic,filename);
  146.         }
  147.     }
  148.     Error(e,"OK",e1,hnum);
  149.     return FALSE;
  150. }
  151.  
  152. /* Loads an image (in various formats)
  153.  * pic     : pointer to Picture structure
  154.  * filename: filename to load
  155.  * Returns : TRUE if loaded OK
  156.  */
  157. BOOL OpalLoad(struct Picture *pic,UBYTE *filename) {
  158.     char                     *e        = NULL;    // Error message main text
  159.     char                    *e1     = NULL;    // sub text,
  160.     long                     Err;                // OpalError
  161.     ULONG                    hnum;                // Help on error
  162.     struct OpalScreen    *OScrn;            // OpalScreen
  163.     UBYTE                    *p[3]= {NULL,NULL,NULL};    // rbg planes
  164.     UBYTE r[256],g[256],b[256];        // rgb colors of screen (8 bit)
  165.     ULONG                 col[4];            // Table for get RGB32
  166.     UWORD                    i,j;                // loop counters
  167.     UBYTE                    *plane;            // EGB bit map byte pointer
  168.     UWORD                 color;            // Colour
  169.     UBYTE *red,*green,*blue;            // rgb pointers
  170.     UWORD                    maxcol;            // Number of colors
  171.     UWORD xadd;                                // bytes to add to get to next line
  172.     decompress_info_ptr info;            // Load JPEG stuff
  173.     BOOL                    ReMap = FALSE;    // Have to remap ourselves
  174.     struct RastPort    Rp;                // Rast port for conversion
  175.     struct DCTVCvtHandle    *handle=NULL;    // DCTV conversion stuff
  176.     UWORD                    *DCTVcolors;    // DCTV palette
  177.  
  178.     OpenProgressWindow();        // Open the progress window
  179.     if (ProgressWnd) {
  180.          GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  181.                               GTTX_Text,(ULONG)"Creating Colormap",
  182.                              TAG_END);
  183.         HandleProgressIDCMP();
  184.     }
  185.     // Store and set up to 256 colours
  186.       maxcol = min((1 << TSMorphWnd->WScreen->BitMap.Depth),256);
  187.     if (GfxBase->lib_Version > 38) {
  188.         for (i=0; i < maxcol; ++i) {
  189.             GetRGB32(TSMorphWnd->WScreen->ViewPort.ColorMap,i,1,col);
  190.             r[i]=(col[0]>>24);
  191.             g[i]=(col[1]>>24);
  192.             b[i]=(col[2]>>24);
  193.         }
  194.     }
  195.     else {
  196.         for (i=0; i < maxcol; ++i) {
  197.             color = GetRGB4(TSMorphWnd->WScreen->ViewPort.ColorMap,i);
  198.             r[i]=((color&0x0f00)>>4)|((color&0x0f00)>>8);
  199.             g[i]=(color&0x00f0)|((color&0x00f0)>>4);
  200.             b[i]=((color&0x000f)<<4)|(color&0x000f);
  201.         }
  202.     }
  203.     // If we are not opening OPAL and the file is IFF
  204.     if ((OpenMode != OPEN_OPAL) &&
  205.          (FileFormat == FORMAT_IFF)) {
  206.         // try and load IFF
  207.         if (pic->ilbm->ParseInfo.iff = AllocIFF()) {
  208.             pic->ilbm->ParseInfo.propchks = props;
  209.             pic->ilbm->ParseInfo.collectchks = nowt;
  210.             pic->ilbm->ParseInfo.stopchks = stops;
  211.             if (ProgressWnd) {
  212.                  GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  213.                                       GTTX_Text,(ULONG)"Loading ILBM",
  214.                                      TAG_END);
  215.                 HandleProgressIDCMP();
  216.             }
  217.             if (loadbrush(pic->ilbm,filename)) {
  218.                 closeifile(&(pic->ilbm->ParseInfo));
  219.                 e = "Failure loading Image '%s'";
  220.                 e1 = filename;
  221.                 hnum = HE_LoadImage;
  222.             }
  223.             else {
  224.                 closeifile(&(pic->ilbm->ParseInfo));
  225.                 if (pic->ilbm->Bmhd.nPlanes == 24) {
  226.                     // 24 bit image - free colours
  227.                     freecolors(pic->ilbm);
  228.                     if (ProgressWnd) {
  229.                          GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  230.                                               GTTX_Text,(ULONG)"Converting 24 bit to Chunky",
  231.                                              TAG_END);
  232.                         HandleProgressIDCMP();
  233.                     }
  234.                     // Allocate Chunky planes
  235.                     if ((p[0] = AllocVec(((((pic->ilbm->Bmhd.w+15)>>4)<<4)*pic->ilbm->Bmhd.h),0)) &&
  236.                          (p[1] = AllocVec(((((pic->ilbm->Bmhd.w+15)>>4)<<4)*pic->ilbm->Bmhd.h),0)) &&
  237.                        (p[2] = AllocVec(((((pic->ilbm->Bmhd.w+15)>>4)<<4)*pic->ilbm->Bmhd.h),0))) {
  238.                       // Convert to chunky RGB
  239.                       if (InitArray(pic->ilbm->Bmhd.w) && PlanarToChunky(pic,pic->ilbm->Bmhd.w,pic->ilbm->Bmhd.h,p[0],p[1],p[2])) {
  240.                           // Bytes to add to get to next row
  241.                             xadd = (((pic->ilbm->Bmhd.w+15)>>4)<<4) - pic->ilbm->Bmhd.w;
  242.                             red = p[0];
  243.                             blue = p[2];
  244.                             green = p[1];
  245.                                 if (EGS) {
  246.                                     // Convert to an EGS BitMap
  247.                                 if (pic->EGS_BitMap = E_AllocBitMap(pic->ilbm->Bmhd.w,pic->ilbm->Bmhd.h,24,E_PIXELMAP,E_EB_NOTSWAPABLE,NULL)) {
  248.                                     plane = pic->EGS_BitMap->Typekey.PixelMap.Planes.Dest;
  249.                                     if (ProgressWnd) {
  250.                                          GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  251.                                                               GTTX_Text,(ULONG)"Converting to EGS",
  252.