Opengl Freeimage 保存屏幕像素到图片

bool FreeImageLoader::SaveImage(char *imgpath)
{
    /*
    int img_widht,img_height;
    unsigned char *mpixels =new unsigned char[1024*768*4];
    FREE_IMAGE_FORMAT fif =FIF_PNG;
    BOOL bSuccess = FALSE;

    glReadBuffer(GL_FRONT);
    glReadPixels(0, 0, 1024, 768, GL_RGBA, GL_UNSIGNED_BYTE, mpixels);
    glReadBuffer(GL_BACK);
    for(int i=0; i<(int)1024*768*4; i+=4)
    {
        //repeated xor to swap bytes
        mpixels[i] ^= mpixels[i+2] ^= mpixels[i] ^= mpixels[i+2];
    }
    FIBITMAP* bitmap = FreeImage_ConvertFromRawBits(mpixels,1024,768,4096,32,8,8,8,FALSE); 
    //FIBITMAP *dibsave = FreeImage_Load(FIF_PNG,"img/default.png", 0);
    //FIBITMAP *dibsave =FreeImage_Allocate(FIF_PNG,800,800,32);
    //img_widht=FreeImage_GetWidth(dibsave);
    //img_height=FreeImage_GetHeight(dibsave);

    
    bSuccess = FreeImage_Save(FIF_PNG, bitmap, "gg1.png", PNG_DEFAULT);
     FreeImage_Unload(bitmap);
     delete[] mpixels;*/
    unsigned char *mpixels =new unsigned char[800*600*4];
    glReadBuffer(GL_FRONT);
    glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, mpixels);
    glReadBuffer(GL_BACK);
    for(int i=0; i<(int)800*600*4; i+=4)
    {
        
        mpixels[i] ^= mpixels[i+2] ^= mpixels[i] ^= mpixels[i+2];
    }
    FIBITMAP* bitmap =FreeImage_Allocate(800,600,32,8,8,8);

    for(int y =0 ;y<FreeImage_GetHeight(bitmap);y++)
    {
        BYTE *bits =FreeImage_GetScanLine(bitmap,y);
        for(int x =0 ;x<FreeImage_GetWidth(bitmap);x++)
        {
            bits[0] =mpixels[(y*800+x)*4+0];
            bits[1] =mpixels[(y*800+x)*4+1];
            bits[2] =mpixels[(y*800+x)*4+2];
            bits[3] =255;
            bits+=4;

        }

    }
    bool bSuccess = FreeImage_Save(FIF_PNG, bitmap,imgpath, PNG_DEFAULT);
     FreeImage_Unload(bitmap);

    return bSuccess;

}

更新

2017 -4 -25

freeimage保存图片主要几个函数

FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;  //  这个是要保存图片的格式,可以选择 FIF_JPEG   FIF_PNG  FIF_BMP 等等几十种格式 
fif = FIF_JPEG;


FIBITMAP* convertedBmp; //定义一个freeimage的数组用来保存数据
//然后我们可以遍历图片的宽高逐个写入像素的值

DLL_API BOOL DLL_CALLCONV FreeImage_Save(FREE_IMAGE_FORMAT fif, FIBITMAP *dib, const char *filename, int flags FI_DEFAULT(0));
//最后这个函数就是保存图片到文件了,第一个参数是图片的格式,上面已经说了,第二个是数据的内容,传刚才的bitmap就好,第三个参数是文件的名字,最后一个是图片的质量

//下面从别的地方抄来的例子

PixelType* pixels = pix.getPixels();
unsigned int width = pix.getWidth();
unsigned int height = pix.getHeight();
unsigned int bpp = pix.getBitsPerPixel();

FREE_IMAGE_TYPE freeImageType = getFreeImageType(pix);
FIBITMAP* bmp = FreeImage_AllocateT(freeImageType, width, height, bpp);
unsigned char* bmpBits = FreeImage_GetBits(bmp);
if(bmpBits != NULL)

{
  int srcStride = width * pix.getBytesPerPixel();
  int dstStride = FreeImage_GetPitch(bmp);
  unsigned char* src = (unsigned char*) pixels;
  unsigned char* dst = bmpBits;
  for(int i = 0; i < (int)height; i++)

  {

    memcpy(dst, src, srcStride);
    src += srcStride;
    dst += dstStride;
  }
}







 

 

 

 

 

posted on 2013-01-15 14:07 c_dragon 阅读(...) 评论(...) 编辑 收藏

导航