-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImage.h
More file actions
130 lines (106 loc) · 3.01 KB
/
Image.h
File metadata and controls
130 lines (106 loc) · 3.01 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
////////////////////////////////////////////////////////////////////////
// author: AirSmith
// 2015-10-12 : orignal edition
// author: AirSmith
// 2016-10-18 : add some annotation and an example
////////////////////////////////////////////////////////////////////////
#ifndef IMAGE_H
#define IMAGE_H
#include <iostream>
//TODO
//添加JPG支持
//做一个类似Array类成为公共接口类
namespace img
{
enum ImageType{IMG_COLOR,IMG_GRAY};
//a single pix
typedef struct tag_Pix
{
unsigned char Blue;
unsigned char Green;
unsigned char Red;
}Pix;
//a single element
/*
image would store element as data-structure
when the image is IMG_COLOR the element would orgnized as BGRA
when the image is IMG_GRAY the element would orgnized as a float
we design this to let no matter the image is gray or color
the total zise would be equal to float32
*/
typedef union tag_Element
{
float value;
struct
{
unsigned char Blue;
unsigned char Green;
unsigned char Red;
unsigned char Alpha;
}channels;
}
Element;
//data structure
/*
a refcount counting the refence of same memory block
when the count is 0
it will auto-free the memory block
*/
class DATA
{
public:
int refcount;
ImageType type;
Element* data;
};
class Image
{
public:
//construct a image default is gray-image
Image(ImageType _type=IMG_GRAY);
Image(int _width,int _height,ImageType _type=IMG_GRAY);
Image(int _width,int _height,float* _datas,ImageType _type=IMG_GRAY);
~Image();
//force-free the image
void free();
//create a new image
void create(int _width,int _height,ImageType _type=IMG_GRAY);
//clone the image to current image
void clone(Image &_img);
//overload () to handle the pix at (x,y)
Element* operator() (int _x,int _y);
//overload = to copy a image
void operator = (Image & _img);
int getWidth(){return width;}
int getHeight(){return height;}
ImageType getType(){return datas->type;}
bool isEmpty(){return (!this->datas)?true:false;}
int refCount();
private:
int width;
int height;
DATA* datas;
};
//common method to load image
int imgRead(Image& _imgR,Image& _imgG,Image& _imgB,std::string _name);
int imgRead(Image& _img,std::string _name);
//save image
int imgSave(Image& _imgR,Image& _imgG,Image& _imgB,std::string _name);
int imgSave(Image& _img,std::string _name);
//expand image edge
enum BoarderType{IMG_ZERO,IMG_NEAR};
int makeBroader(Image& _srcImg,Image& _dstImg,BoarderType _type=IMG_NEAR,int _broaderSize=1);
//add noise
enum noiseType{IMG_UNIFORM,IMG_GAUSS,IMG_SALT};
int addNoise(Image& _srcImg,Image& _dstImg,noiseType _type=IMG_GAUSS,float _percent=0.2);
//split image to 3 channel
int split(Image& _srcImg,Image& _imgR,Image& _imgG,Image& _imgB);
//merge 3 channel to single image
int merge(Image& _imgR,Image& _imgG,Image& _imgB,Image& _dstImg);
//conver RGB imgae to gray or otherwise
enum cvtType{IMG_RGB2GRAY};
int cvtColor(Image&_srcImg,Image & _dstImg,cvtType _type=IMG_RGB2GRAY);
//normalize image pix
int normalize(Image& _srcImg,Image& _dstImg,int normalSize=255);
}
#endif