Skip to content

Commit d1489d0

Browse files
committed
JPEG header gen almost working
1 parent ec25265 commit d1489d0

File tree

8 files changed

+131
-2671
lines changed

8 files changed

+131
-2671
lines changed

source/application/jpeg_header/Makefile

Lines changed: 0 additions & 20 deletions
This file was deleted.

source/application/jpeg_header/footer.bin

Lines changed: 0 additions & 1 deletion
This file was deleted.

source/application/jpeg_header/jhdr.c

Lines changed: 0 additions & 46 deletions
This file was deleted.

source/application/jpeg_header/jhdr.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

source/application/jpeg_header/main.c

Lines changed: 0 additions & 14 deletions
This file was deleted.

source/application/lua_libraries/camera.c

Lines changed: 130 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <stdint.h>
2828
#include "error_logging.h"
2929
#include "i2c.h"
30-
#include "jpeg.h"
3130
#include "lauxlib.h"
3231
#include "lua.h"
3332
#include "nrfx_systick.h"
@@ -245,40 +244,6 @@ static int lua_camera_image_ready(lua_State *L)
245244
return 1;
246245
}
247246

248-
static int scale_mult(int q, float scale)
249-
{
250-
float t = (scale * q + 50) / 100; // Round
251-
if (t < 1) // Prevent divide by 0 error
252-
t = 1;
253-
else if (t > 255) // Prevent overflow
254-
t = 255;
255-
return (int)t;
256-
}
257-
258-
static void generate_jpeg_header(int resolution, int qf, uint8_t *header_data)
259-
{
260-
uint8_t header[] = {255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 2, 0, 0, 100, 0, 100, 0, 0, 255, 219, 0, 67, 0, 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99, 255, 219, 0, 67, 1, 17, 18, 18, 24, 21, 24, 47, 26, 26, 47, 99, 66, 56, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 255, 192, 0, 17, 8, 0, 0, 0, 0, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 31, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 196, 0, 181, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 50, 129, 8, 20, 66, 145, 161, 177, 193, 9, 35, 51, 82, 240, 21, 98, 114, 209, 10, 22, 36, 52, 225, 37, 241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 130, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0};
261-
262-
float scale;
263-
264-
if (qf < 50)
265-
scale = 5000 / qf;
266-
else
267-
scale = 200 - 2 * qf;
268-
269-
for (int i = 25; i <= 88; i++)
270-
header[i] = scale_mult(header[i], scale);
271-
for (int i = 93; i <= 156; i++)
272-
header[i] = scale_mult(header[i], scale);
273-
274-
header[163] = (resolution >> 8) & 0xff;
275-
header[164] = resolution & 0xff;
276-
header[165] = (resolution >> 8) & 0xff;
277-
header[166] = resolution & 0xff;
278-
279-
memcpy(header_data, header, sizeof(header));
280-
}
281-
282247
static int lua_camera_read(lua_State *L)
283248
{
284249
lua_Integer bytes_requested = luaL_checkinteger(L, 1);
@@ -297,12 +262,140 @@ static int lua_camera_read(lua_State *L)
297262
luaL_error(L, "bytes requested is too large");
298263
}
299264

300-
uint8_t *header = malloc(623);
301-
generate_jpeg_header(capture_settings.resolution, capture_settings.quality_factor, header);
265+
uint8_t header[] = {
266+
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46,
267+
0x49, 0x46, 0x00, 0x01, 0x02, 0x00, 0x00, 0x64,
268+
0x00, 0x64, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
269+
0x00, 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10,
270+
0x0e, 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18,
271+
0x28, 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23,
272+
0x25, 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39,
273+
0x33, 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40,
274+
0x44, 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51,
275+
0x57, 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d,
276+
0x71, 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67,
277+
0x63, 0xff, 0xdb, 0x00, 0x43, 0x01, 0x11, 0x12,
278+
0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a, 0x1a, 0x2f,
279+
0x63, 0x42, 0x38, 0x42, 0x63, 0x63, 0x63, 0x63,
280+
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
281+
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
282+
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
283+
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
284+
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
285+
0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0xff, 0xc0,
286+
0x00, 0x11, 0x08, 0x00, 0x00, 0x00, 0x00, 0x03,
287+
0x01, 0x22, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11,
288+
0x01, 0xff, 0xc4, 0x00, 0x1f, 0x00, 0x00, 0x01,
289+
0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
290+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
291+
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
292+
0x0a, 0x0b, 0xff, 0xc4, 0x00, 0x1f, 0x01, 0x00,
293+
0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
294+
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
295+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
296+
0x09, 0x0a, 0x0b, 0xff, 0xc4, 0x00, 0xb5, 0x10,
297+
0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
298+
0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
299+
0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
300+
0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
301+
0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
302+
0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
303+
0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
304+
0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
305+
0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
306+
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
307+
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
308+
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
309+
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
310+
0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
311+
0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
312+
0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
313+
0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
314+
0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
315+
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
316+
0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
317+
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
318+
0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
319+
0xf9, 0xfa, 0xff, 0xc4, 0x00, 0xb5, 0x11, 0x00,
320+
0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07,
321+
0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00,
322+
0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31,
323+
0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13,
324+
0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1,
325+
0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15,
326+
0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1,
327+
0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27,
328+
0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39,
329+
0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
330+
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
331+
0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
332+
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
333+
0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
334+
0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
335+
0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
336+
0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5,
337+
0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4,
338+
0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
339+
0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2,
340+
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
341+
0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
342+
0xfa, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00,
343+
0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00};
302344

303345
// Append JPEG header data
304346
if (header_bytes_sent_out < sizeof(header))
305347
{
348+
// Generate header data
349+
float scale;
350+
351+
if (capture_settings.quality_factor < 50)
352+
{
353+
scale = 5000 / capture_settings.quality_factor;
354+
}
355+
else
356+
{
357+
scale = 200 - 2 * capture_settings.quality_factor;
358+
}
359+
360+
for (int i = 25; i <= 88; i++)
361+
{
362+
float t = (scale * header[i] + 50) / 100;
363+
364+
if (t < 1)
365+
{
366+
t = 1;
367+
}
368+
369+
else if (t > 255)
370+
{
371+
t = 255;
372+
}
373+
374+
header[i] = (uint8_t)t;
375+
}
376+
377+
for (int i = 93; i <= 156; i++)
378+
{
379+
float t = (scale * header[i] + 50) / 100;
380+
381+
if (t < 1)
382+
{
383+
t = 1;
384+
}
385+
386+
else if (t > 255)
387+
{
388+
t = 255;
389+
}
390+
391+
header[i] = (uint8_t)t;
392+
}
393+
394+
header[163] = (capture_settings.resolution >> 8) & 0xff;
395+
header[164] = capture_settings.resolution & 0xff;
396+
header[165] = (capture_settings.resolution >> 8) & 0xff;
397+
header[166] = capture_settings.resolution & 0xff;
398+
306399
size_t length =
307400
sizeof(header) - header_bytes_sent_out < bytes_requested
308401
? sizeof(header) - header_bytes_sent_out
@@ -366,7 +459,6 @@ static int lua_camera_read(lua_State *L)
366459
lua_pushlstring(L, (char *)payload, bytes_requested - remaining);
367460
}
368461

369-
free(header);
370462
free(payload);
371463
return 1;
372464
}

0 commit comments

Comments
 (0)