File: judge.c

package info (click to toggle)
jpegjudge 0.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 308 kB
  • ctags: 69
  • sloc: ansic: 536; sh: 500; makefile: 40
file content (202 lines) | stat: -rw-r--r-- 3,979 bytes parent folder | download | duplicates (5)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * judge.c
 *
 * judges the quality of jpeg files, output format similar to file(1)
 *
 * This uses code from the IJG JPEG library. The original copyright was:
 *
 * Copyright (C) 1994-1996, Thomas G. Lane.
 * This file is part of the Independent JPEG Group's software.
 * For conditions of distribution and use, see the accompanying README-jpeg6a
 * file.
 *
 * The estimate function and all the changes are copyright Marc Lehmann.
 * You can do anything with it *except* claim that you have written it.
 *
 */

#include "config.h"
#undef PACKAGE
#define PACKAGE "jpegjudge"

#include <assert.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <jpeglib.h>

#include "estim.c"

static int
judge_quality (char *filename)
{
  struct jpeg_decompress_struct cinfo;
  struct jpeg_error_mgr jerr;
  int scan_size;
  u8 *buffer, *scanline;
  FILE *input_file;
  JDIMENSION num_scanlines;
  estimate_stat stat;
  
  /* Initialize the JPEG decompression object with default error handling. */
  cinfo.err = jpeg_std_error (&jerr);
  cinfo.err->trace_level = 0;
  jpeg_create_decompress (&cinfo);

  if (filename)
    {
      /* Open the input file. */
      if ((input_file = fopen (filename, "rb")) == NULL)
	{
	  perror (filename);
	  return -1;
	}
    }
  else
    input_file = stdin;

  /* Specify data source for decompression */
  jpeg_stdio_src (&cinfo, input_file);

  /* Read file header, set default decompression parameters */
  (void) jpeg_read_header (&cinfo, TRUE);

  cinfo.dct_method = JDCT_ISLOW;
  cinfo.dither_mode = JDITHER_NONE;
  cinfo.do_fancy_upsampling = FALSE;
  cinfo.do_block_smoothing = FALSE;

  /* Start decompressor */
  (void) jpeg_start_decompress (&cinfo);

  scan_size = cinfo.output_width * cinfo.output_components;
  scanline = buffer = (u8 *) malloc (scan_size * DCTSIZE);
  assert (buffer);		// bad, bad...

  estimate_init (&stat);

  /* Process data */
  while (cinfo.output_height > cinfo.output_scanline)
    {
      num_scanlines = jpeg_read_scanlines (&cinfo, &scanline, 1);
      assert (num_scanlines == 1);
      if ((unsigned) cinfo.output_scanline % DCTSIZE == 0)
	{
	  scanline = buffer;
	  process_blocks (&stat, buffer, cinfo.output_width, cinfo.output_components);
	}
      else
	scanline += scan_size;

    }

  free (buffer);

  (void) jpeg_finish_decompress (&cinfo);
  jpeg_destroy_decompress (&cinfo);

  /* Close files, if we opened them */
  if (filename)
    fclose (input_file);

  /* All done. */
  if (jerr.num_warnings)
    printf ("jpeg read error\n");

  return estimate_quality (&stat);
}

static void
usage (char *prog, int ec)
{
  fprintf (stderr, "Usage: %s [ -Vb ] file ...\n", prog);
  exit (ec);
}

void
version (void)
{
  printf ("%s %s\n", VERSION, PACKAGE);
  exit (0);
}

const char *
is_in_list (int size, char **const list, const char *string)
{
  int i = 0;

  while (i < size)
    {
	if ( !strcmp( list[i], string) )
	  return list[i];

	i++;
    }

  return (const char *)NULL;
}

int
main (int argc, char **argv)
{
  int display_filenames = 1;
  int retcode = 0;

  /* Handle long options */

  if ( is_in_list (argc, argv, "--help"))
    usage (argv[0], 0);

  if ( is_in_list (argc, argv, "--version"))
    version();

  for (;;)
    {
      switch (getopt (argc, argv, "hVb"))
	{
	case 'h':
	  usage (argv[0], 0);
	  
	case 'V':
	  version();
	  break;

	case 'b':
	  display_filenames = 0;
	  break;
	  
	case '?':
	  usage (argv[0], 2);
	
	default:
	  goto noodle;
	}
    }
  
  noodle:
  
  if (optind == argc)
    usage (argv[0], 2);
  
  while (optind < argc)
    {
      int quality;
      char *filename = argv[optind];
      
      quality = judge_quality (strcmp (filename, "-") ? filename : 0);
      
      if (quality<0)
        retcode = 1;
      else if (display_filenames)
        printf ("%s: %3d\n", filename, quality);
      else
        printf ("%3d\n", quality);
      
      optind++;
    }
  
  return retcode;
}