Advertisement
TimLu

CIMXLoader.cpp

Jan 22nd, 2023
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.26 KB | Gaming | 0 0
  1.  
  2. #include "TPixelPlotter.h"
  3. #include "CIMXLoader.h"
  4. #include "depreciate/Ttypes.h"
  5. #include "colour/colour.h"
  6. #include "ILightSampler.h"
  7.  
  8. namespace mg
  9. {
  10. #pragma pack(push)
  11. #pragma pack(1)
  12. enum  imxFormat
  13. {
  14.     // every mode the PS2 supports...
  15.     imxIndex4,
  16.     imxIndex8,
  17.     imxRGBA5551,
  18.     imxRGB888,
  19.     imxRGBA8888,
  20.     imxGrayscale16b,                // 16-bit grayscale bitmap data
  21.     imxNoFormat,
  22.     imxAsInt        = 0xFFFFFFFF
  23. };
  24.  
  25. enum imxChunkType
  26. {
  27.     imxClut16,
  28.     imxClut256,
  29.     imxBitmap,      // data stored in this chunk depends on imxFormat type
  30.     imxEnd,
  31.     imxUserData,            // data stored here is left to the user application to parse
  32.     imxChnkAsInt    = 0xFFFFFFFF
  33. } ;
  34.  
  35.  
  36. typedef struct imxHeader_
  37. {
  38.     UINT32      header;             // "\0X0I" (bytes: 49 4D 58 00)
  39.     UINT8       md5[16];
  40.     UINT32      xsize, ysize;
  41.     imxFormat   format;
  42. } imxHeader;
  43.  
  44. typedef struct imxChunk_
  45. {
  46.     imxChunkType    type;
  47.     UINT32          len;
  48. } imxChunk;
  49. #pragma pack(pop)
  50.  
  51. size_t calcimxfilesize(ISurface2D *img,IPixelFormat *format)
  52. {
  53.     size_t size=sizeof(imxHeader);
  54.  
  55.     switch (format->get_pixel_byte_size())
  56.     {      
  57.         case 4:size+=(16<<2)+8;break;
  58.         case 8:size+=(256<<2)+8;break;
  59.     }
  60.     size+=(img->stride()*img->size().y)+8+8; //8 for header, 8 for end
  61.     return size;
  62. }
  63.  
  64. imxHeader hdr;
  65.  
  66.  
  67. bool get_imx_metrics(FILE *f,tmath::CVector2i &size,EPixelFormat &pixel_format)
  68. {  
  69.     fread(&hdr,1,sizeof(imxHeader),f);
  70.     size.x=hdr.xsize;
  71.     size.y=hdr.ysize;
  72.     switch (hdr.format)
  73.     {
  74.         case imxIndex8:         pixel_format=EPF_INDEX_8I;break;
  75.         case imxGrayscale16b:   pixel_format=EPF_L_8I;break;
  76.         case imxRGBA8888:       pixel_format=EPF_ARGB_8I;break;
  77.         case imxNoFormat:       pixel_format=EPF_UNKNOWN;
  78.         default :
  79.             return false;
  80.     }
  81.     return true;
  82. }
  83.  
  84.  
  85. mg::Targb<UINT8> lcmaprgb[256];
  86. UINT32 lcmap[256];
  87.  
  88. void convert_palette(bool isindexed,int count)
  89. {
  90.     while (count--)
  91.     {
  92.         lcmaprgb[count].swaprgb();
  93.         if (isindexed) //||remapped
  94.             lcmap[count]=count;
  95.         else
  96.             lcmap[count]= 0;//g2d::get_colour(*(UINT32*)&lcmaprgb[count]);     
  97.     }
  98. }
  99.  
  100. UINT32 get_total_chunk_sizes(FILE *f)
  101. {
  102.     imxChunk chnk;
  103.     chnk.type=imxChnkAsInt;
  104.     UINT32 fpos=ftell(f);
  105.     UINT32 count= 0;
  106.     while (chnk.type!=imxEnd)
  107.     {
  108.         fread(&chnk,8,1,f);
  109.         count+=chnk.len+8;
  110.         fseek(f,chnk.len,SEEK_CUR);
  111.     }
  112.     fseek(f,fpos,SEEK_SET);
  113.     return count;
  114. }
  115.  
  116. template <class PIXEL_TYPE>
  117. bool read_imx_data(TPixelPlotter<PIXEL_TYPE> &pp,FILE *f)
  118. {
  119.     imxChunk chnk;
  120.     chnk.len= 0;
  121.     chnk.type=imxClut16;
  122.     bool foundbitmap=false;
  123.     int i= 0;
  124.     UINT32 x,y;
  125.     UINT32 pdata;
  126.     pdata= 0;
  127.     bool haspalette=((hdr.format==imxIndex4)||(hdr.format==imxIndex8));
  128.     //if (!feof(f)) return false;
  129.     while (chnk.type!=imxEnd)
  130.     {
  131.         fread(&chnk,8,1,f);
  132.         if (feof(f)) return true;
  133.         switch (chnk.type)
  134.         {
  135.         case imxClut16:
  136.             fread(lcmaprgb,16<<2,1,f);             
  137.             //convert_palette(bm->isindexed()!= 0,16);                             
  138.             //buildimagecolourpalettes(bm,lcmaprgb);
  139.             break;
  140.         case imxClut256:
  141.             fread(lcmaprgb,256<<2,1,f);
  142.             //convert_palette(bm->isindexed()!= 0,256);
  143.             //buildimagecolourpalettes(bm,lcmaprgb);
  144.             break;
  145.         case imxBitmap:
  146.             if (!foundbitmap)
  147.             {
  148.                 for (y= 0;y<hdr.ysize;y++)
  149.                 switch (hdr.format)
  150.                 {
  151.                     case imxIndex4:
  152.                         for (x= 0;x<(hdr.xsize>>1);x+=2)
  153.                         {  
  154.                             fread(&pdata,1,1,f);
  155.                             pp.plot(lcmap[pdata & 3]);
  156.                             pp.plot(lcmap[pdata & 3]);                                                         
  157.                         }                          
  158.  
  159.                         break;
  160.                     case imxIndex8:
  161.                         for (x= 0;x<hdr.xsize;x+=1)
  162.                         {  
  163.                             fread(&pdata,1,1,f);
  164.                    
  165.                             pp.plot(pdata);     //lcmap[pdata]
  166.                         }
  167.                         break;
  168.  
  169.                     case imxGrayscale16b:
  170.                         for (x= 0;x<hdr.xsize;x+=1)
  171.                         {  
  172.                             fread(&pdata,2,1,f);
  173.                             pdata=(pdata >> 8)|(pdata & 0x0000FF00)|((pdata << 8)& 0x00FF0000);
  174.                             pp.plot(pdata);
  175.                         }
  176.  
  177.                         break;
  178.                     case imxRGBA5551:
  179.                         for (x= 0;x<hdr.xsize;x+=1)
  180.                         {  
  181.                             fread(&pdata,2,1,f);
  182.                             pdata=((pdata << 17)& 0xFF000000)|((pdata << 9)& 0x00FF0000)|((pdata << 6)& 0x0000FF00)|((pdata << 3)& 0x000000FF);
  183.                             pdata=SWAPRGB(pdata);
  184.                             //fix brightness
  185.                             if (pdata & 0x80000)
  186.                                 pdata |= 0x30000;
  187.                             if (pdata & 0x800)
  188.                                 pdata |= 0x300;
  189.                             if (pdata & 0x8)
  190.                                 pdata |= 0x3;
  191.                                
  192.                             //pdata.
  193.                             pp.plot(pdata);
  194.                         }
  195.                         break;
  196.                     case imxRGB888:
  197.                         for (x= 0;x<hdr.xsize;x+=1)
  198.                         {  
  199.                             pdata= 0;
  200.                             fread(&pdata,3,1,f);
  201.                             //pdata=(pdata>> 8) ;
  202.                             pdata=SWAPRGB(pdata);
  203.                             pp.plot(pdata);                        
  204.                         }
  205.                         break;
  206.                     case imxRGBA8888:
  207.                         for (x= 0;x<hdr.xsize;x+=1)
  208.                         {  
  209.                             fread(&pdata,4,1,f);
  210.                             //different byte ordering so swap it
  211.                             pdata=SWAPRGB(pdata)|0xFF000000;
  212.                            
  213.                             pp.plot(pdata);
  214.                            
  215.                         }
  216.                         break;                     
  217.                 }                  
  218.             }else
  219.                 fseek(f,chnk.len,SEEK_CUR);
  220.             foundbitmap=true;
  221.             break;
  222.         default:
  223.             fseek(f,chnk.len,SEEK_CUR);
  224.         }
  225.     }
  226.     return true;
  227.  
  228. }
  229.  
  230.  
  231. COffScreenTexture *read_imx(FILE *f)
  232. {
  233.     tmath::CVector2i wsize;
  234.     EPixelFormat pformat;
  235.    
  236.     if (!get_imx_metrics(f,wsize,pformat))
  237.         return NULL;   
  238.  
  239.     COffScreenTexture *new_texture=new COffScreenTexture(wsize,pformat);
  240.    
  241.    
  242.     if (pformat == EPF_ARGB_8I)
  243.     {
  244.         typedef TPixelPlotter<mg::Cargb_i8> pixel_plotter_type;    
  245.         pixel_plotter_type pixel_plot((mg::Cargb_i8 *)new_texture->lock(),new_texture->size(),new_texture->stride()/new_texture->get_pixel_byte_size());
  246.         if (!read_imx_data(pixel_plot,f)) return NULL;             
  247.     }else if (pformat == mg::EPF_INDEX_8I)
  248.     {
  249.         typedef TPixelPlotter<UINT8> pixel_plotter_type;   
  250.         pixel_plotter_type pixel_plot((UINT8*)new_texture->lock(),new_texture->size(),new_texture->stride()/new_texture->get_pixel_byte_size());
  251.         if (!read_imx_data(pixel_plot,f)) return NULL;             
  252.     }
  253.  
  254.     return new_texture;
  255.        
  256.    
  257. }
  258.  
  259. /*
  260. bool writeimx(FILE *f,tsurface *bm)
  261. {
  262.     imxChunk chnk; 
  263.     hdr.header= 0x00584D49;
  264.     //bung that random data back there!
  265.     if (bm->pfd==PXFunknown)
  266.     {
  267.         hdr.xsize= 0;
  268.         hdr.ysize= 0;
  269.         hdr.format=imxNoFormat;
  270.         fwrite(&hdr,sizeof(hdr),1,f);
  271.         fwrite(bm->scrptr,bm->offsd,1,f);
  272.         return true;
  273.     }
  274.     hdr.xsize=bm->size.x;
  275.     hdr.ysize=bm->size.y;
  276.     switch (bm->pfd)
  277.     {      
  278.     case PXFi4:         hdr.format=imxIndex4;break;
  279.     case PXFi8:         hdr.format=imxIndex8;break;
  280.     case PXFr5g6b5:     hdr.format=imxRGBA5551;break;
  281.     case PXFa1r5g5b5:   hdr.format=imxRGBA5551;break;
  282.     case PXFr8g8b8:     hdr.format=imxRGB888;break;
  283.     case PXFa8r8g8b8:   hdr.format=imxRGBA8888;break;
  284.     case PXFl16:        hdr.format=imxGrayscale16b;break;
  285.  
  286.         //case PXFr4g4b4a4:
  287.     default:return false;       //if it's not one of these formats, it isn't an imx.. ,maybe I should convert to the closest format?
  288.     }
  289.     fwrite(&hdr,sizeof(hdr),1,f);
  290.  
  291.     if (bm->isindexed() && bm->palette)
  292.     {              
  293.         chnk.len=bm->palette->size <<2;
  294.         switch (bm->pfd)
  295.         {
  296.         case PXFi8:chnk.type=imxClut256;break;
  297.         case PXFi4:chnk.type=imxClut16;break;
  298.         }
  299.         fwrite(&chnk,8,1,f);
  300.         mg::Targb<UINT8> wcol,*rcol=bm->palette->rgbadata;
  301.         for (UINT32 i= 0;i<bm->palette->size;i++)
  302.         {
  303.             wcol=*rcol;
  304.             wcol.swaprgb();
  305.             fwrite(&wcol,4,1,f);   
  306.             rcol++;
  307.         }
  308.     }
  309.  
  310.     chnk.type=imxBitmap;
  311.     int bmstride=bm->get_stride(hdr.xsize);
  312.     chnk.len=bmstride*hdr.ysize;
  313.     fwrite(&chnk,8,1,f);
  314.     UINT32 pdata;
  315.     UINT32 x;
  316.     for (UINT32 y= 0;y<hdr.ysize;y++)
  317.     {
  318.         char *idata=(char*)bm->get_pointer_at_char(0,y);
  319.         switch (bm->pfd)
  320.         {
  321.         case PXFi8:            
  322.         case PXFi4:                            
  323.         case PXFl16:                               
  324.             fwrite(idata,bmstride,1,f);
  325.             idata+=bmstride;
  326.             break;
  327.         case PXFr5g6b5:
  328.             for (x= 0;x<hdr.xsize;x++)
  329.             {              
  330.                 pdata=*(UINT16*)idata;             
  331.                 pdata=((pdata >> 11) & 0x1F)|((pdata << 10) & 0x7C00)|((pdata >> 1)& 0x3E0);
  332.                 fwrite(&pdata,2,1,f);
  333.                 idata+=2;
  334.             }
  335.             break;
  336.         case PXFa1r5g5b5:
  337.             for (x= 0;x<hdr.xsize;x++)
  338.             {              
  339.                 //{0x7C00,0x3E0,0x1F
  340.                 pdata=*(UINT16*)idata;                             
  341.                 pdata=((pdata >> 10) & 0x1F)|((pdata << 10) & 0x7C00)|(pdata & 0x3E0);
  342.                 fwrite(&pdata,2,1,f);
  343.                 idata+=2;
  344.             }
  345.             break;
  346.         case PXFr8g8b8:
  347.             for (x= 0;x<hdr.xsize;x+=1)
  348.             {  
  349.                 pdata=*(UINT32*)idata;                             
  350.                 pdata=SWAPRGB(pdata);
  351.                 fwrite(&pdata,3,1,f);              
  352.                 idata+=3;
  353.             }
  354.             break;
  355.         case PXFa8r8g8b8:
  356.             for (x= 0;x<hdr.xsize;x++)
  357.             {  
  358.                 pdata=*(UINT32*)idata;             
  359.                 pdata=SWAPRGB(pdata);
  360.                 fwrite(&pdata,4,1,f);          
  361.                 idata+=4;
  362.             }
  363.  
  364.             break;                     
  365.         }      
  366.     }
  367.     chnk.type=imxEnd;
  368.     chnk.len= 0;
  369.     fwrite(&chnk,8,1,f);
  370.     return true;
  371. }*/
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement