forked from jingweizhanghuai/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_to_gray.c
73 lines (56 loc) · 1.33 KB
/
color_to_gray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include "type.h"
void imgCopyMat(ImgMat *src,ImgMat *dst);
void imgMatRedefine(ImgMat *src,int height,int width,int type);
ImgMat *imgCreateMat(int height,int width,char type);
void imgColorToGray(ImgMat *src,ImgMat *dst)
{
if((src->height != dst->height)||(src->width != dst->width)||(dst->type != TYPE_8UC1))
imgMatRedefine(dst,src->height,src->width,TYPE_8UC1);
if(dst->data.ptr == NULL)
imgCreateMatData(dst);
if(src->memory_valid[0]==0)
imgReadMatOCLMemory(src);
int cn;
cn = ((src->type&0xF8)>>3)+1;
if(cn == 1)
{
imgCopyMat(src,dst);
return;
}
int img_size;
img_size = src->width*src->height;
unsigned char *p_src0;
p_src0 = src->data.ptr;
unsigned char *p_src1;
p_src1 = p_src0+img_size;
unsigned char *p_src2;
p_src2 = p_src1+img_size;
unsigned char *p_dst;
p_dst = dst->data.ptr;
int i;
for(i=0;i<img_size;i++)
{
*p_dst = ((*p_src0)*29+(*p_src1)*150+(*p_src2)*77+128)>>8;
p_dst = p_dst+1;
p_src0 = p_src0+1;
p_src1 = p_src1+1;
p_src2 = p_src2+1;
}
dst->memory_valid[0] = 1;
dst->memory_valid[1] = 0;
}
void ColorToGray(ImgMat *src,ImgMat *dst)
{
if(dst==NULL)
{
dst = imgCreateMat(src->height,src->width,TYPE_8UC1);
imgColorToGray(src,dst);
free(src->data.ptr);
free(src->hidinfo);
*src = *dst;
free(dst);
}
else
imgColorToGray(src,dst);
}