-
Notifications
You must be signed in to change notification settings - Fork 4
/
_makearq.c
168 lines (143 loc) · 4.51 KB
/
_makearq.c
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
/*
* make_arq - A tool for generating Sony A7RIII Pixel-Shift ARQ files
* Copyright (C) 2018 Alberto Griggio <alberto.griggio@gmail.com>
*
* make_arq is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* make_arq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <Python.h>
#include "numpy/arrayobject.h"
#include <stdio.h>
#define color(row, col) ((((row) & 1) << 1) + ((col) & 1))
#define dngcolor(row, col) (0x94949494 >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
static PyObject *get_sony_frame_data(PyObject *self, PyObject *args)
{
int width, height, offset;
int factor, rowstart, colstart;
char *filename;
PyObject *data;
FILE *src;
int r_off, c_off;
int is_dng;
unsigned short *line;
src = NULL;
line = NULL;
if (!PyArg_ParseTuple(args, "Osiiiiiiiii", &data, &filename,
&width, &height, &offset, &factor,
&r_off, &c_off, &rowstart, &colstart, &is_dng)) {
goto err;
}
src = fopen(filename, "rb");
if (!src) {
goto err;
}
if (fseek(src, offset, SEEK_SET) != 0) {
PyErr_SetFromErrno(PyExc_IOError);
goto err;
}
line = (unsigned short *)malloc(width * sizeof(unsigned short));
if (!line) {
PyErr_NoMemory();
goto err;
}
for (int row = 0; row < height; ++row) {
if (fread(line, 2, width, src) != width) {
PyErr_SetString(PyExc_IOError, "fread faliure");
goto err;
}
for (int col = 0; col < width; ++col) {
int rr = (row + r_off) * factor + rowstart;
if (rr >= 0 && rr < height * factor) {
int cc = (col + c_off) * factor + colstart;
if (cc >= 0 && cc < width * factor) {
int c = is_dng ? dngcolor(row, col) : color(row, col);
unsigned short *out =
(unsigned short *)PyArray_GETPTR3(data, rr, cc, c);
*out = line[col];
}
}
}
}
free(line);
fclose(src);
Py_INCREF(Py_None);
return Py_None;
err:
if (line) {
free(line);
}
if (src) {
fclose(src);
}
return NULL;
}
static PyObject *get_fuji_frame_data(PyObject *self, PyObject *args)
{
int factor, rowstart, colstart;
PyObject *data, *im;
int r_off, c_off;
int width, height;
int is_dng;
if (!PyArg_ParseTuple(args, "OiiiOiiiii", &im, &height, &width, &factor,
&data, &r_off, &c_off, &rowstart, &colstart,
&is_dng)) {
goto err;
}
for (int y = 0; y < height; ++y) {
int rr = (y + r_off) * factor + rowstart;
if (rr >= 0 && rr < height * factor) {
for (int x = 0; x < width; ++x) {
int cc = (x + c_off) * factor + colstart;
if (cc >= 0 && cc < width * factor) {
unsigned short *v =
(unsigned short *)PyArray_GETPTR2(im, y, x);
int c = is_dng ? dngcolor(y, x) : color(y, x);
unsigned short *out =
(unsigned short *)PyArray_GETPTR3(data, rr, cc, c);
*out = *v;
}
}
}
}
Py_INCREF(Py_None);
return Py_None;
err:
return NULL;
}
static PyMethodDef _makearq_methods[] = {
{"get_sony_frame_data", (PyCFunction) get_sony_frame_data, METH_VARARGS,
"Get the data stored in one ARW frame"},
{"get_fuji_frame_data", (PyCFunction) get_fuji_frame_data, METH_VARARGS,
"Get the data stored in one RAF frame"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION == 2
void init_makearq(void)
{
Py_InitModule("_makearq", _makearq_methods);
}
#else
static struct PyModuleDef _makearqmodule = {
PyModuleDef_HEAD_INIT,
"_makearq",
NULL,
-1,
_makearq_methods
};
PyMODINIT_FUNC
PyInit__makearq(void)
{
return PyModule_Create(&_makearqmodule);
}
#endif