aboutsummaryrefslogtreecommitdiff
path: root/tools/gfx2crtc/libraw2crtc.c
blob: b7d2f9719f7725aa7c12f5bb7fa466c7892cd2aa (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* GFX2CRTC - libraw2crtc.c
 * CloudStrife - 20080921
 * Diffusé sous licence libre CeCILL v2
 * Voire LICENCE
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned short addrCalc(unsigned char vcc, unsigned char rcc, unsigned char hcc, unsigned char cclk, unsigned char r1, unsigned char r12, unsigned char r13)
{
  unsigned short MA;
  unsigned short addr;

  //MA = vcc*r1 + hcc + (0x0C)*256;
  MA = vcc*r1 + hcc + r12*256 + r13;
  addr = cclk | ((MA & 0x03FF) << 1);
  addr = addr | ((rcc & 0x07) << 11);
  addr = addr | ((MA & 0x3000) << 2);

  return addr;
}

unsigned char mode0interlace(unsigned char *x)
{
  unsigned char mode0pixel[] = {0, 64, 4, 68, 16, 80, 20, 84, 1, 65, 5, 69, 17, 81, 21, 85};
  return mode0pixel[x[0]] << 1 | mode0pixel[x[1]];
}

unsigned char mode1interlace(unsigned char *x)
{
  unsigned char mode1pixel[] = {0, 16, 1, 17};
  return mode1pixel[x[0]] << 3 | mode1pixel[x[1]] << 2 | mode1pixel[x[2]] << 1 | mode1pixel[x[3]];
}

unsigned char mode2interlace(unsigned char *x)
{
  unsigned char out = 0;
  int i;
  for(i = 0; i < 8; i++) out += ((x[7-i]&1) << i);
  return out;
}

unsigned char mode3interlace(unsigned char *x)
{
  unsigned char mode3pixel[] = {0, 16, 1, 17};
  return mode3pixel[x[0]] << 3 | mode3pixel[x[1]] << 2;
}

unsigned char (*ptrMode)(unsigned char *x);

unsigned char *raw2crtc(unsigned char *input, unsigned short width, unsigned short height, unsigned char mode, unsigned char r9, unsigned long *outSize, unsigned char *r1, unsigned char r12, unsigned char r13, unsigned char* reg6)
{
  unsigned char *outBuffer;
  unsigned char *tmpBuffer;
  unsigned char *allocationBuffer;
  unsigned short minAddr = 0;
  unsigned char minAddrIsDefined = 0;
  unsigned short maxAddr;

  unsigned char nbPixPerByte;
  int y,x;

  switch(mode)
  {
    case 0:
    {
      *r1 = (width+3)/4;
      nbPixPerByte = 2;
      ptrMode = mode0interlace;
      break;
    }
    case 1:
    {
      *r1 = (width+7)/8;
      nbPixPerByte = 4;
      ptrMode = mode1interlace;
      break;
    }
    case 2:
    {
      *r1 = (width+15)/16;
      nbPixPerByte = 8;
      ptrMode = mode2interlace;
      break;
    }
    case 3:
    {
      *r1 = (width+3)/4;
      nbPixPerByte = 2;
      ptrMode = mode3interlace;
      break;
    }
    default:
    {
      exit(4);
    }
  }

  tmpBuffer = (unsigned char*)malloc(0xFFFF);
  if (tmpBuffer == NULL)
  {
    printf("Allocation tmpBuffer rat�\n");
    exit(4);
  }

  allocationBuffer = (unsigned char*)calloc(0xFFFF, 1);
  if(allocationBuffer == NULL)
  {
    printf("Allocation allocationBuffer rat�\n");
    exit(4);
  }

  {
	unsigned char r6;
	unsigned char vcc,rcc,hcc,cclk;
	r6 = (height+r9)/(r9+1);
	*reg6 = r6;
	
	for(vcc = 0; vcc < r6; vcc++)
	{
	    for(rcc = 0; rcc < (r9+1); rcc++)
	    {
	      for(hcc = 0; hcc < *r1; hcc++)
	      {
			for(cclk = 0; cclk < 2; cclk++)
			{
			  x = (hcc << 1 | cclk);
			  y = vcc*(r9+1) + rcc;
			  *(tmpBuffer + addrCalc(vcc, rcc, hcc, cclk, *r1, r12, r13)) = (*ptrMode)(input + y*width + x*nbPixPerByte);
			  *(allocationBuffer + addrCalc(vcc, rcc, hcc, cclk, *r1, r12, r13)) += 1;
			}
	      }
	    }
	}
  }

	{
	  unsigned short i;
	  for(i = 0; i < 0xFFFF; i++)
	  {
	    if(*(allocationBuffer + i) > 1)
	    {
	      printf("Attention : Ecriture multiple a l'adresse m�moire %d\n",i);
	    }
	    if(*(allocationBuffer + i) > 0)
	    {
	      maxAddr = i;
	    }
	    if((*(allocationBuffer + i) == 1) && (minAddrIsDefined == 0))
	    {
	      minAddr = i;
	      minAddrIsDefined = 1;
	    }
	  }
	}

  *outSize = (maxAddr + 1) - minAddr;

  outBuffer = (unsigned char*)malloc((*outSize));
  if (outBuffer == NULL)
  {
    printf("Allocation outBuffer rat�\n");
    exit(4);
  }

	{
	  unsigned char *ptrTmp;
	  unsigned char *ptrOut;
	  unsigned short i;
	  ptrTmp = tmpBuffer + minAddr;
	  ptrOut = outBuffer;
	
	  for(i = minAddr; i <= maxAddr; i++)
	  {
	    *(ptrOut++) = *(ptrTmp++);
	  }
	}
  free(tmpBuffer);
  tmpBuffer = NULL;
  free(allocationBuffer);
  allocationBuffer = NULL;
  
  return outBuffer;
}