-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxbuf.c
More file actions
142 lines (123 loc) · 4.29 KB
/
xbuf.c
File metadata and controls
142 lines (123 loc) · 4.29 KB
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
#include "stdlib.h"
#include "xbuf.h"
//#define LOGENABLE
#include "log.h"
Buffer_t * CreateBuffer(int width, int height, int sample_sz) {
Buffer_t* out = (Buffer_t*)malloc(sizeof(Buffer_t));
out->m.height = height;
out->m.width = width;
out->m.sample_sz = sample_sz;
out->m.pitch = out->m.sample_sz * out->m.width;
out->m.size = out->m.pitch * out->m.height;
out->payload = (BYTE*)malloc(out->m.size);
if (!out->payload) {
DBGERROR("out of memory");
FreeBuffer(out);
return NULL;
}
return out;
}
Buffer_t * CreateBuffer2(int width, int height, int sample_sz, int pitch) {
Buffer_t* out = (Buffer_t*)malloc(sizeof(Buffer_t));
out->m.height = height;
out->m.width = width;
out->m.sample_sz = sample_sz;
out->m.pitch = pitch;
out->m.size = out->m.pitch * out->m.height;
out->payload = (BYTE*)malloc(out->m.size);
if (!out->payload) {
DBGERROR("out of memory");
FreeBuffer(out);
return NULL;
}
return out;
}
Buffer_t * CreateBuffer3(int width, int height, int sample_sz, int pitch, void* data) {
Buffer_t* out = (Buffer_t*)malloc(sizeof(Buffer_t));
out->m.height = height;
out->m.width = width;
out->m.sample_sz = sample_sz;
out->m.pitch = pitch;
out->m.size = out->m.pitch * out->m.height;
out->payload = (BYTE*)data;
return out;
}
void FreeBuffer(Buffer_t * buff) {
if (!buff) return;
if (buff->payload) free(buff->payload);
free(buff);
}
int bufFillByte(Buffer_t* buf, BYTE v)
{
// sanity checks
if (!buf) { DBGERROR("bufFill() error: no buffer\n"); return 1; }
// fill payload
memset(buf->payload, v, buf->m.size);
return 0;
}
int bufRectCpy(Buffer_t* buffDst, Point_t dstPoint, Buffer_t* buffSrc, Rect_t srcRect)
{
MemoryRect_t srcOrga = buffSrc->m;
MemoryRect_t dstOrga = buffDst->m;
unsigned long dgbTodoPixelCount = 0;
unsigned long dgbDonePixelCount = 0;
INT X,dstX,srcX;
INT Y,dstY,srcY;
CopyMode_t mode;
// sanity checks
if (!buffSrc) { DBGERROR("bufRectCpy() error: no source\n"); return 1; }
if (!buffDst) { DBGERROR("bufRectCpy() error: no destination\n"); return 1; }
// define copy
srcOrga = buffSrc->m;
dstOrga = buffDst->m;
mode = rectCopyInit(&dstOrga, &dstPoint, &srcOrga, &srcRect, &dgbTodoPixelCount);
DBGMODE(mode);
// Copying...
for (Y = 0; Y < srcRect.height; Y++)
{
void *d;
void *s;
size_t qty;
srcY = srcRect.y + Y;
dstY = dstPoint.y + Y;
switch(mode)
{
case NOCPY:
return 0;
case MEMCPY: {
size_t qty;
srcX = srcRect.x;
dstX = dstPoint.x;
d = buffDst->payload + dstX*dstOrga.sample_sz + dstY*dstOrga.pitch;
s = buffSrc->payload + srcX*srcOrga.sample_sz + srcY*srcOrga.pitch;
qty = srcRect.height*dstOrga.pitch + srcRect.width*dstOrga.sample_sz;
memcpy(d,s,qty);
dgbDonePixelCount+=srcRect.width*srcRect.height;
return 0;
}
case ROWCPY: {
srcX = srcRect.x;
dstX = dstPoint.x;
d = buffDst->payload + dstX*dstOrga.sample_sz + dstY*dstOrga.pitch;
s = buffSrc->payload + srcX*srcOrga.sample_sz + srcY*srcOrga.pitch;
qty = srcRect.width*dstOrga.sample_sz;
memcpy(d,s,qty);
dgbDonePixelCount+=srcRect.width;
break;
}
case PIXCPY:
default:
for (X = 0; X < (srcRect.width); X++) {
Color32_t c={{0}};
srcX = srcRect.x + X;
dstX = dstPoint.x + X;
bufReadPixel(buffSrc, (int)srcX, (int)srcY, &c);
bufWritePixel(buffDst, (int)dstX, (int)dstY, &c);
dgbDonePixelCount++;
}
break;
}
}
DBGMSG("bufRectCpy() %lu/%lu pixels done\n",dgbDonePixelCount,dgbTodoPixelCount);
return 0;
}