imgctl.dllとOpenCVを使ってGIFアニメーションを出力するコードを書いてみた

前に、OpenCVでGIFの読み書きできないのでimgctl.dllを使うというの買いた。
それに加えてGIFアニメーションの出力というのも作ってみた。

コードは以下のように書いた。

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "imgctl.h"
#include "opencv2/opencv.hpp"

int save_GIF_Anime(char *filename,IplImage *image[],int pages,int time1)
{
	HDIB hDibs[255];
	BITMAPINFO bmpInfo;
	unsigned char *bitmapData[255];
	DWORD ret;
	int wd,ht;
	int i,j,p;
	
	if (pages>255) { return 1; /* error: too many pages */ }
	for (p=0;p<pages;p++) {
		wd = image[p]->width;
		ht = image[p]->height;
		bitmapData[p] = (unsigned char *)malloc(wd*ht*3);
		if (bitmapData[p]==NULL) {
			return 1; // error
		}
		ZeroMemory(&bmpInfo, sizeof(bmpInfo));
		bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		bmpInfo.bmiHeader.biWidth = wd;
		bmpInfo.bmiHeader.biHeight = ht;
		bmpInfo.bmiHeader.biPlanes = 1;
		bmpInfo.bmiHeader.biBitCount = 24;
		bmpInfo.bmiHeader.biCompression = BI_RGB;
		for (i=0;i<ht;i++) {
			for (j=0;j<wd;j++) {
				bitmapData[p][ (j+(ht-i-1)*wd)*3  ] = image[p]->imageData[image[p]->widthStep*i+j*3];
				bitmapData[p][ (j+(ht-i-1)*wd)*3+1] = image[p]->imageData[image[p]->widthStep*i+j*3+1];
				bitmapData[p][ (j+(ht-i-1)*wd)*3+2] = image[p]->imageData[image[p]->widthStep*i+j*3+2];
			}
		}
		// create HDIB data
		hDibs[p] = NULL;
		hDibs[p] = CreateDIB(&bmpInfo,bitmapData[p]);
	}
	
	// AnimeGIF output
	ret = DIBtoGIFAni(filename,hDibs,(DWORD)pages,(WORD)time1);
	
	if (!ret) {
		for (p=0;p<pages;p++) {
			DeleteDIB(hDibs[p]);
		}
		return 1; // Error: output failed
	}
	for (p=0;p<pages;p++) {
		free(bitmapData[p]);
		DeleteDIB(hDibs[p]);
	}
	return 0;
}

int main()
{
	IplImage *images[255];
	char filename1[255];
	int i,ret;
	int page_num,timer;
	
	page_num = 10; // Number of JPEGs
	timer = 50;    // GIF Anime play time per frame  .. (unit == 1/100sec)

	// load JPEG
	for (i=0;i<page_num;i++) {
		sprintf(filename1,"test%03d.jpg",i);
		images[i] = cvLoadImage(filename1, CV_LOAD_IMAGE_COLOR);
		if (images[i]==NULL) { printf("Error: file not found.\n"); return 1; }
	}
	
	// output to GIF anime
	ret = save_GIF_Anime("output.gif",images,page_num,timer);
	if (ret!=0) { printf("Error\n"); return 1; }
	
	return 0;
}

連番のJPGファイルを読み込んで、GIFアニメーションのGIFファイルを出力するサンプルコードだ。

これを元にして、全天周カメラTHETAの全天周画像から風景を見回すアニメーションをGIFファイルのサムネイルとして作成するソフトというのを作り中だったりする。




コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

Time limit is exhausted. Please reload CAPTCHA.

9 + 1 =