-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.cc
122 lines (100 loc) · 2.52 KB
/
misc.cc
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
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ios>
#include <sys/stat.h>
#include "typedefs.h"
#include "misc.h"
// Saves a PIC to TGA-file.
int SaveTGA(PIC* pic, char* name, int style)
{
int i = 0, j = 0, k = 0, l = 0, x = 0, y = 0;
fstream tga;
tga.open(name, BINARY | WRITE | TRUNC);
if (tga.fail()) {
tga.close();
return (PIC_ERROPEN);
}
x = pic->x * pic->xn;
y = pic->y * pic->yn;
BYTE header[] = {
0, 0, 2,
0, 0, 0, 0, 0,
0, 0, 0, 0,
x & 0xff, x >> 8, y & 0xff, y >> 8,
24, 0
};
BYTE* temp = new BYTE[x * y * 3];
for (i = 0; i < y; i++) {
k = x * 3 - 1;
l = i * x * 3;
for (j = 0; j < (x * 3); j += 3) {
temp[l + j] = pic->pic[l + k - 2];
temp[l + j + 1] = pic->pic[l + k - 1];
temp[l + j + 2] = pic->pic[l + k];
k -= 3;
}
}
tga.write((char*)&header, sizeof(header));
tga.write((char*)temp, x * y * 3);
delete temp;
tga.close();
return (PIC_OK);
}
// Load a PCX-file into buffer and set size into x and y.
// xn and yn are set to 1.
int LoadPCX(PIC *pic, char *filename, int style)
{
int xsize, ysize, tavu1, tavu2, position = 0;
FILE *handle = fopen(filename, "rb");
BYTE *tmp;
BYTE *buffer;
int fpos;
struct stat st;
if (handle == (FILE *) - 1) {
return (PIC_ERROPEN);
}
fstat(fileno(handle), &st);
tmp = new BYTE[st.st_size];
fread(tmp, st.st_size, 1, handle);
fclose(handle);
fpos = 8;
xsize = tmp[fpos] + (int)(tmp[fpos + 1] << 8) + 1; fpos += 2;
ysize = tmp[fpos] + (int)(tmp[fpos + 1] << 8) + 1; fpos += 2;
buffer = new BYTE[xsize * ysize];
fpos = 128;
while (position < (xsize * ysize)) {
tavu1 = tmp[fpos++];
if (tavu1 > 192) {
tavu2 = tmp[fpos++];
for (; tavu1 > 192; tavu1--) buffer[position++] = tavu2;
} else buffer[position++] = tavu1;
}
pic->x = xsize;
pic->xn = 0;
pic->y = ysize;
pic->yn = 0;
pic->pic = buffer;
delete tmp;
return (PIC_OK);
}
int ConvTextToInt(char* text, int start, int lenght)
{
int multiplier = 1, count = 0, pw = 0, result = 0, stop = 0;
if (text[start] == '-') {
multiplier = -1;
count++;
}
int loop0 = lenght;
for (loop0 = lenght; loop0 > 0; loop0--) {
if ((text[start + count + loop0 - 1] == ' ') && (pw != 0)) stop = 1;
if ((text[start + count + loop0 - 1] != ' ') && (stop == 0)) {
result += (int)(pow(10, pw) * (text[start + count + loop0 - 1] - CHARZERO));
pw++;
}
}
result *= multiplier;
return (result);
}