home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / gfx / edit / tsmorph / opalload.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  37KB  |  1,202 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. //    $Author: M_J_Paddock $
  21. //    $Date: 1992/08/08 01:09:24 $
  22. //    $Revision: 1.5 $
  23.  
  24. // Include precompiled headers if required
  25. #ifndef TSMORPH_H
  26. #include "TSMorph.h"
  27. #endif
  28.  
  29. #include "JPEG_LS/jinclude.h"
  30.  
  31. extern UWORD FileFormat = 0;    // Format of file being loaded
  32.  
  33. /* Loads an image (in various formats)
  34.  * pic     : pointer to Picture structure
  35.  * filename: filename to load
  36.  * Returns : TRUE if loaded OK
  37.  */
  38. BOOL
  39. MyLoadBrush (struct Picture *pic,UBYTE *filename) {
  40.     BOOL OpenILBM = FALSE;    // Are we loading a (<24 plane) ILBM
  41.     char *e = NULL;            // First
  42.     char *e1 = NULL;            // and second part of error message
  43.     long hnum;                    // Help number
  44.     long err;                    // Error flag
  45.     BPTR fh;                        // File handle
  46.     UBYTE buffer[5]="\0\0\0\0";    // First 4 bytes of file for identification
  47.  
  48.     // Try and open file and read 1st 4 bytes
  49.     if (fh = Open(filename,MODE_OLDFILE)) {
  50.         FRead(fh,buffer,4,1);
  51.         // check if a sort of IFF file
  52.         if (!strcmp(buffer,"LIST") ||
  53.              !strcmp(buffer,"CAT ") ||
  54.              !strcmp(buffer,"FORM")) {
  55.             FileFormat = FORMAT_IFF;
  56.         }
  57.         else {
  58.             // check if a JFIF JPEG
  59.             if ((buffer[0] == 0xFF) &&
  60.                  (buffer[1] == 0xD8)) {
  61.                 FileFormat = FORMAT_JPEG;
  62.             }
  63.             else {
  64.                 // check if a GIF
  65.                 if ((buffer[0] == 'G') &&
  66.                      (buffer[1] == 'I') &&
  67.                      (buffer[2] == 'F')) {
  68.                     FileFormat = FORMAT_GIF;
  69.                 }
  70.                 else {
  71.                     // check if a PPM
  72.                     if ((buffer[0] == 'P') &&
  73.                          ((buffer[1] == '2') ||
  74.                           (buffer[1] == '3') ||
  75.                           (buffer[1] == '5') ||
  76.                           (buffer[1] == '6'))) {
  77.                         FileFormat = FORMAT_PPM;
  78.                     }
  79.                     else {
  80.                         // Otherwise default to TARGA (the only other format we load)
  81.                         FileFormat = FORMAT_TARGA;
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         Close(fh);
  87.     }
  88.     else {
  89.         // error file not found
  90.         FileFormat = 0;
  91.         e = "File does not exist '%s'";
  92.         hnum = HE_NoFile;
  93.         e1 = filename;
  94.     }
  95.     // If the file is and IFF and we only open ILBMs in some cases then perform some checks
  96.     if ((FileFormat == FORMAT_IFF) && ((OpenMode == OPEN_ILBM_IF_ILBM) || (OpenMode == OPEN_ILBM_IF_COLOURS))) {
  97.         if (pic->ilbm->ParseInfo.iff = AllocIFF()) {
  98.             err = queryilbm(pic->ilbm,filename);
  99.             if (!err) {
  100.                 if (OpenMode == OPEN_ILBM_IF_COLOURS) {
  101.                     // if we only open if screen colours then check depth
  102.                     if (!(pic->ilbm->Bmhd.nPlanes > TSMorphWnd->WScreen->BitMap.Depth)) {
  103.                         OpenILBM = TRUE;
  104.                     }
  105.                 }
  106.                 else {    // OpenMode == OPEN_ILBM_IF_ILBM
  107.                     // if open if an ILBM then check < 24 bits
  108.                     if (pic->ilbm->Bmhd.nPlanes < 24) {
  109.                         OpenILBM = TRUE;
  110.                     }
  111.                 }
  112.             }
  113.             FreeIFF(pic->ilbm->ParseInfo.iff);
  114.             pic->ilbm->ParseInfo.iff = NULL;
  115.         }
  116.         else {
  117.             e = "Unable to AllocIFF";
  118.             hnum = HE_AllocIFF;
  119.         }
  120.     }
  121.     // If we always open ILBM or the above checks out OK then try and open
  122.     if (OpenILBM || ((OpenMode == OPEN_ILBM_ALWAYS) && !e)) {
  123.         if (pic->ilbm->ParseInfo.iff = AllocIFF()) {
  124.             // set up IFF stuff and load image
  125.             pic->ilbm->ParseInfo.propchks = props;
  126.             pic->ilbm->ParseInfo.collectchks = nowt;
  127.             pic->ilbm->ParseInfo.stopchks = stops;
  128.             if (loadbrush(pic->ilbm,filename)) {
  129.                 closeifile(&(pic->ilbm->ParseInfo));
  130.                 e = "Failure loading Image '%s'";
  131.                 e1 = filename;
  132.                 hnum = HE_LoadImage;
  133.             }
  134.             else {
  135.                 closeifile(&(pic->ilbm->ParseInfo));
  136.                 FreeIFF(pic->ilbm->ParseInfo.iff);
  137.                 pic->ilbm->ParseInfo.iff = NULL;
  138.                 return TRUE;
  139.             }
  140.         }
  141.         else {
  142.             e = "Unable to AllocIFF";
  143.             hnum = HE_AllocIFF;
  144.         }
  145.     }
  146.     else {
  147.         // otherwise try and load using another format
  148.         if (!e) {
  149.             return OpalLoad(pic,filename);
  150.         }
  151.     }
  152.     Error(e,"OK",e1,hnum);
  153.     return FALSE;
  154. }
  155.  
  156. /* Loads an image (in various formats)
  157.  * pic     : pointer to Picture structure
  158.  * filename: filename to load
  159.  * Returns : TRUE if loaded OK
  160.  */
  161. BOOL OpalLoad(struct Picture *pic,UBYTE *filename) {
  162.     char                     *e        = NULL;    // Error message main text
  163.     char                    *e1     = NULL;    // sub text,
  164.     long                     Err;                // OpalError
  165.     ULONG                    hnum;                // Help on error
  166.     struct OpalScreen    *OScrn;            // OpalScreen
  167.     UBYTE                    *p[3]= {NULL,NULL,NULL};    // rbg planes
  168.     UBYTE r[256],g[256],b[256];        // rgb colors of screen (8 bit)
  169.     UWORD                    i;                    // loop counter
  170.     UWORD                 color;            // Colour
  171.     UBYTE *red,*green,*blue;            // rgb pointers
  172.     UWORD                    maxcol;            // Number of colors
  173.     UWORD xadd;                                // bytes to add to get to next line
  174.     decompress_info_ptr info;            // Load JPEG stuff
  175.     BOOL                    ReMap = FALSE;    // Have to remap ourselves
  176.     struct RastPort    Rp;                // Rast port for conversion
  177.     struct DCTVCvtHandle    *handle=NULL;    // DCTV conversion stuff
  178.     UWORD                    *DCTVcolors=NULL;    // DCTV palette
  179.  
  180.     OpenProgressWindow();        // Open the progress window
  181.     if (ProgressWnd) {
  182.          GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  183.                               GTTX_Text,(ULONG)"Creating Colormap",
  184.                              TAG_END);
  185.         HandleProgressIDCMP();
  186.     }
  187.     // Store and set up to 256 colours
  188.       maxcol = min((1 << TSMorphWnd->WScreen->BitMap.Depth),256);
  189.     for (i=0; i < maxcol; ++i) {
  190.         color = GetRGB4(TSMorphWnd->WScreen->ViewPort.ColorMap,i);
  191.         r[i]=((color&0x0f00)>>4)|((color&0x0f00)>>8);
  192.         g[i]=(color&0x00f0)|((color&0x00f0)>>4);
  193.         b[i]=((color&0x000f)<<4)|(color&0x000f);
  194.     }
  195.     // If we are not opening OPAL and the file is IFF
  196.     if ((OpenMode != OPEN_OPAL) &&
  197.          (FileFormat == FORMAT_IFF)) {
  198.         // try and load IFF
  199.         if (pic->ilbm->ParseInfo.iff = AllocIFF()) {
  200.             pic->ilbm->ParseInfo.propchks = props;
  201.             pic->ilbm->ParseInfo.collectchks = nowt;
  202.             pic->ilbm->ParseInfo.stopchks = stops;
  203.             if (ProgressWnd) {
  204.                  GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  205.                                       GTTX_Text,(ULONG)"Loading ILBM",
  206.                                      TAG_END);
  207.                 HandleProgressIDCMP();
  208.             }
  209.             if (loadbrush(pic->ilbm,filename)) {
  210.                 closeifile(&(pic->ilbm->ParseInfo));
  211.                 e = "Failure loading Image '%s'";
  212.                 e1 = filename;
  213.                 hnum = HE_LoadImage;
  214.             }
  215.             else {
  216.                 closeifile(&(pic->ilbm->ParseInfo));
  217.                 if (pic->ilbm->Bmhd.nPlanes == 24) {
  218.                     // 24 bit image - free colours
  219.                     freecolors(pic->ilbm);
  220.                     if (ProgressWnd) {
  221.                          GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  222.                                               GTTX_Text,(ULONG)"Converting 24 bit to Chunky",
  223.                                              TAG_END);
  224.                         HandleProgressIDCMP();
  225.                     }
  226.                     // Allocate Chunky planes
  227.                     if ((p[0] = AllocVec(((((pic->ilbm->Bmhd.w+15)>>4)<<4)*pic->ilbm->Bmhd.h),0)) &&
  228.                          (p[1] = AllocVec(((((pic->ilbm->Bmhd.w+15)>>4)<<4)*pic->ilbm->Bmhd.h),0)) &&
  229.                          (p[2] = AllocVec(((((pic->ilbm->Bmhd.w+15)>>4)<<4)*pic->ilbm->Bmhd.h),0))) {
  230.                         // Convert to chunky RGB
  231.                         if (InitArray(pic->ilbm->Bmhd.w) && PlanarToChunky(pic,pic->ilbm->Bmhd.w,pic->ilbm->Bmhd.h,p[0],p[1],p[2])) {
  232.                             // Bytes to add to get to next row
  233.                             xadd = (((pic->ilbm->Bmhd.w+15)>>4)<<4) - pic->ilbm->Bmhd.w;
  234.                             red = p[0];
  235.                             blue = p[2];
  236.                             green = p[1];
  237.                             if (ProgressWnd) {
  238.                                 GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  239.                                                        GTTX_Text,(ULONG)"Remapping 24 bit palette",
  240.                                                       TAG_END);
  241.                                 GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass2],ProgressWnd,NULL,
  242.                                                      GTSL_Level,0,
  243.                                                       GTSL_Max,pic->ilbm->Bmhd.h-1,TAG_END);
  244.                                 HandleProgressIDCMP();
  245.                              }
  246.                              // Convert to screen palette using 020 if present
  247.                             if (((struct ExecBase *)SysBase)->AttnFlags & AFF_68020) {
  248.                                 RGBToScreen020(red,green,blue,pic->ilbm->Bmhd.h,pic->ilbm->Bmhd.w,maxcol,xadd,r,g,b);
  249.