-
Notifications
You must be signed in to change notification settings - Fork 5
/
ijgenc.c
92 lines (69 loc) · 2.48 KB
/
ijgenc.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
/*
* Copyright (c) 2014, Derek Buitenhuis
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <jerror.h>
#include <jpeglib.h>
#include "ijgdec.h"
#include "ijgenc.h"
static void init(j_compress_ptr cinfo)
{
}
static boolean empty(j_compress_ptr cinfo)
{
return TRUE;
}
static void term(j_compress_ptr cinfo)
{
}
int jpeg_encode(uint8_t *in, uint8_t *out, jinfo res, int quality)
{
JSAMPROW curline[1];
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
struct jpeg_destination_mgr d;
int size;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
cinfo.dest = &d;
cinfo.image_width = res.width;
cinfo.image_height = res.height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_YCbCr;
cinfo.use_moz_defaults = TRUE;
jpeg_set_defaults(&cinfo);
cinfo.use_flat_quant_tbl = FALSE;
cinfo.lambda_log_scale1 = 14.25;
cinfo.lambda_log_scale2 = 12.75;
cinfo.use_lambda_weight_tbl = TRUE;
cinfo.dest->init_destination = &init;
cinfo.dest->empty_output_buffer = ∅
cinfo.dest->term_destination = &term;
cinfo.dest->free_in_buffer = res.width * res.height * 3;
cinfo.dest->next_output_byte = (JOCTET *) out;
cinfo.comp_info[0].v_samp_factor = res.vsamp;
cinfo.comp_info[0].h_samp_factor = res.hsamp;
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
while (cinfo.next_scanline < cinfo.image_height) {
curline[0] = &in[cinfo.next_scanline * res.width * 3];
jpeg_write_scanlines(&cinfo, curline, 1);
}
jpeg_finish_compress(&cinfo);
size = (res.width * res.height * 3) - cinfo.dest->free_in_buffer;
jpeg_destroy_compress(&cinfo);
return size;
}