home *** CD-ROM | disk | FTP | other *** search
/ Computer Panoráma / computer_panorama_1997-12-hibas.iso / SHARE / GRAPH / PTC051.ZIP / SRC / CONVERT.H < prev    next >
C/C++ Source or Header  |  1997-09-21  |  13KB  |  464 lines

  1. //////////////////////
  2. // converter module //
  3. //////////////////////
  4.  
  5. #ifndef __CONVERT_H
  6. #define __CONVERT_H
  7.  
  8. #include "misc.h"
  9. #include "lang.h"
  10. #include "globals.h"
  11. #include "config.h"
  12. #include "format.h"
  13. #include "color.h"
  14. #include "list.h"
  15.  
  16. #include "x86_8.h"
  17. #include "x86_16.h"
  18. #include "x86_32.h"
  19. #include "x86_fake.h"
  20. #include "x86_copy.h"
  21.  
  22. #include "mmx_8.h"
  23. #include "mmx_16.h"
  24. #include "mmx_32.h"
  25. #include "mmx_fake.h"
  26. #include "mmx_copy.h"
  27.  
  28. #include "cpp_8.h"
  29. #include "cpp_16.h"
  30. #include "cpp_32.h"
  31. #include "cpp_copy.h"
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. class CONVERTER
  41. {
  42.     private:
  43.  
  44.         // converter handle
  45.         class HANDLE
  46.         {
  47.             public:
  48.  
  49.                 // setup
  50.                 HANDLE();
  51.                 HANDLE(int src_id,int dest_id,int iextra=0,int icleanup=0);
  52.                 ~HANDLE();
  53.  
  54.                 // initialize
  55.                 void init(int src_id,int dest_id,int iextra=0,int icleanup=0);
  56.  
  57.                 // data
  58.                 FORMAT *src;
  59.                 FORMAT *dest;
  60.                 int extra;
  61.                 int copy;
  62.                 int cleanup;
  63.                 void (*Convert)(void *src,void *dest,uint pixels,void *extra); 
  64.                 void (*AreaConvert)(void *src,void *dest,uint width,uint height,uint dest_advance,uint src_advance,void *extra);
  65.                 void (*PartialConvert)(void *src,void *dest,int start_byte,int stop_byte,void *extra);
  66.         };
  67.  
  68.     public:
  69.         
  70.         // setup
  71.         CONVERTER();
  72.         ~CONVERTER();
  73.  
  74.         // basic conversion
  75.         inline int Convert(void *src,FORMAT const &src_format,void *dest,FORMAT const &dest_format,uint pixels,void *extra); 
  76.         inline int AreaConvert(void *src,FORMAT const &src_format,void *dest,FORMAT const &dest_format,uint width,uint height,uint src_advance,uint dest_advance,void *extra); 
  77.         inline int PartialConvert(void *src,FORMAT const &src_format,void *dest,FORMAT const &dest_format,int start_byte,int stop_byte,void *extra);
  78.  
  79.         // conversion routine access
  80.         inline HANDLE* RequestHandle(int src_id,int dest_id);
  81.         inline HANDLE* RequestHandle(FORMAT const &src,FORMAT const &dest);
  82.         inline void FreeHandle(HANDLE* converter);
  83.  
  84.         // conversion via handle
  85.         inline int Convert(HANDLE *handle,void *src,void *dest,uint pixels,void *extra); 
  86.         inline int AreaConvert(HANDLE *handle,void *src,void *dest,uint width,uint height,uint src_advance,uint dest_advance,void *extra); 
  87.         inline int PartialConvert(HANDLE *handle,void *src,void *dest,int start_byte,int stop_byte,void *extra); 
  88.  
  89.     private:
  90.  
  91.         // converter handle internals
  92.         static void InitHandles();
  93.         static void CloseHandles();
  94.         static void DefaultHandles();
  95.         static int DefaultHandles_X86();
  96.         static int DefaultHandles_CPP();
  97.         static int DefaultHandles_MMX();
  98.         static int SetupHandle(int src_id,int dest_id,void *convert,void *area,void *partial);
  99.         
  100.         // interal conversion routines
  101.         inline int InternalConvert(HANDLE *handle,void *src,void *dest,uint pixels,void *extra);
  102.  
  103.         // table generation
  104.         static int GenerateTable16_X86(void *dest,FORMAT const &format);
  105.  
  106.         // reference count
  107.         static int count;
  108.  
  109.         // static copy converter handle
  110.         static HANDLE *StaticCopyHandle;
  111.  
  112.         // static 32bit -> X converter handles
  113.         static HANDLE *StaticHandle32;
  114.  
  115.         // static 16bit -> X converter handles
  116.         static HANDLE *StaticHandle16;
  117.  
  118.         // static 8bit -> X converter handles
  119.         static HANDLE *StaticHandle8;
  120.         static HANDLE *StaticHandle8_4BYTE;
  121.         static HANDLE *StaticHandle8_3BYTE;
  122.         static HANDLE *StaticHandle8_2BYTE;
  123.         static HANDLE *StaticHandle8_1BYTE;
  124. };
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. inline int CONVERTER::Convert(void *src,FORMAT const &src_format,void *dest,FORMAT const &dest_format,uint pixels,void *extra)
  134. {
  135.     // convert
  136.     HANDLE *handle=RequestHandle(src_format,dest_format);
  137.     int result=Convert(handle,src,dest,pixels,extra);
  138.     FreeHandle(handle);
  139.     return result;
  140. }
  141.  
  142.  
  143. inline int CONVERTER::AreaConvert(void *src,FORMAT const &src_format,void *dest,FORMAT const &dest_format,uint width,uint height,uint src_advance,uint dest_advance,void *extra)
  144. {
  145.     // area convert
  146.     HANDLE *handle=RequestHandle(src_format,dest_format);
  147.     int result=AreaConvert(handle,src,dest,width,height,src_advance,dest_advance,extra);
  148.     FreeHandle(handle);
  149.     return result;
  150. }
  151.  
  152.  
  153. inline int CONVERTER::PartialConvert(void *src,FORMAT const &src_format,void *dest,FORMAT const &dest_format,int start_byte,int stop_byte,void *extra)
  154. {
  155.     return 0;
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. inline CONVERTER::HANDLE* CONVERTER::RequestHandle(int src_id,int dest_id)
  166. {
  167.     // fail on invalid destination id
  168.     if (dest_id<FORMATBASE || dest_id>FORMATMAX) return NULL;
  169.  
  170.     // check for identical format ids = copy convert
  171.     if (src_id==dest_id) return &StaticCopyHandle[src_id-FORMATBASE];
  172.  
  173.     // converters via src id
  174.     if (src_id==ARGB8888) return &StaticHandle32[dest_id-FORMATBASE];
  175.     if (src_id==RGB565)   return &StaticHandle16[dest_id-FORMATBASE];
  176.     if (src_id==INDEX8)   return &StaticHandle8 [dest_id-FORMATBASE];
  177.  
  178.     // failure
  179.     return NULL;
  180. }
  181.  
  182.  
  183. inline CONVERTER::HANDLE* CONVERTER::RequestHandle(FORMAT const &src,FORMAT const &dest)
  184. {
  185.     // try to get converter by id
  186.     HANDLE *handle=RequestHandle(src.id,dest.id);
  187.     if (handle) return handle;
  188.  
  189.     // check 8bit -> X
  190.     if (src.id==INDEX8)
  191.     {
  192.         // check for byte size table handle
  193.         switch (dest.bytes)
  194.         {
  195.             case 4: return StaticHandle8_4BYTE;
  196.             case 3: return StaticHandle8_3BYTE;
  197.             case 2: return StaticHandle8_2BYTE;
  198.             case 1: return StaticHandle8_1BYTE;
  199.         }
  200.     }
  201.  
  202.     // unknown format - check for match
  203.     if (src==dest && src.ok())
  204.     {
  205.         // allocate custom copy handle
  206.         handle=new HANDLE;
  207.         if (!handle) return NULL;
  208.         *handle->src=src;
  209.         *handle->dest=dest;
  210.         handle->extra=0;
  211.         handle->copy=1;
  212.         handle->cleanup=1;
  213.         handle->Convert=StaticCopyHandle[0].Convert;
  214.         handle->AreaConvert=StaticCopyHandle[0].AreaConvert;
  215.         handle->PartialConvert=StaticCopyHandle[0].PartialConvert;
  216.         return handle;
  217.     }
  218.  
  219.     // generic converter
  220.     handle=new HANDLE;
  221.     if (!handle) return NULL;
  222.     *handle->src=src;
  223.     *handle->dest=dest;
  224.     handle->extra=0;          // how to handle this?
  225.     handle->copy=0;
  226.     handle->cleanup=1;
  227.     handle->Convert=NULL;
  228.     handle->AreaConvert=NULL;
  229.     handle->PartialConvert=NULL;
  230.     return handle;
  231. }
  232.  
  233.  
  234. inline void CONVERTER::FreeHandle(CONVERTER::HANDLE *handle)
  235. {
  236.     if (handle && handle->cleanup) delete handle;
  237. }
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245. inline int CONVERTER::Convert(CONVERTER::HANDLE *handle,void *src,void *dest,uint pixels,void *extra)
  246. {
  247.     // check valid handle
  248.     if (!handle) return 0;
  249.  
  250.     // copy convert needs bytes not pixels
  251.     if (handle->copy) pixels*=handle->src->bytes;
  252.  
  253.     // check that if handle needs extra its been given
  254.     if (handle->extra && !extra) return 0;
  255.     
  256.     // perform the conversion
  257.     return InternalConvert(handle,src,dest,pixels,extra);
  258. }
  259.  
  260.  
  261. inline int CONVERTER::AreaConvert(CONVERTER::HANDLE *handle,void *src,void *dest,uint width,uint height,uint src_advance,uint dest_advance,void *extra)
  262. {
  263.     /*
  264.     // check for valid converter handle
  265.     if (!handle || !handle->Convert) return 0;
  266.     
  267.     // check that if converter needs extra its been given
  268.     if (handle->extra && !extra) return 0;
  269.  
  270.     // copy convert needs bytes not pixels
  271.     if (handle->copy) width*=handle->src->bytes;
  272.  
  273.     // try specified AreaConverter in Converter (not yet)
  274.  
  275.     // fail on invalid data
  276.     if (!src || !dest) return 0;
  277.  
  278.     // provide a backup area converter
  279.     if (handle->AreaConvert==NULL)
  280.     {
  281.         // fail on bad converter
  282.         if (!handle->Con