Voxia OS
v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
jpeg.c
Go to the documentation of this file.
1
#include <
libk/jpeg.h
>
2
3
// #include <stdint.h>
4
// #include <stdlib.h>
5
// #include <math.h>
6
// #include <string.h>
7
// #include <stdio.h>
8
9
// #define MAX_COMPONENT 3
10
// #define MAX_MCU 4096
11
// #define MAX_DQT 4
12
// #define MAX_HUFF 4
13
14
// typedef struct {
15
// uint8_t id;
16
// uint8_t h_samp;
17
// uint8_t v_samp;
18
// uint8_t q_table_id;
19
// } jpg_component_t;
20
21
// typedef struct {
22
// uint16_t width;
23
// uint16_t height;
24
// uint8_t component_count;
25
// jpg_component_t components[MAX_COMPONENT];
26
// } sof0_frame_t;
27
28
// typedef struct {
29
// uint8_t id;
30
// uint8_t precision; // 0=8bit, 1=16bit
31
// uint16_t table[64];
32
// } dqt_t;
33
34
// typedef struct {
35
// uint8_t bits[16]; // jumlah symbol per panjang kode
36
// uint8_t values[256];
37
// uint16_t code[256];
38
// uint8_t size[256];
39
// uint16_t count;
40
// } huffman_table_t;
41
42
// typedef struct {
43
// uint8_t num_components;
44
// struct {
45
// uint8_t id;
46
// uint8_t dc_table;
47
// uint8_t ac_table;
48
// } comp[MAX_COMPONENT];
49
// uint8_t start_spectral;
50
// uint8_t end_spectral;
51
// uint8_t approx_high;
52
// uint8_t approx_low;
53
// } sos_frame_t;
54
55
// typedef struct {
56
// int16_t coef[64];
57
// } block_t;
58
59
// // Zigzag index
60
// static const uint8_t zigzag[64] = {
61
// 0,1,5,6,14,15,27,28,
62
// 2,4,7,13,16,26,29,42,
63
// 3,8,12,17,25,30,41,43,
64
// 9,11,18,24,31,40,44,53,
65
// 10,19,23,32,39,45,52,54,
66
// 20,22,33,38,46,51,55,60,
67
// 21,34,37,47,50,56,59,61,
68
// 35,36,48,49,57,58,62,63
69
// };
70
71
// // --- IDCT 8x8 ---
72
// void idct_block(int16_t input[64], uint8_t output[64]) {
73
// for (int y=0;y<8;y++) {
74
// for (int x=0;x<8;x++) {
75
// double sum = 0.0;
76
// for (int u=0;u<8;u++) {
77
// for (int v=0;v<8;v++) {
78
// double cu = (u==0)?1.0/sqrt(2):1.0;
79
// double cv = (v==0)?1.0/sqrt(2):1.0;
80
// double dct = (double)input[u*8+v];
81
// sum += cu*cv*dct*cos((2*x+1)*u*M_PI/16.0)*cos((2*y+1)*v*M_PI/16.0);
82
// }
83
// }
84
// sum *= 0.25;
85
// sum += 128.0;
86
// if(sum<0) sum=0; if(sum>255) sum=255;
87
// output[y*8+x] = (uint8_t)sum;
88
// }
89
// }
90
// }
91
92
// // --- YCbCr -> RGB ---
93
// void ycbcr_to_rgb(uint8_t Y, uint8_t Cb, uint8_t Cr, uint8_t *R, uint8_t *G, uint8_t *B){
94
// int r = Y + 1.402*(Cr-128);
95
// int g = Y - 0.344136*(Cb-128) - 0.714136*(Cr-128);
96
// int b = Y + 1.772*(Cb-128);
97
// if(r<0) r=0; if(r>255) r=255;
98
// if(g<0) g=0; if(g>255) g=255;
99
// if(b<0) b=0; if(b>255) b=255;
100
// *R=r; *G=g; *B=b;
101
// }
102
103
// // --- Contoh put_pixel (isi framebuffer) ---
104
// void put_pixel(int x,int y,uint32_t color,uint32_t *fb,int fb_width){
105
// fb[y*fb_width + x] = color;
106
// }
107
108
// // --- Parsing JPEG dari file (minimal baseline) ---
109
// int parse_jpeg(const uint8_t *data, size_t size,
110
// sof0_frame_t *frame,
111
// dqt_t *dqts, int *dqts_count,
112
// huffman_table_t *ht_dc, int *ht_dc_count,
113
// huffman_table_t *ht_ac, int *ht_ac_count,
114
// sos_frame_t *sos){
115
// size_t idx=0;
116
// *dqts_count=0; *ht_dc_count=0; *ht_ac_count=0;
117
118
// while(idx<size){
119
// if(data[idx]==0xFF){
120
// uint8_t marker = data[idx+1];
121
// if(marker==0xD8){ idx+=2; continue; } // SOI
122
// else if(marker==0xE0){ // APP0
123
// uint16_t len = (data[idx+2]<<8)|data[idx+3];
124
// idx += len+2;
125
// }
126
// else if(marker==0xDB){ // DQT
127
// uint16_t len = (data[idx+2]<<8)|data[idx+3];
128
// size_t s = idx+4;
129
// while(s<idx+len+2){
130
// uint8_t pq_tq = data[s++];
131
// uint8_t pq = pq_tq>>4;
132
// uint8_t tq = pq_tq & 0xF;
133
// dqt_t *q = &dqts[(*dqts_count)++];
134
// q->id=tq; q->precision=pq;
135
// for(int i=0;i<64;i++){
136
// if(pq==0) q->table[i] = data[s++];
137
// else { q->table[i] = (data[s]<<8)|data[s+1]; s+=2; }
138
// }
139
// }
140
// idx+=len+2;
141
// }
142
// else if(marker==0xC0){ // SOF0 baseline
143
// uint16_t len = (data[idx+2]<<8)|data[idx+3];
144
// frame->component_count = data[idx+5];
145
// frame->height = (data[idx+3]<<8)|data[idx+4];
146
// frame->width = (data[idx+1]<<8)|data[idx+2]; // perbaiki urutan sesuai JPEG
147
// for(int i=0;i<frame->component_count;i++){
148
// frame->components[i].id = data[idx+6+i*3];
149
// uint8_t samp = data[idx+7+i*3];
150
// frame->components[i].h_samp = samp>>4;
151
// frame->components[i].v_samp = samp&0xF;
152
// frame->components[i].q_table_id = data[idx+8+i*3];
153
// }
154
// idx+=len+2;
155
// }
156
// else if(marker==0xC4){ // DHT
157
// uint16_t len = (data[idx+2]<<8)|data[idx+3];
158
// // Parsing Huffman table bisa ditambahkan di sini
159
// idx+=len+2;
160
// }
161
// else if(marker==0xDA){ // SOS
162
// uint16_t len = (data[idx+2]<<8)|data[idx+3];
163
// // parsing SOS
164
// idx+=len+2;
165
// }
166
// else{ idx+=2; }
167
// } else idx++;
168
// }
169
// return 0;
170
// }
171
172
// // --- Fungsi contoh render MCU (IDCT + YCbCr→RGB) ---
173
// void render_example(uint32_t *fb,int fb_width,int fb_height){
174
// block_t Y, Cb, Cr;
175
// for(int i=0;i<64;i++){ Y.coef[i]=rand()%256 -128; Cb.coef[i]=128; Cr.coef[i]=128; }
176
// render_mcu(&Y,&Cb,&Cr,0,0,2,2,fb,fb_width);
177
// }
178
179
// int main(){
180
// // --- framebuffer contoh ---
181
// int width=320, height=240;
182
// uint32_t *fb = malloc(width*height*sizeof(uint32_t));
183
// memset(fb,0,sizeof(uint32_t)*width*height);
184
185
// // --- parsing JPEG ---
186
// FILE *f = fopen("test.jpg","rb");
187
// fseek(f,0,SEEK_END); size_t sz = ftell(f); fseek(f,0,SEEK_SET);
188
// uint8_t *buf = malloc(sz); fread(buf,1,sz,f); fclose(f);
189
190
// sof0_frame_t frame;
191
// dqt_t dqts[MAX_DQT];
192
// huffman_table_t ht_dc[MAX_HUFF], ht_ac[MAX_HUFF];
193
// int dqts_count, ht_dc_count, ht_ac_count;
194
// sos_frame_t sos;
195
196
// parse_jpeg(buf,sz,&frame,dqts,&dqts_count,ht_dc,&ht_dc_count,ht_ac,&ht_ac_count,&sos);
197
198
// // --- contoh render ---
199
// render_example(fb,width,height);
200
201
// // --- tampilkan di konsol sebagai PPM ---
202
// FILE *ppm = fopen("out.ppm","wb");
203
// fprintf(ppm,"P6\n%d %d\n255\n",width,height);
204
// for(int i=0;i<width*height;i++){
205
// fputc((fb[i]>>16)&0xFF,ppm);
206
// fputc((fb[i]>>8)&0xFF,ppm);
207
// fputc(fb[i]&0xFF,ppm);
208
// }
209
// fclose(ppm);
210
// }
jpeg.h
kernel
libk
jpeg.c
Generated on Sat May 30 2026 11:09:57 for Voxia OS by
1.13.2