生活随笔
收集整理的這篇文章主要介紹了
LinuxC语言简单实现图片加马赛克-标准IO实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 基于C語言實現,標準IO練習
2. 圖片格式bmp
BMP圖片格式詳解(獲取圖片前54個字節中有用的信息鏈接)
3. 代碼實現
#include <stdio.h>
#include <stdlib.h>
typedef struct
{unsigned int img_size
; unsigned int img_width
; unsigned int img_high
; unsigned short img_bitcount
;
} image_info_t;
typedef struct
{unsigned char b
; unsigned char g
; unsigned char r
;
} point_t;
void show_image_info(image_info_t info
)
{printf("大小size = %d,寬width = %d,高high = %d,素點占bitcount = %d\n",info
.img_size
, info
.img_width
, info
.img_high
, info
.img_bitcount
);
}void get_image_info(FILE
*fp
, image_info_t *info
)
{fseek(fp
, 2, SEEK_SET); fread(&info
->img_size
, 1, 4, fp
); fseek(fp
, 18, SEEK_SET); fread(&info
->img_width
, 1, 4, fp
); fread(&info
->img_high
, 1, 4, fp
); fseek(fp
, 2, SEEK_CUR); fread(&info
->img_bitcount
, 1, 2, fp
);
}void copy_image_file(FILE
*sfp
, FILE
*dfp
)
{int ret
;char buf
[1024] = {0};while (!(feof(sfp
) || ferror(sfp
))){ret
= fread(buf
, 1, sizeof(buf
), sfp
);fwrite(buf
, 1, ret
, dfp
);}return;
}
void set_image_mosaic(FILE
*fp
, image_info_t *info
, int x
, int y
)
{int i
, j
, k
, w
;point_t color
= {0, 0, 0xff}; char *buffer
= (char *)malloc((info
->img_width
) * (info
->img_high
) * 3);fseek(fp
, 54, SEEK_SET);fread(buffer
, 1, (info
->img_size
- 54), fp
);for (i
= 0; i
< info
->img_high
/ y
; i
++){for (j
= 0; j
< info
->img_width
/ x
; j
++){color
= *(point_t *)(buffer
+ (j
* 3 * x
) + (i
* y
* info
->img_width
* 3));for (k
= 0; k
< y
; k
++){for (w
= 0; w
< x
; w
++){*(point_t *)(buffer
+ w
* 3 + (k
* info
->img_width
* 3) +(j
* 3 * x
) + (i
* y
* info
->img_width
* 3)) = color
;}}}}fseek(fp
, 54, SEEK_SET); fwrite(buffer
, 1, (info
->img_size
- 54), fp
);free(buffer
);
}
int main(int argc
, char const *argv
[])
{FILE
*sfp
, *dfp
;int size
;image_info_t info
; char new_name
[20] = {0};if (argc
!= 2){ fprintf(stderr, "input error,try again\n");fprintf(stderr, "usage:./a.out xxxx.bmp\n");return -1;}if ((sfp
= fopen(argv
[1], "r")) == NULL){perror("open error");return -1;}snprintf(new_name
, sizeof(new_name
), "new_%s", argv
[1]);if ((dfp
= fopen(new_name
, "w+")) == NULL) {perror("open error");return -1;}copy_image_file(sfp
, dfp
);get_image_info(dfp
, &info
); show_image_info(info
); set_image_mosaic(dfp
, &info
, 10, 10);fclose(sfp
);fclose(dfp
);return 0;
}
4. 執行
5. 對比
6. 非原創
總結
以上是生活随笔為你收集整理的LinuxC语言简单实现图片加马赛克-标准IO实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。