C + + Generate GIF summary

Source: Internet
Author: User

 Disclaimer: All rights reserved.

Reprint must indicate source: http://blog.csdn.net/cartzhang/article/details/44020175


Recently, the memory images of BMP or Kinect need to be pinch into small GIF images. Look for, a lot of things, do a summary, for later use.

First, GIF.h

This method is simple, which is a header file. But I didn't try to succeed. The possible reason is that my BMP images are generated in their own order with GIF. The inconsistency required by the H header file.

The Gif.h header file code is as follows:

Here's how to use it:

Gifwriter m_gif_writer;//char* file_name = "E:\\aaa.gif";//int width = 128;//int height = 128;//int delay = 10;//gifbegi N (&m_gif_writer, file_name, width, height, delay);//code inside automatically starts from the first frame. Only the first frame adds the GIF header information//for ()//{////gifwriteframe ()//}//gifend ()


Header file Source:Charlie Tangora

GitHub Address: https://github.com/ginsweater/gif-h


Second, CximageLibrary

This library is open source and can be downloaded freely.

Use the downloaded version for the 702full version. Vs2013 compiled very smoothly, because of the need to use the version of the x64 , so the release of the use of the mode. There is an MFC -related compilation However, directly ignore, I do not use MFC.

The generated static library for Lib .

I copy the required header files and the static library to my own set of directories and the corresponding folders,


The include file copies the header file from the cximage , andthelib library file is inside the x64 file generated after the compilation , including Debug version and release version.

I found a code on the Internet and wrote two functions for cximage GIF . I have slightly added and modified the code on the basis.

In fact, the main processing of related folders convenient to call. Thank you very much, the header files and CPP files are as follows: (File Source:http://blog.csdn.net/fengbingchun/article/details/43538081

If there is a problem, please feel free to contact, thank you very much! )

MGif.h header file:

#pragma once#ifndef _mgif_h__#define _mgif_h__#include <string>using namespace std;void decoding_gif (string Strgifname, String strsavepath), void Encoding_gif (String strimgpath, String strgifname); #endif//

MGif.CPP File:

Cartzhang#include "MGif.h" #include "stdafx.h" #include "mGif.h" #include <iostream> #include "ximagif.h" # Include <io.h>using namespace std;std::wstring s2ws (const std::string& s) {int len;int slength = (int) s.length ( ) + 1;len = MultiByteToWideChar (CP_ACP, 0, S.c_str (), slength, 0, 0); wchar_t* buf = new Wchar_t[len]; MultiByteToWideChar (CP_ACP, 0, S.c_str (), Slength, buf, Len), std::wstring R (BUF);d elete[] Buf;return R;} void Decoding_gif (String strgifname, String strsavepath) {cximage img;std::wstring stemp = S2ws (strgifname);//Temporary Buffer is requiredlpcwstr picname = STEMP.C_STR (); img. Load (Picname, cximage_format_gif); int inumframes = img. Getnumframes (); cout << "Frames num =" << inumframes << Endl; cximage* newimage = new Cximage (); for (int i = 0; i < inumframes; i++) {newimage->setframe (i); Newimage->load (PICN Ame, Cximage_format_gif), Char tmp[64];sprintf (TMP, "%d", i), string tmp1;tmp1 = Tmp1.insert (0, tmp); TMP1 = Strsavepath + TM P1 + ". png";Stemp = S2ws (TMP1); Temporary buffer is Requiredpicname = Stemp.c_str (); Newimage->save (Picname, cximage_format_png);} if (newimage) delete newimage;} int Traversefolder (const string strFilePath, String strimagenamesets[]) {int iimagecount = 0;_finddata_t Fileinfo;long Handle = _findfirst (Strfilepath.c_str (), &fileinfo), if (handle = = -1l) {Cerr << failed to transfer files ' <& Lt endl;return-1;} Do {//cout << fileinfo.name <<endl;strimagenamesets[iimagecount] = (string) fileinfo.name;iimagecount++;} while (_findnext (handle, &fileinfo) = = 0); return iimagecount;} void Encoding_gif (String strimgpath, String strgifname) {string strimgsets[100] = {};int Iimgcount = Traversefolder ( Strimgpath, strimgsets); string strtmp = Strimgpath.substr (0, strimgpath.find_last_of ("/") + 1); cximage** img = new Cximage*[iimgcount];if (img = = NULL) {cout << "new Cximage error!" << Endl;return;} Std::wstring stemp; LPCWSTR picname;for (int i = 0; i < Iimgcount; i++) {string TMP1;TMP1 = strtmp + strimgsets[i];stemp = S2WS (TMP1); Temporary buffer is Requiredpicname = Stemp.c_str (); Img[i] = new Cximage;img[i]->load (Picname, Cximage_format_bmp) ;//bpp = 1;BPP = 4; BPP = 8;if (0 = = Img[i]->getnumcolors ()) {IMG[I]-&GT;DECREASEBPP (8, True);}} Cxiofile hfile;stemp = S2ws (strgifname); Temporary buffer is Requiredpicname = Stemp.c_str (); string Method = "WB"; std::wstring STEMPMD = S2ws (Method); LPCWSTR Wmethod = Stempmd.c_str (); bool bflag = Hfile.open (Picname, Wmethod); Cximagegif Multiimage;multiimage. Setloops ( -1); multiimage. Setframedelay (Multiimage); Setdisposalmethod (2); multiimage. Encode (&hfile, IMG, Iimgcount, False, False) Hfile.close ();d elete[] img;}

Main test code:

String Strimgpath = "Img/*.bmp"; string strgifname = "Img/test.gif"; Encoding_gif (Strimgpath, strgifname);

The test result is that gif images can be generated . Thank you again !


Third, Creategif

Resources on CSDN:http://download.csdn.net/detail/iamshuke/2567835

Thank you very much! If you have any questions, please feel free to contact us.

This program is based on MFC , for me to use, I do not have MFC.

One of the important files, the other is the calling process:

The main functions are affixed:

BOOL GetData (hbitmap hbmp,byte **pppalette,byte **ppdata,byte *pbitspixel,int *pwidth,int *pheight); void Creategifheard (CFile &file,word nimagewidth,word nimageheight,byte bitspixel); void Addimagetogif (CFile &file , BYTE *pdata,byte *palette,word nimagewidth,word nimageheight,byte bitspixel,word ndelay, short   int Ntransparentcolorindex); void Closegif (CFile &file);

--------------------------------------

If you have any questions, please feel free to contact us!

Thank you very much!

C + + Generate GIF summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.