00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef CONV_H
00022 #define CONV_H
00023
00024 #include <climits>
00025 #include "image.h"
00026 #include "imutil.h"
00027 #include "misc.h"
00028
00029 #define RED_WEIGHT 0.299
00030 #define GREEN_WEIGHT 0.587
00031 #define BLUE_WEIGHT 0.114
00032
00033 static image<uchar> *imageRGBtoGRAY(image<rgb> *input) {
00034 int width = input->width();
00035 int height = input->height();
00036 image<uchar> *output = new image<uchar>(width, height, false);
00037
00038 for (int y = 0; y < height; y++) {
00039 for (int x = 0; x < width; x++) {
00040 imRef(output, x, y) = (uchar)
00041 (imRef(input, x, y).r * RED_WEIGHT +
00042 imRef(input, x, y).g * GREEN_WEIGHT +
00043 imRef(input, x, y).b * BLUE_WEIGHT);
00044 }
00045 }
00046 return output;
00047 }
00048
00049 static image<rgb> *imageGRAYtoRGB(image<uchar> *input) {
00050 int width = input->width();
00051 int height = input->height();
00052 image<rgb> *output = new image<rgb>(width, height, false);
00053
00054 for (int y = 0; y < height; y++) {
00055 for (int x = 0; x < width; x++) {
00056 imRef(output, x, y).r = imRef(input, x, y);
00057 imRef(output, x, y).g = imRef(input, x, y);
00058 imRef(output, x, y).b = imRef(input, x, y);
00059 }
00060 }
00061 return output;
00062 }
00063
00064 static image<float> *imageUCHARtoFLOAT(image<uchar> *input) {
00065 int width = input->width();
00066 int height = input->height();
00067 image<float> *output = new image<float>(width, height, false);
00068
00069 for (int y = 0; y < height; y++) {
00070 for (int x = 0; x < width; x++) {
00071 imRef(output, x, y) = imRef(input, x, y);
00072 }
00073 }
00074 return output;
00075 }
00076
00077 static image<float> *imageINTtoFLOAT(image<int> *input) {
00078 int width = input->width();
00079 int height = input->height();
00080 image<float> *output = new image<float>(width, height, false);
00081
00082 for (int y = 0; y < height; y++) {
00083 for (int x = 0; x < width; x++) {
00084 imRef(output, x, y) = imRef(input, x, y);
00085 }
00086 }
00087 return output;
00088 }
00089
00090 static image<uchar> *imageFLOATtoUCHAR(image<float> *input,
00091 float min, float max) {
00092 int width = input->width();
00093 int height = input->height();
00094 image<uchar> *output = new image<uchar>(width, height, false);
00095
00096 if (max == min)
00097 return output;
00098
00099 float scale = UCHAR_MAX / (max - min);
00100 for (int y = 0; y < height; y++) {
00101 for (int x = 0; x < width; x++) {
00102 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
00103 imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
00104 }
00105 }
00106 return output;
00107 }
00108
00109 static image<uchar> *imageFLOATtoUCHAR(image<float> *input) {
00110 float min, max;
00111 min_max(input, &min, &max);
00112 return imageFLOATtoUCHAR(input, min, max);
00113 }
00114
00115 static image<long> *imageUCHARtoLONG(image<uchar> *input) {
00116 int width = input->width();
00117 int height = input->height();
00118 image<long> *output = new image<long>(width, height, false);
00119
00120 for (int y = 0; y < height; y++) {
00121 for (int x = 0; x < width; x++) {
00122 imRef(output, x, y) = imRef(input, x, y);
00123 }
00124 }
00125 return output;
00126 }
00127
00128 static image<uchar> *imageLONGtoUCHAR(image<long> *input, long min, long max) {
00129 int width = input->width();
00130 int height = input->height();
00131 image<uchar> *output = new image<uchar>(width, height, false);
00132
00133 if (max == min)
00134 return output;
00135
00136 float scale = UCHAR_MAX / (float)(max - min);
00137 for (int y = 0; y < height; y++) {
00138 for (int x = 0; x < width; x++) {
00139 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
00140 imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
00141 }
00142 }
00143 return output;
00144 }
00145
00146 static image<uchar> *imageLONGtoUCHAR(image<long> *input) {
00147 long min, max;
00148 min_max(input, &min, &max);
00149 return imageLONGtoUCHAR(input, min, max);
00150 }
00151
00152 static image<uchar> *imageSHORTtoUCHAR(image<short> *input,
00153 short min, short max) {
00154 int width = input->width();
00155 int height = input->height();
00156 image<uchar> *output = new image<uchar>(width, height, false);
00157
00158 if (max == min)
00159 return output;
00160
00161 float scale = UCHAR_MAX / (float)(max - min);
00162 for (int y = 0; y < height; y++) {
00163 for (int x = 0; x < width; x++) {
00164 uchar val = (uchar)((imRef(input, x, y) - min) * scale);
00165 imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
00166 }
00167 }
00168 return output;
00169 }
00170
00171 static image<uchar> *imageSHORTtoUCHAR(image<short> *input) {
00172 short min, max;
00173 min_max(input, &min, &max);
00174 return imageSHORTtoUCHAR(input, min, max);
00175 }
00176
00177 #endif