home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / flash078.zip / imagecnv.zip / imagecnv.cpp < prev    next >
C/C++ Source or Header  |  2001-05-13  |  5KB  |  207 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. int main( int argc , char *argv[ ])
  153. {
  154.  
  155.     if(argc < 3) 
  156.     {
  157.         std::cout << "Usage: imagecnv inputfilename outputfilename [1 | 2]\n1 = Lossless Compression\n2 = JPEG compression";
  158.         return -1;
  159.     }
  160.     std::ostringstream f(std::ios::binary);
  161.     std::ofstream fileout(argv[2],std::ios::binary);
  162.     if(fileout.fail())
  163.     {
  164.         std::cout << "ERROR: Could not open output file.\n";
  165.         return -1;
  166.     }
  167.     bool lossless = true;
  168.  
  169.     if(argc > 3)
  170.     {
  171.         if(strcmp(argv[3], "1") == 0)
  172.         {
  173.             lossless = true;
  174.         }
  175.         else if(strcmp(argv[3], "2") == 0)
  176.         {
  177.             lossless = false;
  178.         }
  179.     }
  180.     f << FlashTagBackgroundColor(0xff,0xff,0xff);
  181.         
  182.     int width;
  183.     int height;
  184.     UWORD id;
  185.     try
  186.     {
  187.         if(lossless) 
  188.             id = ReadImage(argv[1],f,width,height);
  189.         else
  190.             id = ReadImage2(argv[1],f,width,height);
  191.     }
  192.     catch(...)
  193.     {
  194.         return -1;
  195.     }
  196.  
  197.     f << FlashTagPlaceObject2 (1,id);
  198.  
  199.     f << FlashTagShowFrame();
  200.  
  201.     f << FlashTagEnd();
  202.     
  203.     fileout << FlashHeader(5,f.str().size(),width*20,height*20,15.0,1);
  204.     fileout << f.str();
  205.     return 0;
  206. }
  207.