//jpg.c文件中的代码,当前目录下需要创建一个image文件夹,文件中放的都是图片,通过读这个目录(readdir),获取图片名,我发现每次读取image这个文件中的图片
都是一样的,因而我是按照它读取目录这个顺序一样给这些图片做的特效,而不是随即获取图片来制作特效,另外,这里面我自己添加了通过点击鼠标的左键或右键来切换图片,
我还想再加一个播放音频的,但是手写这个程序耗时太长,就随便调用了系统中的播放器,但是在纯命令界面是没用的(ctrl+alt+(F1~F6)进入纯命令界面,ctrl+alt+F7
退出该界面,本人制作的数码特效很简单)
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <math.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#ifndef u_char
#define u_char unsigned char
#endif
struct fb_var_screeninfo fb_var;
int width,height;
int * fb_ptr;
short pic_w,pic_h;
u_char *ptr;
int init_fb()
{
int fb,bpp;
fb = open("/dev/fb0",O_RDWR);
if(fb < 0)
{
printf("Cant open device fb0!\n");
printf("Try:sudo\n");
return -1;
}
ioctl(fb,FBIOGET_VSCREENINFO,&fb_var);
width = fb_var.xres;
height = fb_var.yres;
bpp = fb_var.bits_per_pixel;
printf("Framebuffer: %d * %d %d \n",width,height,bpp);
fb_ptr = mmap(NULL,width*height*bpp/8,PROT_READ|PROT_WRITE,MAP_SHARED,fb,0);
return 0;
}
void readfilename(char *dirname)
{
int i,signal,x,y;
DIR *p = opendir(dirname);
if(p == NULL)
{
printf("open dir error!\n");
return ;
}
struct dirent * pd;
for( i = 0; (pd = readdir(p)) != NULL;)
{
if(pd->d_name[0] == '.') continue;
printf("name: %s\n",pd->d_name);
char file_name[255+4] = {0};
//memset(file_name,0,sizeof(file_name));
strcat(file_name,dirname);
if(strstr(pd->d_name,".jpeg") != NULL || strstr(pd->d_name,".jpg") != NULL)
{
strcat(file_name,pd->d_name);
ptr = (u_char *)decode_jpeg(file_name,&pic_w,&pic_h);
printf("pic_w: %d,pic_h: %d\n",pic_w,pic_h);
signal = readmouse();
system("clear");
if(signal == 9 || signal == 10)
{
if(strstr(pd->d_name,"3.jpg") != NULL)
pic_one();
if(strstr(pd->d_name,"4.jpg") != NULL)
pic_two();
if(strstr(pd->d_name,"1.jpg") != NULL)
pic_three();
if(strstr(pd->d_name,"0.jpg") != NULL)
pic_four();
if(strstr(pd->d_name,"2.jpg") != NULL)
pic_five();
i++;
}
}
}
system("killall totem");
}
int pic_one()
{
int x,y;
u_char *tmp;
unsigned char red,green,blue;
unsigned int color;
tmp = ptr;
for( y = 0; y < pic_h; y++)
{
for( x = 0; x < pic_w; x++)
{
red = *tmp++;
green = *tmp++;
blue = *tmp++;
color = red << 16 | green << 8 | blue;
*(fb_ptr+x+y*width) = color;
}
usleep(10000);
}
return 0;
}
int pic_two()
{
int flag = 0;
int x,y;
u_char *tmp;
unsigned char red,green,blue;
unsigned int color;
tmp = ptr;
for( y = 0; y < pic_h; y++)
{
for( x = 0; x < pic_w; x++)
{
if (flag == 0)
{
tmp = ptr + pic_w*pic_h*3-1;
flag =1;
}
blue = *tmp--;
green = *tmp--;
red = *tmp--;
color = red << 16 | green << 8 | blue;
*(fb_ptr+width-x-1+(height-y-1)*width) = color;
}
usleep(10000);
}
return 0;
}
int pic_three()
{
int x,y;
u_char *tmp;
unsigned char red,green,blue;
unsigned int color;
tmp = ptr;
for( y = 0; y < pic_h; y++)
{
for( x = 0; x < pic_w; x++)
{
if(x < pic_w/4 || (x >= pic_w/2 && x < pic_w*3/4))
{
tmp = ptr+x*3+y*width*3;
red = *tmp++;
green = *tmp++;
blue = *tmp++;
color = red << 16 | green << 8 | blue;
*(fb_ptr+x+y*width) = color;
}
else
{
tmp = ptr+(pic_h-y-1)*width*3+x*3;
red = *tmp++;
green = *tmp++;
blue = *tmp++;
color = red << 16 | green << 8 | blue;
*(fb_ptr+x+(pic_h-y-1)*width) = color;
}
}
usleep(10000);
}
return 0;
}
int pic_four()
{
int x,y,w,h,number,k,l;
u_char *tmp;
unsigned char red,green,blue;
unsigned int color;
w = pic_w/8;
h = pic_h/5;
for(number = 0; number < 40; number++)
{
k = number/8;
l = number%8;
tmp = ptr;
for(y = 0; y < h; y++)
{
for(x = 0; x < w; x++)
{
tmp = ptr+(y+(h-1)*k)*pic_w*3+(x+l*w)*3;
red = *tmp++;
green = *tmp++;
blue = *tmp++;
color = red << 16 | green << 8 | blue;
*(fb_ptr+x+80+l*w+(y+50+(h-1)*k)*width) = color;
}
usleep(10000);
}
}
return 0;
}
int pic_five()
{
int x,y,w,h;
int k = 0;
u_char *tmp;
unsigned char red,green,blue;
unsigned int color;
tmp = ptr;
for( w = 0; w <= pic_w/2; w++)
{
for( h = 0; h < pic_h; h++)
{
if(k == 0){
tmp = ptr+pic_w/2*3+h*pic_w*3;
red = *tmp++;
green = *tmp++;
blue = *tmp++;
color = red << 16 | green << 8 | blue;
*(fb_ptr+pic_w/2+h*width) = color;
k = 1;}
else{
tmp = ptr+(w+pic_w/2)*3+h*pic_w*3;
red = *tmp++;
green = *tmp++;
blue = *tmp++;
color = red << 16 | green << 8 | blue;
*(fb_ptr+w+pic_w/2+h*width) = color;
tmp = ptr+(pic_w/2-w-1)*3+h*pic_w*3;
red = *tmp++;
green = *tmp++;
blue = *tmp++;
color = red << 16 | green << 8 | blue;
*(fb_ptr+pic_w/2-1-w+h*width) = color;}
}
usleep(10000);
}
return 0;
}
int openmouse()
{
int fb;
fb = open("/dev/input/mice",O_RDONLY);
if( fb < 0)
{
printf("open file error!\n");
return ;
}
return fb;
}
int readmouse()
{
int fb,data,i;
static unsigned char buff[8] = {0};
fb = openmouse();
while((data = read(fb,buff,8)) > 0)
{
printf("data = %d\n",data);
for( i = 0; i < data; i++)
{
printf("buff[%d] = %d\t",i,(int)buff[i]);
if(buff[0] == 9 || buff[0] == 10)
{
return buff[0];
}
}
}
}
int main(int argc,char *argv[])
{
system("/usr/bin/totem /home/akaedu/desktop/jpeg/music/bye.mp3");
init_fb();
readfilename("./image/");
return 0;
}
//老师提供的解码图片数据的程序jpeg.c,代码如下:
/*Copyright George Peter Staplin 2003*//*You may use/copy/modify/distribute this software for any purpose *provided that I am given credit and you don't sue me for any bugs. *//*Please contact me using GeorgePS@XMission.com if you like this, or *have questions. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <jpeglib.h>#include <jerror.h>#include <time.h>#ifndef u_char#define u_char unsigned char#endifvoid jpeg_error_exit (j_common_ptr cinfo) { cinfo->err->output_message (cinfo); exit (EXIT_FAILURE);}/*This returns an array for a 24 bit image.*/u_char *decode_jpeg (char * filename, short *widthPtr, short *heightPtr){ register JSAMPARRAY lineBuf; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr err_mgr; int bytesPerPix; FILE *inFile; u_char *retBuf; inFile = fopen (filename, "rb"); if (NULL == inFile) { printf ("Open file error %s\n",filename); return NULL; } cinfo.err = jpeg_std_error (&err_mgr); err_mgr.error_exit = jpeg_error_exit; jpeg_create_decompress (&cinfo); jpeg_stdio_src (&cinfo, inFile); jpeg_read_header (&cinfo, 1); cinfo.do_fancy_upsampling = 0; cinfo.do_block_smoothing = 0; jpeg_start_decompress (&cinfo); *widthPtr = cinfo.output_width; *heightPtr = cinfo.output_height; bytesPerPix = cinfo.output_components; lineBuf = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE, (*widthPtr * bytesPerPix), 1); retBuf = (u_char *) malloc (3 * (*widthPtr * *heightPtr)); if (NULL == retBuf) { perror (NULL); return NULL; } if (3 == bytesPerPix) { int y; for (y = 0; y < cinfo.output_height; ++y) { jpeg_read_scanlines (&cinfo, lineBuf, 1); memcpy ((retBuf + y * *widthPtr * 3),lineBuf[0],3 * *widthPtr);} } else if (1 == bytesPerPix) { unsigned int col;int lineOffset = (*widthPtr * 3);int lineBufIndex;int x ;int y;for (y = 0; y < cinfo.output_height; ++y) { jpeg_read_scanlines (&cinfo, lineBuf, 1); lineBufIndex = 0; for (x = 0; x < lineOffset; ++x) { col = lineBuf[0][lineBufIndex]; retBuf[(lineOffset * y) + x] = col; ++x; retBuf[(lineOffset * y) + x] = col; ++x; retBuf[(lineOffset * y) + x] = col; ++lineBufIndex; } } } else {fprintf (stderr, "Error: the number of color channels is %d. This program only handles 1 or 3\n", bytesPerPix);return NULL; } jpeg_finish_decompress (&cinfo); jpeg_destroy_decompress (&cinfo); fclose (inFile); return retBuf;}int encode_jpeg(const char * filename, unsigned char * rgbbuf,short widthPtr, short heightPtr){struct jpeg_compress_struct cinfo;// JPEG image parametersstruct jpeg_error_mgr jerr;// default JPEG error handlerFILE * outfile;// target file handlerunsigned char ** buffer;// points to large array of RGB dataint row_stride,k;// physical row width in image buffer// create/open output fileoutfile = fopen(filename, "w+b");if (outfile == NULL){ printf ("open %s error\n",filename);return -1;}// Set up the normal JPEG error routinescinfo.err = jpeg_std_error(&jerr);// Initialize the JPEG compression objectjpeg_create_compress(&cinfo);jpeg_stdio_dest(&cinfo, outfile);// Set parameters for compressioncinfo.image_width = widthPtr; // set image widthcinfo.image_height = heightPtr;// set image heightcinfo.input_components = 3;// number of compents per pixelcinfo.in_color_space = JCS_RGB;jpeg_set_defaults(&cinfo);// set rest of values defaultjpeg_set_quality(&cinfo, 75, TRUE);// set image quality// Start compressorjpeg_start_compress(&cinfo, TRUE);// Now, write to scan lines as the JPEG is being created// JSAMPLEs per row in image_bufferrow_stride = widthPtr * cinfo.input_components;// Create data bufferbuffer = malloc (cinfo.image_height*4);buffer[0] = malloc(cinfo.image_height * row_stride);for (k = 0; k < (int)(cinfo.image_height); k++) {buffer[k] = buffer[0] + row_stride*k;}// Load input buffer with dataint i, j;for (j = 0; j < heightPtr; j++){for (i = 0; i < widthPtr*3; i += 3){buffer[j][i] = rgbbuf[j*row_stride+i];buffer[j][i+1] = rgbbuf[j*row_stride+i+1];buffer[j][i+2] = rgbbuf[j*row_stride+i+2];}}while (cinfo.next_scanline < cinfo.image_height) // jpeg_write_scanlines expects an array of pointers to scanlines. // Here the array is only one element long, but you could pass // more than one scanline at a time if that's more convenient.{ (void) jpeg_write_scanlines(&cinfo, &buffer[cinfo.next_scanline], 1);}// Complete compression and close output filejpeg_finish_compress(&cinfo);fclose(outfile);// Delete image bufferfree(buffer[0]);free(buffer);// Destroy compression objectjpeg_destroy_compress(&cinfo);return 0;}
//因为编译的语句太长,我们把它写在makefile.sh文件中,内容如下:
gcc jpg.c jpeg.c -L/user/local/lib -ljpeg
编译时输入:. ./makefile.sh(点和点之间有空格)
编译通过以后输入(纯命令模式):sudo ./a.out