qimageblitz
interpolate.h
Go to the documentation of this file.00001 #ifndef __PIXIE_INTERPOLATE_H
00002 #define __PIXIE_INTERPOLATE_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <QImage>
00031 #include <QVector>
00032 #include <cmath>
00033
00041 class InlineInterpolate
00042 {
00043 public:
00054 InlineInterpolate(QImage *image, unsigned int background){
00055 img = image; ptr = img->bits();
00056 colorTable = img->colorTable();
00057 w=img->width(); h=img->height();
00058 bg=background; truecolor = img->depth() > 8;
00059 }
00064 inline unsigned int interpolate(float x_offset, float y_offset);
00069 inline unsigned int interpolateBackground(float x_offset, float y_offset);
00070 private:
00071 int w, h;
00072 unsigned int p, q, r, s, bg;
00073 unsigned char *ptr;
00074 QVector<QRgb> colorTable;
00075 bool truecolor;
00076 QImage *img;
00077 };
00078
00079 inline unsigned int InlineInterpolate::interpolate(float x_offset,
00080 float y_offset)
00081 {
00082 int x = qBound(0, (int)x_offset, w-2);
00083 int y = qBound(0, (int)y_offset, h-2);
00084
00085 if(truecolor){
00086 p = *(((QRgb *)ptr)+(y*w)+x);
00087 q = *(((QRgb *)ptr)+(y*w)+x+1);
00088 r = *(((QRgb *)ptr)+((y+1)*w)+x);
00089 s = *(((QRgb *)ptr)+((y+1)*w)+x+1);
00090 }
00091 else{
00092 p = colorTable[*(ptr+(y*w)+x)];
00093 q = colorTable[*(ptr+(y*w)+x+1)];
00094 r = colorTable[*(ptr+((y+1)*w)+x)];
00095 s = colorTable[*(ptr+((y+1)*w)+x+1)];
00096 }
00097 x_offset -= std::floor(x_offset); y_offset -= std::floor(y_offset);
00098 unsigned int alpha = (unsigned int)(255*x_offset);
00099 unsigned int beta = (unsigned int)(255*y_offset);
00100
00101 p = BlitzPrivate::interpolate255(p, 255-alpha, q, alpha);
00102 r = BlitzPrivate::interpolate255(r, 255-alpha, s, alpha);
00103 return(BlitzPrivate::interpolate255(p, 255-beta, r, beta));
00104 }
00105
00106 inline unsigned int InlineInterpolate::interpolateBackground(float x_offset,
00107 float y_offset)
00108 {
00109 int x = (int)x_offset;
00110 int y = (int)y_offset;
00111 p = q = r = s = bg;
00112
00113 if(truecolor){
00114 if(y >= 0 && y < h && x >= 0 && x < w){
00115 p = *(((QRgb *)ptr)+(y*w)+x);
00116 if(y+1 < h) r = *(((QRgb *)ptr)+((y+1)*w)+x);
00117 if(x+1 < w){
00118 q = *(((QRgb *)ptr)+(y*w)+x+1);
00119 if(y+1 < h) q = *(((QRgb *)ptr)+((y+1)*w)+x+1);
00120 }
00121 }
00122 }
00123 else{
00124 if(y >= 0 && y < h && x >= 0 && x < w){
00125 p = colorTable[*(ptr+(y*w)+x)];
00126 if(y+1 < h) r = colorTable[*(ptr+((y+1)*w)+x)];
00127 if(x+1 < w){
00128 q = colorTable[*(ptr+(y*w)+x+1)];
00129 if(y+1 < h) s = colorTable[*(ptr+((y+1)*w)+x+1)];
00130 }
00131 }
00132 }
00133 x_offset -= std::floor(x_offset); y_offset -= std::floor(y_offset);
00134 unsigned int alpha = (unsigned int)(255*x_offset);
00135 unsigned int beta = (unsigned int)(255*y_offset);
00136
00137 p = BlitzPrivate::interpolate255(p, 255-alpha, q, alpha);
00138 r = BlitzPrivate::interpolate255(r, 255-alpha, s, alpha);
00139 return(BlitzPrivate::interpolate255(p, 255-beta, r, beta));
00140 }
00141
00142 #endif
00143