Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

imconv.h

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

Generated on Wed Oct 5 14:36:12 2005 for RobotFlow by  doxygen 1.4.4