File: rwjpeg.c

package info (click to toggle)
metapixel 0.11-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 356 kB
  • ctags: 575
  • sloc: ansic: 5,096; xml: 219; perl: 150; makefile: 98
file content (109 lines) | stat: -rw-r--r-- 2,528 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* -*- c -*- */

/*
 * rwjpeg.c
 *
 * metapixel
 *
 * Copyright (C) 2000 Mark Probst
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#include <jpeglib.h>

typedef struct
{
    FILE *file;
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
} jpeg_data_t;

void*
open_jpeg_file (char *filename, int *width, int *height)
{
    jpeg_data_t *data = (jpeg_data_t*)malloc(sizeof(jpeg_data_t));

    assert(data != 0);

    data->file = fopen(filename, "r");
    assert(data->file != 0);

    data->cinfo.err = jpeg_std_error(&data->jerr);
    jpeg_create_decompress(&data->cinfo);
    jpeg_stdio_src(&data->cinfo, data->file);
    jpeg_read_header(&data->cinfo, TRUE);

    if (data->cinfo.num_components == 1)
	data->cinfo.out_color_space = JCS_GRAYSCALE;
    else if (data->cinfo.num_components == 3)
	data->cinfo.out_color_space = JCS_RGB;
    else
	assert(0);

    *width = data->cinfo.image_width;
    *height = data->cinfo.image_height;

    jpeg_start_decompress(&data->cinfo);

    return data;
}

void
jpeg_read_lines (void *_data, unsigned char *lines, int num_lines)
{
    jpeg_data_t *data = (jpeg_data_t*)_data;
    int row_stride, i;

    row_stride = data->cinfo.image_width * 3;

    for (i = 0; i < num_lines; ++i)
    {
	unsigned char *scanline = lines + i * row_stride;

	jpeg_read_scanlines(&data->cinfo, &scanline, 1);

	if (data->cinfo.num_components == 1)
	{
	    int j;

	    for (j = data->cinfo.image_width - 1; j >= 0; --j)
	    {
		unsigned char value = scanline[j];
		int k;

		for (k = 0; k < 3; ++k)
		    scanline[j * 3 + k] = value;
	    }
	}
    }
}

void
jpeg_free_data (void *_data)
{
    jpeg_data_t *data = (jpeg_data_t*)_data;

    jpeg_finish_decompress(&data->cinfo);
    jpeg_destroy_decompress(&data->cinfo);

    fclose(data->file);

    free(data);
}