summaryrefslogtreecommitdiffstats
path: root/all_pairs/source/cjpeg_wrbmp/cjpeg_wrbmp.c
blob: 7bef7ab2066165cd65128978c6fbdbdf6edb5649 (plain) (blame)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*

  This program is part of the TACLeBench benchmark suite.
  Version V 1.x

  Name: cjpeg_jpeg6b_wrbmp.c

  Author: Thomas G. Lane.

  Function: This file contains routines to write output images in Microsoft "BMP"
  format (MS Windows 3.x and OS/2 1.x flavors).
  Either 8-bit colormapped or 24-bit full-color format can be written.
  No compression is supported.

  These routines may need modification for non-Unix environments or
  specialized applications.  As they stand, they assume output to
  an ordinary stdio stream.

  Source: Independent JPEG Group's software

  Changes: a brief summary of major functional changes (not formatting)

  License:  See the accompanying README file

*/

#include "../extra.h"
#include "cdjpeg.h"

#ifdef CJPEG_WRBMP_BMP_SUPPORTED

/*
  Declaration of global variables
*/
typedef struct {
  struct cjpeg_wrbmp_djpeg_dest_struct pub; /* public fields */
  cjpeg_wrbmp_boolean is_os2;   /* saves the OS2 format request flag */
  cjpeg_wrbmp_jvirt_sarray_ptr
  whole_image; /* needed to reverse row order */
  CJPEG_WRBMP_JDIMENSION data_width;  /* JSAMPLEs per row */
  CJPEG_WRBMP_JDIMENSION
  row_width;   /* physical width of one row in the BMP file */
  int pad_bytes;    /* number of padding bytes needed per row */
  CJPEG_WRBMP_JDIMENSION
  cur_output_row;  /* next row# to write to virtual array */
} cjpeg_wrbmp_bmp_dest_struct;

typedef cjpeg_wrbmp_bmp_dest_struct *cjpeg_wrbmp_bmp_dest_ptr;
extern unsigned char cjpeg_wrbmp_colormap[3][256];
unsigned char cjpeg_wrbmp_output_array[6144];
unsigned char *cjpeg_wrbmp_jpeg_stream /*= cjpeg_jpeg6b_wrbmp_output_array*/;
int cjpeg_wrbmp_checksum;

struct cjpeg_wrbmp_jpeg_decompress_struct
  cjpeg_wrbmp_jpeg_dec_1;
struct cjpeg_wrbmp_jpeg_decompress_struct
  cjpeg_wrbmp_jpeg_dec_2;
struct cjpeg_wrbmp_djpeg_dest_struct
  cjpeg_wrbmp_djpeg_dest;
cjpeg_wrbmp_bmp_dest_struct    cjpeg_wrbmp_bmp_dest;

/*
  Forward declaration of functions
*/
void cjpeg_wrbmp_initInput( void );
void cjpeg_wrbmp_finish_output_bmp( cjpeg_wrbmp_j_decompress_ptr cinfo);
void cjpeg_wrbmp_write_colormap( cjpeg_wrbmp_j_decompress_ptr
                                        cinfo,
                                        int map_colors, int map_entry_size,
                                        int cMap );
int cjpeg_wrbmp_putc_modified( int character );
void cjpeg_wrbmp_init();
void cjpeg_wrbmp_main();
int cjpeg_wrbmp_return();
//int main();

/*
   Initialization functions
*/
void cjpeg_wrbmp_init()
{
  cjpeg_wrbmp_initInput();

  cjpeg_wrbmp_jpeg_dec_1.progress                = 0;
  cjpeg_wrbmp_jpeg_dec_1.output_height           = 30;
  cjpeg_wrbmp_jpeg_dec_1.actual_number_of_colors = 256;
  cjpeg_wrbmp_jpeg_dec_1.out_color_components    = 2;

  cjpeg_wrbmp_jpeg_dec_2.progress                = 0;
  cjpeg_wrbmp_jpeg_dec_2.output_height           = 30;
  cjpeg_wrbmp_jpeg_dec_2.actual_number_of_colors = 256;
  cjpeg_wrbmp_jpeg_dec_2.out_color_components    = 3;

  cjpeg_wrbmp_jpeg_stream = cjpeg_wrbmp_output_array;

  cjpeg_wrbmp_checksum = 0;
}

/*
   Calculation functions
*/
int cjpeg_wrbmp_putc_modified( int character )
{
  *( cjpeg_wrbmp_jpeg_stream ) = character;

  ++cjpeg_wrbmp_jpeg_stream;

  cjpeg_wrbmp_checksum += character;

  return character;
}

void cjpeg_wrbmp_finish_output_bmp( cjpeg_wrbmp_j_decompress_ptr cinfo )
{
  CJPEG_WRBMP_JDIMENSION row;
  cjpeg_wrbmp_cd_progress_ptr progress =
    ( cjpeg_wrbmp_cd_progress_ptr ) cinfo->progress;

  // Write the file body from our virtual array
  _Pragma( "loopbound min 30 max 30" )
  for ( row = cinfo->output_height; row > 0; --row ) {
    if ( progress != 0 ) {
      progress->pub.pass_counter = ( long )( cinfo->output_height - row );
      progress->pub.pass_limit = ( long ) cinfo->output_height;
    }
  }

  if ( progress != 0 )
     progress->completed_extra_passes++;
  }

void cjpeg_wrbmp_write_colormap( cjpeg_wrbmp_j_decompress_ptr
                                        cinfo,
                                        int map_colors, int map_entry_size, int cMap )
{

  int num_colors = cinfo->actual_number_of_colors;
  int i;

  if ( cMap != 0 ) {

    if ( cinfo->out_color_components == 3 ) {
      // Normal case with RGB colormap
      _Pragma( "loopbound min 256 max 256" )
      for ( i = 0; i < num_colors; i++ ) {
        cjpeg_wrbmp_putc_modified( CJPEG_WRBMP_GETJSAMPLE(
                                            cjpeg_wrbmp_colormap[2][i] ) );
        cjpeg_wrbmp_putc_modified( CJPEG_WRBMP_GETJSAMPLE(
                                            cjpeg_wrbmp_colormap[1][i] ) );
        cjpeg_wrbmp_putc_modified( CJPEG_WRBMP_GETJSAMPLE(
                                            cjpeg_wrbmp_colormap[0][i] ) );

        if ( map_entry_size == 4 )
          cjpeg_wrbmp_putc_modified( 0 );
      }
    } else {
      // Grayscale colormap (only happens with grayscale quantization)
      _Pragma( "loopbound min 256 max 256" )
      for ( i = 0; i < num_colors; i++ ) {

        cjpeg_wrbmp_putc_modified( CJPEG_WRBMP_GETJSAMPLE(
                                            cjpeg_wrbmp_colormap[2][i] ) );
        cjpeg_wrbmp_putc_modified( CJPEG_WRBMP_GETJSAMPLE(
                                            cjpeg_wrbmp_colormap[1][i] ) );
        cjpeg_wrbmp_putc_modified( CJPEG_WRBMP_GETJSAMPLE(
                                            cjpeg_wrbmp_colormap[0][i] ) );

        if ( map_entry_size == 4 )
          cjpeg_wrbmp_putc_modified( 0 );
      }
    }
  } else {
    // If no colormap, must be grayscale data.  Generate a linear "map".
    _Pragma( "loopbound min 256 max 256" )
    for ( i = 0; i < 256; i++ ) {
      cjpeg_wrbmp_putc_modified( i );
      cjpeg_wrbmp_putc_modified( i );
      cjpeg_wrbmp_putc_modified( i );

      if ( map_entry_size == 4 )
        cjpeg_wrbmp_putc_modified( 0 );
    }
  }

  // Pad colormap with zeros to ensure specified number of colormap entries.
  _Pragma( "loopbound min 512 max 512" )
  for ( ; i < map_colors; i++ ) {
    cjpeg_wrbmp_putc_modified( 0 );
    cjpeg_wrbmp_putc_modified( 0 );
    cjpeg_wrbmp_putc_modified( 0 );

    if ( map_entry_size == 4 )
      cjpeg_wrbmp_putc_modified( 0 );
  }
}

void _Pragma( "entrypoint" ) cjpeg_wrbmp_main()
{
  cjpeg_wrbmp_finish_output_bmp( &cjpeg_wrbmp_jpeg_dec_1);
  cjpeg_wrbmp_write_colormap(    &cjpeg_wrbmp_jpeg_dec_1, 768, 4, 1 );

  cjpeg_wrbmp_finish_output_bmp( &cjpeg_wrbmp_jpeg_dec_2 );
  cjpeg_wrbmp_write_colormap(    &cjpeg_wrbmp_jpeg_dec_2, 768, 4, 1 );
}

int cjpeg_wrbmp_return()
{
	return (cjpeg_wrbmp_checksum  + (-209330) ) != 0;
}

int main(int argc, char **argv)
{
	SET_UP
	for (jobsComplete=-1; jobsComplete<maxJobs; jobsComplete++){
		START_LOOP
		cjpeg_wrbmp_init();
  		cjpeg_wrbmp_main();
		STOP_LOOP
	}
	WRITE_TO_FILE
    return ( cjpeg_wrbmp_return() );
}

#endif /* BMP_SUPPORTED */