Skip to content

Commit 8a1ed80

Browse files
authored
Merge pull request #290 from brilliantlabsAR/robert/jpeg-hdr
lightweight C jpeg header - copy from origin/raj/jpeg-optimizations
2 parents 3357ced + 3319c03 commit 8a1ed80

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Authored by: Robert Metchev / Chips & Scripts (rmetchev@ieee.org)
3+
#
4+
# CERN Open Hardware Licence Version 2 - Permissive
5+
#
6+
# Copyright (C) 2024 Robert Metchev
7+
#
8+
9+
HEIGHT ?= 200
10+
WIDTH ?= 200
11+
QF ?= 50
12+
13+
all: header.bin
14+
header.bin: jhdr.exe
15+
$(<) $(HEIGHT) $(WIDTH) $(QF)
16+
jhdr.exe: jhdr.c main.c
17+
gcc -o $(@) $(^)
18+
19+
clean:
20+
rm -f jhdr.exe jhdr.o header.bin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
��
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Authored by: Robert Metchev / Chips & Scripts (rmetchev@ieee.org)
3+
CERN Open Hardware Licence Version 2 - Permissive
4+
Copyright (C) 2024 Robert Metchev
5+
6+
Pre-compiled JPEG header template (incl. quantization and Huffman tables).
7+
QF = 50, height = 0, width = 0
8+
Positions: QT0 25..88, QT1 93..156, height 163..164, width 165..166
9+
Total length = 623
10+
*/
11+
#include "jhdr.h"
12+
13+
int scale_mult (int q, float scale) {
14+
float t = (scale*q + 50)/100; // Round
15+
if (t < 1) // Prevent divide by 0 error
16+
t = 1;
17+
else if (t > 255) // Prevent overflow
18+
t = 255;
19+
return (int)t;
20+
}
21+
22+
void hdr(int height, int width, int qf, char header_file_name[]) {
23+
char 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};
24+
float scale;
25+
26+
if (qf < 50)
27+
scale = 5000/qf;
28+
else
29+
scale = 200 - 2*qf;
30+
31+
for (int i=25; i<=88; i++)
32+
header[i] = scale_mult(header[i], scale);
33+
for (int i=93; i<=156; i++)
34+
header[i] = scale_mult(header[i], scale);
35+
36+
header[163] = (height >> 8) & 0xff;
37+
header[164] = height & 0xff;
38+
header[165] = (width >> 8) & 0xff;
39+
header[166] = width & 0xff;
40+
41+
FILE *f = fopen(header_file_name, "wb");
42+
fwrite(header, 1, 623, f);
43+
fclose(f);
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Authored by: Robert Metchev / Chips & Scripts (rmetchev@ieee.org)
3+
CERN Open Hardware Licence Version 2 - Permissive
4+
Copyright (C) 2024 Robert Metchev
5+
6+
Pre-compiled JPEG header template (incl. quantization and Huffman tables).
7+
QF = 50, height = 0, width = 0
8+
Positions: QT0 25..88, QT1 93..156, height 163..164, width 165..166
9+
Total length = 623
10+
*/
11+
#include <stdlib.h>
12+
#include <stdio.h>
13+
void hdr(int height, int width, int qf, char header_file_name[]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Authored by: Robert Metchev / Chips & Scripts (rmetchev@ieee.org)
3+
CERN Open Hardware Licence Version 2 - Permissive
4+
Copyright (C) 2024 Robert Metchev
5+
*/
6+
#include "jhdr.h"
7+
int main(int argc, char *argv[]) {
8+
if(argc <= 2)
9+
exit(1);
10+
// call hdr, write header to "header.bin"
11+
hdr(atoi(argv[1]), atoi(argv[2]), argc > 3 ? atoi(argv[3]) : 50 , argc > 4 ? argv[4] : "header.bin");
12+
return 0;
13+
}
14+

0 commit comments

Comments
 (0)