Logo Search packages:      
Sourcecode: magics++ version File versions  Download package

MAGICS_NO_EXPORT bool BaseDriver::convertToPixmap ( const string &  fname,
const GraphicsFormat  format,
const int  reso,
const float  wx0,
const float  wy0,
const float  wx1,
const float  wy1 
) const [protected, virtual, inherited]

converting object to pixmap

This method should be used by all Magics++ drivers

Reimplemented in magics::CairoDriver.

Definition at line 256 of file BaseDriverImages.h.

Referenced by magics::CairoDriver::convertToPixmap(), magics::BaseDriver::renderImage(), and magics::GDDriver::renderLogo().

{
      debugOutput("Start Image conversion");

      int Landscape = 0;
      float bx1=100.;
      float by1=100.;
      unsigned char *image = 0;
      int col=0,row=0;
      int status = 0;
      string s2(" ");
      string pixmapFormat("rgb");

      if(format==PS || format==EPS) //File is PostScript
      {
//          FILE* fd3;
//          char buf[1024];
            string cmd;
            int x1 = 0;
            int y1 = 0;

            if(check_ps(fname,x1,y1,Landscape) )
            {
                  Log::error() << "BaseDriverImages: Open of source PostScript file failed!" << endl;
                  return false;
                  }

            s2 = getTmpName()+".ppm";
            if(s2==" ")
            {
                  Log::error() << "BaseDriverImages: Open of temp file failed!" << endl;
                  return false;
            }

      //    if(format==PS)  // does not work with EPS
            {
              const float Xres = float(reso);
              bx1 = float(x1)/72.*Xres + 0.5;
              x1  = (int) bx1;
              by1 = float(y1)/72.*Xres + 0.5;
              y1  = (int) by1;

              char boxx[5];
              char boxy[5];
              char boxz[5];
              sprintf(boxx,"%d",x1);
              sprintf(boxy,"%d",y1);
              sprintf(boxz,"%d",reso);

              cmd = "( gs -q -dNOPAUSE -dSAFER -sDEVICE=ppmraw -sOutputFile=" + s2 +
                  " -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -dCOLORSCREEN -dBATCH -g" +
                  boxx + "x" + boxy + " -r" + boxz + " " + fname + " < /dev/null )";
            }

            status = system(cmd.c_str());

            if(status)
            {
                  Log::error() << "BaseDriverImages: Command exit Not zero" << endl;
                  return false;
            }
            ifstream I(s2.c_str());
            if(!I)
            {
                  Log::error() << "BaseDriverImages: Incorrect PostScript Format!" << endl;
                  return false;
            }
            if(check_ppmHeader(I,col,row) )
            {
                  return 1;
            }

            image = new unsigned char [row*col*3];
            I.read((char*)image,row*col*3);
            I.close();
            remove(s2.c_str());
      }
#ifdef MAGICS_RASTER
      else if(format==GIF || format==PNG || format==JPG)
      {
            // convert png to ppm(raw) like image
            FILE* fd3 = fopen(fname.c_str(),"rb");
            if(!fd3)
            {
                  Log::error() << "BaseDriverImages: Open failed for raster source > "<< fname << endl;
                  return false;
            }
            gdImagePtr imp = 0;
            if(format==JPG)      imp = gdImageCreateFromJpeg(fd3);
            else if(format==PNG) imp = gdImageCreateFromPng(fd3);
            else if(format==GIF)
            {
#ifdef MAGICS_GIF
                  imp = gdImageCreateFromGif(fd3);
#else
                  Log::error() << "GIF pixmap import is not supported in this version! You need a GIF enabled GD library." << endl;
                  return false;
#endif
            }
            fclose(fd3);
            if(!imp)
            {
                  Log::error() << "BaseDriverImages: Incorrect raster file format (can not be handled by GD) !" << endl;
                  return false;
            }

            col = gdImageSX(imp);
            row = gdImageSY(imp);
            bx1 = (float) col;
            by1 = (float) row;
            Landscape = 0;

            if(format == PNG && // imp->transparent == 1 &&
                   alphaEnabled_ == true)
            {
                  pixmapFormat="rgba";

                  image = new unsigned char [col*row*4];
                  unsigned char *p = image;
                  int i,j;
                  for (i = 0; i<row; i++)
                  for (j = 0; j<col; j++)
                  {
                        int c = gdImageGetPixel(imp,j,i);
                        int r = gdImageRed(imp,c);
                        int g = gdImageGreen(imp,c);
                        int b = gdImageBlue(imp,c);
                        int a = gdImageAlpha(imp,c);
                        *(p++) = (unsigned char) r;
                        *(p++) = (unsigned char) g;
                        *(p++) = (unsigned char) b;
                        *(p++) = (unsigned char) 255-2*a;
                  }
            }
            else
            {
                  image = new unsigned char [col*row*3];
                  unsigned char *p = image;
                  int i,j;
                  for (i = 0; i<row; i++)
                  for (j = 0; j<col; j++)
                  {
                        int c = gdImageGetPixel(imp,j,i);
                        int r = gdImageRed(imp,c);
                        int g = gdImageGreen(imp,c);
                        int b = gdImageBlue(imp,c);
                        *(p++) = (unsigned char) r;
                        *(p++) = (unsigned char) g;
                        *(p++) = (unsigned char) b;
                  }
            }

            gdImageDestroy(imp);
      }
#endif
      else 
      {
            Log::warning() << "BaseDriverImages: graphics formats ("<<format<<") is NOT supported!" << endl;
            return 1;//No PPM
      }

      float x0 = wx0; //Left
      float x1 = wx1; //Right
      float y0 = wy0;
      float y1 = wy1;

      // const float aspect = bx1/by1;
      //if ( Landscape == 1 )       y1 = y0 + abs(x1-x0)*aspect;
      //else if ( Landscape == 0 )  x1 = x0 + abs(y1-y0)*aspect;

      bool alpha = (pixmapFormat == "rgba");
      status = renderPixmap(x0,y0,x1,y1,col,row,image,Landscape,alpha);

      if(!status) Log::warning() <<"BaseDriver::convertToPixmap() -> no Pixmap could be drawn! Zero size of at least one dimension."<<endl;
      delete [] image;

      return status;
}


Generated by  Doxygen 1.6.0   Back to index