< dst_height; ++ver) {for(int hor = 0; hor < dst_width; ++hor) {srcX = clip3(int(hor/resizeX + 0.5), 0, src_width - 1);srcY = clip3(int(ver/resizeY + 0.5), 0, src_height - 1);*(dst_image + dst_width * ver + hor) = *(src_image + src_width * srcY + srcX);} }}void resize(const char *input_file, int src_width, int src_height, const char *output_file, int dst_width, int dst_height, int resize_type){ //define and init src buffer int *src_y = new int[src_width * src_height]; int *src_cb = new int[src_width * src_height / 4]; int *src_cr = new int[src_width * src_height / 4]; memset(src_y, 0, sizeof(int) * src_width * src_height); memset(src_cb, 0, sizeof(int) * src_width * src_height / 4); memset(src_cr, 0, sizeof(int) * src_width * src_height / 4); //define and init dst buffer int *dst_y = new int[dst_width * dst_height]; int *dst_cb = new int[dst_width * dst_height / 4]; int *dst_cr = new int[dst_width * dst_height / 4]; memset(dst_y, 0, sizeof(int) * dst_width * dst_height); memset(dst_cb, 0, sizeof(int) * dst_width * dst_height / 4); memset(dst_cr, 0, sizeof(int) * dst_width * dst_height / 4); //define and init mid buffer int *mid_y = new int[dst_width * src_height]; int *mid_cb = new int[dst_width * src_height / 4]; int *mid_cr = new int[dst_width * src_height / 4]; memset(mid_y, 0, sizeof(int) * dst_width * src_height); memset(mid_cb, 0, sizeof(int) * dst_width * src_height / 4); memset(mid_cr, 0, sizeof(int) * dst_width * src_height / 4); uint8 *data_in_8bit = new uint8[src_width * src_height * 3 / 2]; memset(data_in_8bit, 0, sizeof(uint8) * src_width * src_height * 3 / 2); uint8 *data_out_8bit = new uint8[dst_width * dst_height * 3 / 2]; memset(data_out_8bit, 0, sizeof(uint8) * dst_width * dst_height * 3 / 2); FILE *fp_in = fopen(input_file,"rb"); if(NULL == fp_in) {//exit(0);printf("open file failure"); } FILE *fp_out = fopen(output_file, "wb+"); //data read fread(data_in_8bit, sizeof(uint8), src_width * src_height * 3 / 2, fp_in); //Y component for(int ver = 0; ver < src_height; ver++) {for(int hor =0; hor < src_width; hor++){src_y[ver * src_width + hor] = data_in_8bit[ver * src_width + hor];} } //c component YUV420P for(int ver = 0; ver < src_height / 2; ver++) {for(int hor =0; hor < src_width / 2; hor++){src_cb[ver * (src_width / 2) + hor] = data_in_8bit[src_height * src_width + ver * src_width / 2 + hor];src_cr[ver * (src_width / 2) + hor] = data_in_8bit[src_height * src_width + src_height * src_width / 4 + ver * src_width / 2 + hor];} } //resize if(0 == resize_type) {nearestScaler(src_y, dst_y, src_width, src_height, dst_width, dst_height);nearestScaler(src_cb, dst_cb, src_width / 2, src_height / 2, dst_width / 2, dst_height / 2);nearestScaler(src_cr, dst_cr, src_width / 2, src_height / 2, dst_width / 2, dst_height / 2); } else if(1 == resize_type) {bilinearHorScaler(src_y, mid_y, src_width, src_height, dst_width, src_height);bilinearHorScaler(src_cb, mid_cb, src_width / 2, src_height / 2, dst_width / 2, src_height / 2);bilinearHorScaler(src_cr, mid_cr, src_width / 2, src_height / 2, dst_width / 2, src_height / 2);bilinearVerScaler(mid_y, dst_y, dst_width, src_height, dst_width, dst_height);bilinearVerScaler(mid_cb, dst_cb, dst_width / 2, src_height / 2, dst_width / 2, dst_height / 2);bilinearVerScaler(mid_cr, dst_cr, dst_width / 2, src_height / 2, dst_width / 2, dst_height / 2); }else {nearestScaler(src_y, dst_y, src_width, src_height, dst_width, dst_height);nearestScaler(src_cb, dst_cb, src_width / 2, src_height / 2, dst_width / 2, dst_height / 2);nearestScaler(src_cr, dst_cr, src_width / 2, src_height / 2, dst_width / 2, dst_height / 2); } //data write for(int ver = 0; ver < dst_height; ver++) {for(int hor =0; hor < dst_width; hor++){data_out_8bit[ver * dst_width + hor] = clip3(dst_y[ver * dst_width + hor], 0, 255);} } for(int ver = 0; ver < dst_height / 2; ver++) {for(int hor = 0; hor < dst_width / 2; hor++){data_out_8bit[dst_height * dst_width + ver * dst_width / 2 + hor] = clip3(dst_cb[ver * (dst_width / 2) + hor], 0, 255);data_out_8bit[dst_height * dst_width + dst_height * dst_width / 4 + ver * dst_width / 2 + hor] = clip3(dst_cr[ver * (dst_width / 2) + hor], 0, 255);} } fwrite(data_out_8bit, sizeof(uint8), dst_width * dst_height * 3 / 2, fp_out); delete [] src_y; delete [] src_cb; delete [] src_cr; delete [] dst_y; delete [] dst_cb; delete [] dst_cr; delete [] mid_y; delete [] mid_cb; delete [] mid_cr; delete [] data_in_8bit; delete [] data_out_8bit; fclose(fp_in); fclose(fp_out);}resize.h
#ifndef RESIZE_H#define RESIZE_H#include <stdio.h>#include <string.h>typedef unsigned char uint8;typedef unsigned short uint16;int clip3(int data, int min, int max);void bilinearHorScaler(int *src_image, int *dst_image, int src_width, int src_height, int dst_width, int dst_height);void bilinearVerScaler(int *src_image, int *dst_image, int src_width, int src_height, int dst_width, int dst_height);void nearestScaler(int *src_image, int *dst_image, int src_width, int src_height, int dst_width, int dst_height);void resize(const char *input_file, int src_width, int src_height, const char *output_file, int dst_width, int dst_height, int resize_type);#endif
推荐阅读
- 计算机入门必备算法——快速排序法
- 全方位理解哈希算法及其应用,不再迷茫
- 随机数大家都会用,但是你知道生成随机数算法吗?
- linux内核调度算法--快速找到最高优先级进程
- Java内置条件队列应用,实现经典的生产者消费者算法
- 手写最简单的LRU算法
- 删除列表中重复的元素算法
- 掌握这些数学函数,你会在算法效率的分析时经常用到
- 多目标跟踪:监控领域你必须要了解的算法
- 轻量级人脸检测算法实现大盘点