version 2.8
image_util.c
1 #include "zgl.h"
2 
3 /*
4  * image conversion
5  */
6 
7 void gl_convertRGB_to_5R6G5B(unsigned short *pixmap,unsigned char *rgb,
8  int xsize,int ysize)
9 {
10  int i,n;
11  unsigned char *p;
12 
13  p=rgb;
14  n=xsize*ysize;
15  for(i=0;i<n;i++) {
16  pixmap[i]=((p[0]&0xF8)<<8) | ((p[1]&0xFC)<<3) | ((p[2]&0xF8)>>3);
17  p+=3;
18  }
19 }
20 
21 void gl_convertRGB_to_8A8R8G8B(unsigned int *pixmap, unsigned char *rgb,
22  int xsize, int ysize)
23 {
24  int i,n;
25  unsigned char *p;
26 
27  p=rgb;
28  n=xsize*ysize;
29  for(i=0;i<n;i++) {
30  pixmap[i]=(((unsigned int)p[0])<<16) |
31  (((unsigned int)p[1])<<8) |
32  (((unsigned int)p[2]));
33  p+=3;
34  }
35 }
36 
37 /*
38  * linear interpolation with xf,yf normalized to 2^16
39  */
40 
41 #define INTERP_NORM_BITS 16
42 #define INTERP_NORM (1 << INTERP_NORM_BITS)
43 
44 static inline int interpolate(int v00,int v01,int v10,int xf,int yf)
45 {
46  return v00+(((v01-v00)*xf + (v10-v00)*yf) >> INTERP_NORM_BITS);
47 }
48 
49 
50 /*
51  * TODO: more accurate resampling
52  */
53 
54 void gl_resizeImage(unsigned char *dest,int xsize_dest,int ysize_dest,
55  unsigned char *src,int xsize_src,int ysize_src)
56 {
57  unsigned char *pix,*pix_src;
58  float x1,y1,x1inc,y1inc;
59  int xi,yi,j,xf,yf,x,y;
60 
61  pix=dest;
62  pix_src=src;
63 
64  x1inc=(float) (xsize_src - 1) / (float) (xsize_dest - 1);
65  y1inc=(float) (ysize_src - 1) / (float) (ysize_dest - 1);
66 
67  y1=0;
68  for(y=0;y<ysize_dest;y++) {
69  x1=0;
70  for(x=0;x<xsize_dest;x++) {
71  xi=(int) x1;
72  yi=(int) y1;
73  xf=(int) ((x1 - floor(x1)) * INTERP_NORM);
74  yf=(int) ((y1 - floor(y1)) * INTERP_NORM);
75 
76  if ((xf+yf) <= INTERP_NORM) {
77  for(j=0;j<3;j++) {
78  pix[j]=interpolate(pix_src[(yi*xsize_src+xi)*3+j],
79  pix_src[(yi*xsize_src+xi+1)*3+j],
80  pix_src[((yi+1)*xsize_src+xi)*3+j],
81  xf,yf);
82  }
83  } else {
84  xf=INTERP_NORM - xf;
85  yf=INTERP_NORM - yf;
86  for(j=0;j<3;j++) {
87  pix[j]=interpolate(pix_src[((yi+1)*xsize_src+xi+1)*3+j],
88  pix_src[((yi+1)*xsize_src+xi)*3+j],
89  pix_src[(yi*xsize_src+xi+1)*3+j],
90  xf,yf);
91  }
92  }
93 
94  pix+=3;
95  x1+=x1inc;
96  }
97  y1+=y1inc;
98  }
99 }
100 
101 #define FRAC_BITS 16
102 
103 /* resizing with no interlating nor nearest pixel */
104 
105 void gl_resizeImageNoInterpolate(unsigned char *dest,int xsize_dest,int ysize_dest,
106  unsigned char *src,int xsize_src,int ysize_src)
107 {
108  unsigned char *pix,*pix_src,*pix1;
109  int x1,y1,x1inc,y1inc;
110  int xi,yi,x,y;
111 
112  pix=dest;
113  pix_src=src;
114 
115  x1inc=(int)((float) ((xsize_src)<<FRAC_BITS) / (float) (xsize_dest));
116  y1inc=(int)((float) ((ysize_src)<<FRAC_BITS) / (float) (ysize_dest));
117 
118  y1=0;
119  for(y=0;y<ysize_dest;y++) {
120  x1=0;
121  for(x=0;x<xsize_dest;x++) {
122  xi=x1 >> FRAC_BITS;
123  yi=y1 >> FRAC_BITS;
124  pix1=pix_src+(yi*xsize_src+xi)*3;
125 
126  pix[0]=pix1[0];
127  pix[1]=pix1[1];
128  pix[2]=pix1[2];
129 
130  pix+=3;
131  x1+=x1inc;
132  }
133  y1+=y1inc;
134  }
135 }
136