File: pixels.c

package info (click to toggle)
gltron 0.61-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,948 kB
  • ctags: 1,175
  • sloc: ansic: 7,580; perl: 168; makefile: 149; sh: 15
file content (99 lines) | stat: -rw-r--r-- 2,469 bytes parent folder | download
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
#include "gltron.h"

#ifndef TEST
unsigned char* loadPixels(char *filename, gDisplay *d) {
  texture* tex;
  /* load a texture, scale it to the appropriate size */
  tex = loadTextureData(filename);
  return scalePixels(tex->data, 
		     tex->width, tex->height,
		     0, 0,
		     tex->width, tex->height,
		     d->vp_w, d->vp_h, 
		     tex->channels);
}
#endif

unsigned char *scalePixelBitmap(unsigned char *source, int sw, int sh, 
			   int posx, int posy, int width, int height,
			   int dw, int dh, int bytes) {
  int x, y;
  unsigned char *data;
  unsigned char byte;
  int i;

  int row;

  row = dw / 8;
  if(dw % 8) row++;
  
  data = (unsigned char*) malloc( row * dh );
  for(y = 0; y < dh; y++) {
    byte = 0;
    for(x = 0; x < dw; x++) {
      int sx, sy;
      sx = posx + ((x * width) / dw);
      sy = posy + ((y * height) / dh);
      for(i = 0; (i < bytes) && (i < 3); i++) {
	if(source[bytes * (sx + sy * sw) + i] != 0) {
	  /* printf("source value: (%d %d, %d): %d\n", 
	     sx, sy, i, source[bytes * (sx + sy * sw) + i]); */
	  byte |= (1 << (x % 8) );
	  break;
	} else {
	  /* printf("source value: (%d %d, %d): %d\n", 
	     sx, sy, i, source[bytes * (sx + sy * sw) + i]); */
	}
      }
      if((x % 8) == 7) {
	data[x / 8 + y * row] = byte;
      }
    }
    if(x % 8)
      data[x / 8 + y * row] = byte;
    byte = 0;
  }
  return data;
}

unsigned char *scalePixels(unsigned char *source, int sw, int sh, 
			   int posx, int posy, int width, int height,
			   int dw, int dh, int bytes) {
  int x, y;
  unsigned char *data;
  /*
  fprintf(stderr, "scaling from (%d,%d-%d,%d) in a (%d, %d)"
	  "to (%d,%d), bytes: %d\n", posx, posy, posx + width, posy + width,
	  sw, sh, dw, dh, bytes);
  */
  data = (unsigned char*) malloc( dw * dh * bytes );
  for(y = 0; y < dh; y++) {
    for(x = 0; x < dw; x++) {
      int sx, sy, j;
      int sx2, sy2;
      
      sx = posx + ((x * width) / dw);
      sy = posy + ((y * height) / dh);
      (sx == sw - 1) ? (sx2 = sx - 1) : (sx2 = sx + 1);
      (sy == sh - 1) ? (sy2 = sy - 1) : (sy2 = sy + 1);
      /* printf("%d, %d to %d, %d\n", x, y, sx, sy); */
      for(j = 0; j < bytes; j++) {
	data[bytes * (x + y * dw) + j] = 
	  (source[bytes * (sx + sy * sw) + j] +
	   source[bytes * (sx2 + sy * sw) + j] +
	   source[bytes * (sx + sy2 * sw) + j] +
	   source[bytes * (sx2 + sy2 * sw) + j]) / 4;
	// data[x + y * dw + j] = (j != 0) ? 255 : 0;
      }
    }
  }
  return data;
}