home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / flash078.zip / flashsource-r0_7_8.zip / imagecnv.cpp < prev    next >
C/C++ Source or Header  |  2001-05-16  |  5KB  |  244 lines

  1. #include <fstream>
  2. #include <sstream>
  3. #include <hash_map>
  4. #include <vector>
  5.  
  6. #include "fbutton.h"
  7. #include "fbase.h"
  8. #include "fshape.h"
  9. #include "fdisplay.h"
  10. #include "fcontrol.h"
  11. #include "fsprite.h"
  12. #include "fsound.h"
  13. #include "flashmp3encoder.h"
  14. #include "ffont.h"
  15. #include "flashfontobj.h"
  16. #include "fmorph.h"
  17.  
  18. #include "ftarga.h"
  19. #include "fbitmap.h"
  20.  
  21. #include "FImport.h"
  22. #include "Magick++/Magick++.h"
  23.  
  24. using namespace Magick; 
  25.  
  26. UWORD ReadImage(char *fname, std::ostringstream &f, int &width, int &height) 
  27. {
  28.     UWORD return_val = 0;
  29.     try
  30.     {
  31.         Image image(fname);
  32.         width = image.baseColumns();
  33.         height = image.baseRows();
  34.         
  35.         //Encode Image with 24 bit color
  36.         int format = 5;
  37.         
  38.         unsigned char *imagedata = (unsigned char *)malloc(height*width*3);
  39.         unsigned char *tgadata = (unsigned char *)malloc(height*width*4);
  40.  
  41.         image.write(0,0,width,height,"RGB",CharPixel,imagedata);    
  42.         {
  43.             for(UWORD y = 0; y < height; y++)
  44.             {
  45.                 for(UWORD x = 0; x < width; x++)
  46.                 {
  47.                     UDWORD base = (y*width+x)*4;
  48.  
  49.                     UDWORD base2 = (y*width+x)*3;
  50.                 
  51.                     unsigned char r = imagedata[base2];
  52.                     unsigned char g = imagedata[base2+1];
  53.                     unsigned char b = imagedata[base2+2];
  54.                     
  55.                     ((unsigned char*)tgadata)[base+0]   =  0xff;
  56.                     ((unsigned char*)tgadata)[base+1]   =  r;
  57.                     ((unsigned char*)tgadata)[base+2]   =  g;
  58.                     ((unsigned char*)tgadata)[base+3]   =  b;
  59.                     
  60.                 }
  61.             }
  62.         }
  63.         FlashZLibBitmapData d((unsigned char *)tgadata,(width*height*4));
  64.         FlashTagDefineBitsLossless2 db(format,width, height, d);
  65.         f << db;
  66.  
  67.         free (imagedata);
  68.         free (tgadata);
  69.  
  70.         FlashMatrix m;
  71.         m.SetScale(20,20);
  72.         
  73.         FlashShapeWithStyle s;
  74.         FlashFillStyleArray ffa;
  75.         FlashFillStyleBitmap sf(db.GetID(),m); 
  76.  
  77.         ffa.AddFillStyle(&sf);
  78.         s.SetFillStyleArray(ffa);
  79.             
  80.         FlashShapeRecordChange c(0,0);
  81.         c.ChangeFillStyle1(1);
  82.         s.AddRecord(c);
  83.         s.AddRecord(FlashShapeRecordStraight(width*20, 0));
  84.         s.AddRecord(FlashShapeRecordStraight(0, height*20));
  85.         s.AddRecord(FlashShapeRecordStraight(-width*20, 0));
  86.         s.AddRecord(FlashShapeRecordStraight(0, -height*20)); 
  87.             
  88.         
  89.         FlashTagDefineShape3 ftds(s);
  90.         
  91.         f << ftds;
  92.         return_val =  ftds.GetID();
  93.     }
  94.     catch(Exception &error_)
  95.     {
  96.         std::cout << "Caught exception: " << error_.what() << "\n"; 
  97.         throw (error_);
  98.     }
  99.     return return_val;
  100. }
  101. UWORD ReadImage2(char *fname, std::ostringstream &f, int &width, int &height) 
  102. {
  103.     UWORD return_val = 0;
  104.     try
  105.     {
  106.         Image image(fname);
  107.         width = image.baseColumns();
  108.         height = image.baseRows();
  109.         
  110.         Blob blob; 
  111.         image.magick( "JPEG" ); // Set JPEG output format 
  112.         image.write( &blob ); 
  113.         
  114.         unsigned char *data = (unsigned char*)malloc(blob.length());
  115.         memcpy(data,blob.data(),blob.length());
  116.         FlashTagDefineBitsJPEG2 db(data, blob.length());
  117.         f << db;
  118.         free(data);
  119.  
  120.         FlashMatrix m;
  121.         m.SetScale(20,20);
  122.         
  123.         FlashShapeWithStyle s;
  124.         FlashFillStyleArray ffa;
  125.         FlashFillStyleBitmap sf(db.GetID(),m); 
  126.  
  127.         ffa.AddFillStyle(&sf);
  128.         s.SetFillStyleArray(ffa);
  129.             
  130.         FlashShapeRecordChange c(0,0);
  131.         c.ChangeFillStyle1(1);
  132.         s.AddRecord(c);
  133.         s.AddRecord(FlashShapeRecordStraight(width*20, 0));
  134.         s.AddRecord(FlashShapeRecordStraight(0, height*20));
  135.         s.AddRecord(FlashShapeRecordStraight(-width*20, 0));
  136.         s.AddRecord(FlashShapeRecordStraight(0, -height*20)); 
  137.             
  138.         
  139.         FlashTagDefineShape3 ftds(s);
  140.         
  141.         f << ftds;
  142.         return_val =  ftds.GetID();
  143.     }
  144.     catch(Exception &error_)
  145.     {
  146.         std::cout << "Caught exception: " << error_.what() << "\n"; 
  147.         throw (error_);
  148.     }
  149.     return return_val;
  150. }
  151.  
  152. void main(void)
  153. {
  154.     char buffer[255];    
  155.  
  156.     std::ifstream in("convert.txt");
  157.     try
  158.     {
  159.         for(;;)
  160.         {
  161.             in.getline(buffer,255);
  162.             if(in.eof())
  163.             {
  164.               break;
  165.             }
  166.             Image img(buffer);
  167.             for(int i = 0; i < 255; i++)
  168.             {
  169.                 if(buffer[i] == '.') 
  170.                 {
  171.                     buffer[i+1]='p';
  172.                     buffer[i+2]='n';
  173.                     buffer[i+3]='g';
  174.                 }
  175.             }
  176.             Geometry g("150");
  177.             img.zoom(g);
  178.             img.write(buffer);        
  179.         }
  180.     }
  181.     catch(Exception &error_)
  182.     {
  183.         std::cout << "Caught exception: " << error_.what() << "\n"; 
  184.         throw (error_);
  185.     }
  186.  
  187. }
  188. /*
  189. int main( int argc , char *argv[ ])
  190. {
  191.  
  192.     if(argc < 3) 
  193.     {
  194.         std::cout << "Usage: imagecnv inputfilename outputfilename [1 | 2]\n1 = Lossless Compression\n2 = JPEG compression";
  195.         return -1;
  196.     }
  197.     std::ostringstream f(std::ios::binary);
  198.     std::ofstream fileout(argv[2],std::ios::binary);
  199.     if(fileout.fail())
  200.     {
  201.         std::cout << "ERROR: Could not open output file.\n";
  202.         return -1;
  203.     }
  204.     bool lossless = true;
  205.  
  206.     if(argc > 3)
  207.     {
  208.         if(strcmp(argv[3], "1") == 0)
  209.         {
  210.             lossless = true;
  211.         }
  212.         else if(strcmp(argv[3], "2") == 0)
  213.         {
  214.             lossless = false;
  215.         }
  216.     }
  217.     f << FlashTagBackgroundColor(0xff,0xff,0xff);
  218.         
  219.     int width;
  220.     int height;
  221.     UWORD id;
  222.     try
  223.     {
  224.         if(lossless) 
  225.             id = ReadImage(argv[1],f,width,height);
  226.         else
  227.             id = ReadImage2(argv[1],f,width,height);
  228.     }
  229.     catch(...)
  230.     {
  231.         return -1;
  232.     }
  233.  
  234.     f << FlashTagPlaceObject2 (1,id);
  235.  
  236.     f << FlashTagShowFrame();
  237.  
  238.     f << FlashTagEnd();
  239.     
  240.     fileout << FlashHeader(5,f.str().size(),width*20,height*20,15.0,1);
  241.     fileout << f.str();
  242.     return 0;
  243. }
  244. */