home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / gfx / edit / tsmorph / jpeg_ls / jdmain.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  7KB  |  274 lines

  1. // This file has been completely rewritten
  2.  
  3. #include "jinclude.h"
  4.  
  5. #ifndef RENDER
  6. #ifndef TSMORPH_H
  7. #include "/TSMorph.h"
  8. #endif
  9. #else
  10. /* recognized file formats */
  11. extern UWORD FileFormat;
  12. #define FORMAT_IFF    1
  13. #define FORMAT_JPEG    2
  14. #define FORMAT_GIF    3
  15. #define FORMAT_PPM    4
  16. #define FORMAT_TARGA    5
  17. #include <intuition/intuition.h>
  18. #include <proto/gadtools.h>
  19. #include "/progress.h"
  20. void AddMessage(UBYTE *message);
  21. #endif
  22.  
  23. #include <proto/exec.h>
  24.  
  25. #include <setjmp.h>
  26.  
  27. static jmp_buf setjmp_buffer;    /* for return to caller */
  28.  
  29. static struct External_methods_struct e_methods;
  30. static struct Decompress_info_struct cinfo;
  31. static struct Decompress_methods_struct dc_methods;
  32.  
  33. extern UBYTE *r1=NULL,*g1=NULL,*b1=NULL;
  34.  
  35. METHODDEF void
  36. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  37. {
  38. #ifndef RENDER
  39.     if (ProgressWnd) {
  40.       GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass2],ProgressWnd,NULL,
  41.                                     GTSL_Level,(ULONG)loopcounter,
  42.                                     GTSL_Max,(ULONG)looplimit-1,
  43.                                     TAG_END);
  44.       GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass1],ProgressWnd,NULL,
  45.                                     GTSL_Level,(ULONG)cinfo->completed_passes+1,
  46.                                     GTSL_Max,(ULONG)cinfo->total_passes +
  47.                                                  (cinfo->quantize_colors?0:1),
  48.                                     TAG_END);
  49.         HandleProgressIDCMP();
  50.     }
  51. #endif
  52. }
  53.  
  54. METHODDEF void
  55. trace_message(const char *msgtext)
  56. {
  57.     char text[512];
  58.     sprintf(text,msgtext,e_methods.message_parm[0],e_methods.message_parm[1],e_methods.message_parm[2],
  59.                 e_methods.message_parm[3],e_methods.message_parm[4],e_methods.message_parm[5],
  60.                 e_methods.message_parm[6],e_methods.message_parm[7]);
  61. #ifndef RENDER
  62.     if (ProgressWnd) {
  63.          GT_SetGadgetAttrs(ProgressGadgets[GDX_Mess],ProgressWnd,NULL,
  64.                               GTTX_Text,(ULONG)text,
  65.                              TAG_END);
  66.         HandleProgressIDCMP();
  67.     }
  68. #else
  69.     AddMessage(text);
  70. #endif
  71. }
  72.  
  73. METHODDEF void
  74. error_exit (const char *msgtext)
  75. {
  76.   (*e_methods.free_all) ();    /* clean up memory allocation & temp files */
  77.   longjmp(setjmp_buffer, 1);    /* return control to outer routine */
  78. }
  79.  
  80. /*
  81.  * This routine gets control after the input file header has been read.
  82.  * It must determine what output file format is to be written,
  83.  * and make any other decompression parameter changes that are desirable.
  84.  */
  85.  
  86. METHODDEF void
  87. d_ui_method_selection (decompress_info_ptr cinfo)
  88. {
  89. }
  90.  
  91. static BOOL
  92. LoadOther(UBYTE **p) {
  93.     UBYTE *pixel_row[3];
  94.     UWORD i;
  95.     if (FileFormat == FORMAT_GIF) {
  96.         jselrgif(&cinfo);
  97.     }
  98.     else {
  99.         if (FileFormat == FORMAT_PPM) {
  100.             jselrppm(&cinfo);
  101.         }
  102.         else {
  103.             jselrtarga(&cinfo);
  104.         }
  105.     }
  106. #ifndef RENDER
  107.     if (ProgressWnd) {
  108.       GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass1],ProgressWnd,NULL,
  109.                                     GTSL_Level,1,
  110.                                     GTSL_Max,2,
  111.                                     TAG_END);
  112.         HandleProgressIDCMP();
  113.     }
  114. #endif
  115.     (*cinfo.methods->input_init)(&cinfo);
  116. #ifndef RENDER
  117.     if (ProgressWnd) {
  118.       GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass2],ProgressWnd,NULL,
  119.                                     GTSL_Level,0,
  120.                                     GTSL_Max,cinfo.image_height-1,
  121.                                     TAG_END);
  122.         HandleProgressIDCMP();
  123.     }
  124. #endif
  125.   if (!cinfo.quantize_colors) {
  126.     if (cinfo.output_file = (BPTR)AllocVec((((cinfo.image_width+15)>>4)<<4) * cinfo.image_height * 3,NULL)) {
  127.       pixel_row[0] = (UBYTE *)cinfo.output_file;
  128.       pixel_row[1] = pixel_row[0] + (((cinfo.image_width+15)>>4)<<4) * cinfo.image_height;
  129.       pixel_row[2] = pixel_row[1] + (((cinfo.image_width+15)>>4)<<4) * cinfo.image_height;
  130.     }
  131.     else {
  132.         return FALSE;
  133.     }
  134.   }
  135.   else {
  136.     if (p[0] = AllocVec((((cinfo.image_width+15)>>4)<<4)*cinfo.image_height*3,0)) {
  137.       p[1] = p[0] + (((cinfo.image_width+15)>>4)<<4)*cinfo.image_height;
  138.       p[2] = p[1] + (((cinfo.image_width+15)>>4)<<4)*cinfo.image_height;
  139.       pixel_row[0] = p[0];
  140.       pixel_row[1] = p[1];
  141.       pixel_row[2] = p[2];
  142.     }
  143.     else {
  144.       return FALSE;
  145.     }
  146.   }
  147.      for (i = 0;
  148.            i < cinfo.image_height;
  149.            i++) {
  150. #ifndef RENDER
  151.             if (ProgressWnd) {
  152.               GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass2],ProgressWnd,NULL,
  153.                                             GTSL_Level,i,
  154.                                             TAG_END);
  155.                 HandleProgressIDCMP();
  156.             }
  157. #endif
  158.             (*cinfo.methods->get_input_row)(&cinfo,pixel_row);
  159.             pixel_row[0] += (((cinfo.image_width+15)>>4)<<4);
  160.             pixel_row[1] += (((cinfo.image_width+15)>>4)<<4);
  161.             pixel_row[2] += (((cinfo.image_width+15)>>4)<<4);
  162.     }
  163.     (*cinfo.methods->input_term)(&cinfo);
  164. #ifndef RENDER
  165.     if (ProgressWnd) {
  166.       GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass1],ProgressWnd,NULL,
  167.                                     GTSL_Level,2,
  168.                                     TAG_END);
  169.         HandleProgressIDCMP();
  170.     }
  171. #endif
  172.     return TRUE;
  173. }
  174.  
  175. extern decompress_info_ptr
  176. LoadJPEG(char *filename,UWORD colors,UBYTE *r,UBYTE *g,UBYTE *b,UBYTE **p) {
  177.  
  178.   r1=r;
  179.   g1=g;
  180.   b1=b;
  181.  
  182.   if (!(cinfo.input_file = Open(filename,MODE_OLDFILE))) {
  183.     return NULL;
  184.   }
  185.  
  186.   cinfo.out_color_space = CS_RGB;
  187.  
  188.   cinfo.output_file = NULL;
  189.  
  190.   cinfo.methods = &dc_methods;
  191.   cinfo.emethods = &e_methods;
  192.  
  193.   jselwmem(&cinfo);
  194.  
  195.   e_methods.error_exit = error_exit; /* supply error-exit routine */
  196.   e_methods.trace_message = trace_message; /* supply trace-message routine */
  197.   e_methods.trace_level = 1;    /* default = no tracing */
  198.   e_methods.num_warnings = 0;    /* no warnings emitted yet */
  199.   e_methods.first_warning_level = 0; /* display first corrupt-data warning */
  200.   e_methods.more_warning_level = 3; /* but suppress additional ones */
  201.  
  202.   if (setjmp(setjmp_buffer)) {
  203.     Close(cinfo.input_file);
  204.     if (cinfo.output_file) {
  205.       FreeVec((void *)cinfo.output_file);
  206.       cinfo.output_file = NULL;
  207.     }
  208.     if (p) {
  209.       if (p[0]) {
  210.         FreeVec(p[0]);
  211.         p[0] = NULL;
  212.       }
  213.      p[1] = NULL;
  214.       p[2] = NULL;
  215.      }
  216.     return NULL;
  217.   }
  218.  
  219.   jselmemmgr(cinfo.emethods);    /* memory allocation routines */
  220.  
  221.   cinfo.methods->d_ui_method_selection = d_ui_method_selection;
  222.  
  223.   j_d_defaults(&cinfo, TRUE);
  224.  
  225.   jselrjfif(&cinfo);
  226.  
  227.   if (colors) {
  228.     cinfo.desired_number_of_colors = colors;
  229.     cinfo.quantize_colors = TRUE;
  230.   }
  231.   else {
  232.     cinfo.quantize_colors = FALSE;
  233.   }
  234.   cinfo.use_dithering = FALSE;
  235.   cinfo.two_pass_quantize = FALSE;
  236.   cinfo.methods->progress_monitor = progress_monitor;
  237.  
  238.   /* Here we go! */
  239.   if (FileFormat == FORMAT_JPEG) {
  240.       jpeg_decompress(&cinfo);
  241. #ifndef RENDER
  242.       if (!colors) {
  243.           GT_SetGadgetAttrs(ProgressGadgets[GDX_Pass1],ProgressWnd,NULL,
  244.                                     GTSL_Level,(ULONG)cinfo.total_passes + 1,
  245.                                     TAG_END);
  246.             }
  247. #endif
  248.   }
  249.   else {
  250.      if (!LoadOther(p)) {
  251.           (*e_methods.free_all) ();    /* clean up memory allocation & temp files */
  252.           Close(cinfo.input_file);
  253.           if (cinfo.output_file) {
  254.             FreeVec((void *)cinfo.output_file);
  255.             cinfo.output_file = NULL;
  256.           }
  257.           if (p) {
  258.               if (p[0]) {
  259.                     FreeVec(p[0]);
  260.                 p[0] = NULL;
  261.             }
  262.              p[1] = NULL;
  263.              p[2] = NULL;
  264.           }
  265.           return NULL;
  266.      }
  267.   }
  268.  
  269.   (*e_methods.free_all) ();    /* clean up memory allocation & temp files */
  270.   Close(cinfo.input_file);
  271.  
  272.   return &cinfo;
  273. }
  274.