在我們IT行業每天面對的就是敲程式碼,所以很多人無法接受這份工作,因為很無聊也很枯燥,長期工作會使人情緒低落,其實我們程式設計很多時候也有有趣的地方,接下來我就用一個簡單的c語言作圖來緩解一下氣氛。
新的一年開始了,是時候作出改變了。
以下為用C語言畫心形的三種方式(附程式碼)
畫心1
關於%*。*s
小數點。後“*”表示輸出位數,具體的資料來自引數表
printf格式字串中,與寬度控制和精度控制有關的常量都可以換成變數,方法就是使用一個“*”代替那個常量,然後在後面提供變數給“*”。
同樣,小數點。前也可以新增*,也要使用者輸入一個位寬值來代替,表示輸出的字元所佔位寬。
也就是說,前面定義輸出總寬度,後面定義輸出字元個數。
printf(“%*。*s\n”, 50, 3, a); // 50表示此次輸出佔位寬,
//3表示輸出a陣列的三個字元
畫心2
畫心3
Linux上透過framebuffer將jpeg圖片畫在螢幕上
安裝JPEG庫
1。解壓jpeg原始碼 tar -xzvf jpegsrc。v8a。tar。gz
2。在/home/xxx下新建jpeg目錄 mkdir jpeg
3。進入jpeg原始碼目錄jpeg-8a cd jpeg-8a
4。生成makefile指令碼 。/configure ——prefix=/home/xxx/jpeg
5。編譯 make
6。安裝 make install
安裝完畢後就可以在/home/xxx/jpeg目錄下看到 jpeg解碼庫
配置JPEG庫
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include “jpeglib。h”
typedef struct Tag_RGB
{
unsigned char ucRed;
unsigned char ucGreen;
unsigned char ucBlue;
}St_RGB;
int main (int agrc, char *argv[])
{
int fp=0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
fp = open(“/dev/fb0”,O_RDWR);
if (fp < 0)
{
printf(“Error : Can not open framebuffer device\n”);
exit(EXIT_FAILURE);
}
if (ioctl(fp,FBIOGET_FSCREENINFO,&finfo))
{
printf(“Error reading fixed information\n”);
exit(EXIT_FAILURE);
}
if (ioctl(fp,FBIOGET_VSCREENINFO,&vinfo))
{
printf(“Error reading variable information\n”);
exit(EXIT_FAILURE);
}
printf(“The mem is :%d\n”, finfo。smem_len);
printf(“The line_length is :%d\n”, finfo。line_length);
printf(“The xres is :%d\n”, vinfo。xres);
printf(“The yres is :%d\n”, vinfo。yres);
printf(“bits_per_pixel is :%d\n”, vinfo。bits_per_pixel);
unsigned long screensize = 0;
screensize = vinfo。xres * vinfo。yres * vinfo。bits_per_pixel / 8;
//這就是把fp所指的檔案中從開始到screensize大小的內容給映射出來,
//得到一個指向這塊空間的指標
unsigned char *fbp =(unsigned char *)mmap(0, screensize, PROT_READ|PROT_WRITE
, MAP_SHARED, fp, 0);
if (fbp == (unsigned char*)-1)
{
printf (“Error: failed to map framebuffer device to memory。/n”);
exit (EXIT_FAILURE);
}
unsigned int location = 0;
struct jpeg_decompress_struct jinfo;
struct jpeg_error_mgr jerr;
/*Bind error handler*/
jinfo。err = jpeg_std_error(&jerr);
/*init jpeg decompress object*/
jpeg_create_decompress(&jinfo);
/*Bind jpg data object*/
FILE * pfJPG = fopen(argv[1], “rb”);
if(NULL == pfJPG)
{
printf(“open %s failed。 error->%s\n”, argv[1], strerror(errno));
jpeg_destroy_decompress(&jinfo);
}
else
{
jpeg_stdio_src(&jinfo, pfJPG);
jpeg_read_header(&jinfo, TRUE);
/*Start decompressing*/
jpeg_start_decompress(&jinfo);
/*Only get decompress arguments*/
//jpeg_calc_output_dimensions(&jinfo);
printf(“Output Width = %d\n”,jinfo。output_width);
printf(“Output Height = %d\n”,jinfo。output_height);
//Color Channel
printf(“Output Components = %d\n”,jinfo。output_components);
unsigned char** ppucRowData = NULL;
ppucRowData = (*jinfo。mem->alloc_sarray)((j_common_ptr) &jinfo, JPOOL_IMAGE
, jinfo。output_width * jinfo。output_components, 1);
unsigned int i = 0;
unsigned int j = 0;
St_RGB stColor = ;
while (jinfo。output_scanline < jinfo。output_height) //jinfo。output_height
{
jpeg_read_scanlines(&jinfo, ppucRowData, 1);
for (i=0; i
{
location = i*(vinfo。bits_per_pixel/8)+j*finfo。line_length;
stColor。ucRed = ppucRowData[0][i*jinfo。output_components];
stColor。ucGreen = ppucRowData[0][i*jinfo。output_components+1];
stColor。ucBlue = ppucRowData[0][i*jinfo。output_components+2];
/*直接賦值來改變螢幕上某點的顏色*/
*(fbp + location) = stColor。ucBlue;
*(fbp + location + 1) = stColor。ucGreen;
*(fbp + location + 2) = stColor。ucRed;
*(fbp + location + 3) = 0; /*是否透明*/
}
j++;
}
/*Finish Decompress*/
jpeg_finish_decompress(&jinfo);
/*Destroy Decompress Object*/
jpeg_destroy_decompress(&jinfo);
fclose(pfJPG);
}
munmap (fbp, screensize); /*解除對映*/
close (fp);
return 0;
}
編譯:gcc heart。c -o ourheart -ljpeg
執行:。/ourheart