C语言编写的数码像框所有代码(jpg.c jpeg.c makefile.sh)
2011-07-21 17:49
387 查看
//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 #endif void 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 parameters struct jpeg_error_mgr jerr; // default JPEG error handler FILE * outfile; // target file handler unsigned char ** buffer; // points to large array of RGB data int row_stride,k; // physical row width in image buffer // create/open output file outfile = fopen(filename, "w+b"); if (outfile == NULL) { printf ("open %s error\n",filename); return -1; } // Set up the normal JPEG error routines cinfo.err = jpeg_std_error(&jerr); // Initialize the JPEG compression object jpeg_create_compress(&cinfo); jpeg_stdio_dest(&cinfo, outfile); // Set parameters for compression cinfo.image_width = widthPtr; // set image width cinfo.image_height = heightPtr; // set image height cinfo.input_components = 3; // number of compents per pixel cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); // set rest of values default jpeg_set_quality(&cinfo, 75, TRUE); // set image quality // Start compressor jpeg_start_compress(&cinfo, TRUE); // Now, write to scan lines as the JPEG is being created // JSAMPLEs per row in image_buffer row_stride = widthPtr * cinfo.input_components; // Create data buffer buffer = 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 data int 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 file jpeg_finish_compress(&cinfo); fclose(outfile); // Delete image buffer free(buffer[0]); free(buffer); // Destroy compression object jpeg_destroy_compress(&cinfo); return 0; }//因为编译的语句太长,我们把它写在makefile.sh文件中,内容如下:
gcc jpg.c jpeg.c -L/user/local/lib -ljpeg
编译时输入:. ./makefile.sh(点和点之间有空格)
编译通过以后输入(纯命令模式):sudo ./a.out
相关文章推荐
- 用C语言编写一个去除C代码中所有的注释的部分
- 【C语言】编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数。
- C语言编写smtp用户代理之代码综合和管理
- 求出小于或等于lim的所有素数并放在aa数组中, 函数返回所求出的素数的个数。C语言编写【20题】
- c语言:编写一个程序,它从标准输入(终端)读取C源代码,并验证所有的花括号都正确的成对出现。
- 编程精粹--编写高质量的C语言代码(2):自己设计并使用断言(一)
- 【数据结构学习之完全从零实现所有数据结构的代码编写之二】智能指针
- 用Python编写MapReduce代码与调用-某一天之前的所有活跃用户统计(1)
- 用Python编写MapReduce代码与调用-某一天之前的所有活跃用户统计(2)
- 【c语言】编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现
- 使用vim在linux下编写C语言代码
- 如何使用C语言来编写MSP430的高质量代码
- 编程精粹--编写高质量C语言代码(5):为子系统设防(二)
- 编写一个删除C语言源程序中的所有注释语句,要正确处理带引号的字符串和字符常量。
- c语言中两种常见代码编写错误,“写入位置 0x00000000 时发生访问冲突”和“语法错误 : 缺少“;”(在“{”的前面)”
- C语言宏定义##连接符和#符的使用(MFC就是靠##自动把消息和消息函数对应起来了,借助宏来减少switch case代码的编写量)
- c语言:编写一个程序,它从标准输入(终端)读取C源代码,并验证所有的花括号都正确的成对出现。
- C语言代码编写技巧(1)
- 编写代码对所有JSP进行预编译
- 如何用C语言编写幂运算的代码?救急!!!