-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpeg_utils.c
71 lines (59 loc) · 1.8 KB
/
jpeg_utils.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
#include "jpeg_utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
void
load_jpeg(const char *filename, ImageViewer *image_viewer)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *file;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
file = fopen(filename, "rb");
if (!file) {
fprintf(stderr, "Could not open file: %s\n", filename);
exit(1);
}
jpeg_stdio_src(&cinfo, file);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
image_viewer->width = cinfo.output_width;
image_viewer->height = cinfo.output_height;
image_viewer->color_components = cinfo.output_components;
image_viewer->image_buffer =
(unsigned char *)malloc(image_viewer->width *
image_viewer->height * image_viewer->color_components);
while (cinfo.output_scanline < cinfo.output_height) {
unsigned char *buffer_array[1];
buffer_array[0] = image_viewer->image_buffer +
(cinfo.output_scanline) * image_viewer->width *
image_viewer->color_components;
jpeg_read_scanlines(&cinfo, buffer_array, 1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(file);
}
void
convert_jpeg_to_ximage(ImageViewer *image_viewer)
{
int depth = DefaultDepth(image_viewer->display, image_viewer->screen);
int bitmap_pad = (depth == 24) ? 32 : depth;
int bytes_per_line =
((image_viewer->width * image_viewer->color_components *
bitmap_pad +
7) /
8);
image_viewer->ximage = XCreateImage(image_viewer->display,
DefaultVisual(image_viewer->display, image_viewer->screen),
depth, ZPixmap, 0, (char *)image_viewer->image_buffer,
image_viewer->width, image_viewer->height, bitmap_pad,
bytes_per_line);
}
void
free_jpeg_buffer(ImageViewer *image_viewer)
{
free(image_viewer->image_buffer);
image_viewer->image_buffer = NULL;
}