summaryrefslogtreecommitdiff
path: root/common/im_jpg.cpp
blob: 9940839297a52c86ad4ff2d74350d2a971079902 (plain)
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
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include "image.h"
extern "C" {
#include <jpeglib.h>
}

typedef struct bt_jpeg_error_mgr
{
  struct jpeg_error_mgr pub;  // "public" fields
  jmp_buf setjmp_buffer;      // for return to caller 
} bt_jpeg_error_mgr;

static void bt_jpeg_error_exit (j_common_ptr cinfo)
{
  bt_jpeg_error_mgr* myerr = (bt_jpeg_error_mgr*) cinfo->err;
  char buffer[JMSG_LENGTH_MAX];
  (*cinfo->err->format_message) (cinfo, buffer);
//	MessageBox(NULL, buffer, "JPEG Fatal Error", MB_ICONSTOP);
  longjmp(myerr->setjmp_buffer, 1);
}

// stash a scanline
static void j_putRGBScanline(unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
{
  int offset = row * widthPix * 3;
  int count;
  for (count = 0; count < widthPix; count++) 
  {
    unsigned char iRed, iBlu, iGrn;
    unsigned char *oRed, *oBlu, *oGrn;

    iRed = *(jpegline + count * 3 + 0);
    iGrn = *(jpegline + count * 3 + 1);
    iBlu = *(jpegline + count * 3 + 2);

    oRed = outBuf + offset + count * 3 + 0;
    oGrn = outBuf + offset + count * 3 + 1;
    oBlu = outBuf + offset + count * 3 + 2;

    *oRed = iRed;
    *oGrn = iGrn;
    *oBlu = iBlu;
  }
}

// stash a gray scanline
static void j_putGrayScanlineToRGB(unsigned char* jpegline, int widthPix, unsigned char* outBuf, int row)
{
  int offset = row * widthPix * 3;
  int count;
  for (count = 0; count < widthPix; count++) 
  {
    unsigned char iGray;
    unsigned char *oRed, *oBlu, *oGrn;

    // get our grayscale value
    iGray = *(jpegline + count);

    oRed = outBuf + offset + count * 3;
    oGrn = outBuf + offset + count * 3 + 1;
    oBlu = outBuf + offset + count * 3 + 2;

    *oRed = iGray;
    *oGrn = iGray;
    *oBlu = iGray;
  }
}

// =============================================================================

LC_IMAGE* OpenJPG (char* filename)
{
  struct jpeg_decompress_struct cinfo;
  struct bt_jpeg_error_mgr jerr;
  FILE* infile = NULL;
  JSAMPARRAY buffer;	// Output row buffer
  int row_stride;	// physical row width in output buffer

  if ((infile = fopen(filename, "rb")) == NULL) 
    return NULL;

  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = bt_jpeg_error_exit;

  if (setjmp(jerr.setjmp_buffer)) 
  {
    jpeg_destroy_decompress(&cinfo);

    if (infile != NULL)
      fclose(infile);
    return NULL;
  }

  jpeg_create_decompress(&cinfo);
  jpeg_stdio_src(&cinfo, infile);
  jpeg_read_header(&cinfo, TRUE);
  jpeg_start_decompress(&cinfo);

  // get our buffer set to hold data
  LC_IMAGE* image = (LC_IMAGE*)malloc(cinfo.output_width*cinfo.output_height*3 + sizeof(LC_IMAGE));

  if (image == NULL)
  {
//    MessageBox(NULL, "Error", "Cannot allocate memory", MB_ICONSTOP);
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);
    return NULL;
  }

  image->width = cinfo.output_width;
  image->height = cinfo.output_height;
  image->bits = (char*)image + sizeof(LC_IMAGE);

  row_stride = cinfo.output_width * cinfo.output_components;
  buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

  while (cinfo.output_scanline < cinfo.output_height) 
  {
    jpeg_read_scanlines(&cinfo, buffer, 1);

    if (cinfo.out_color_components == 3)
      j_putRGBScanline(buffer[0], image->width, (unsigned char*)image->bits, cinfo.output_scanline-1);
    else if (cinfo.out_color_components == 1)
      j_putGrayScanlineToRGB(buffer[0], image->width, (unsigned char*)image->bits, cinfo.output_scanline-1);
  }

  jpeg_finish_decompress(&cinfo);
  jpeg_destroy_decompress(&cinfo);
  fclose(infile);

  return image;
}

// =============================================================================

bool SaveJPG (char* filename, LC_IMAGE* image, int quality, bool progressive)
{
  struct jpeg_compress_struct cinfo;
  struct bt_jpeg_error_mgr jerr;
  int row_stride;	// physical row widthPix in image buffer
  FILE* outfile;

  // allocate and initialize JPEG compression object
  cinfo.err = jpeg_std_error(&jerr.pub);
  jerr.pub.error_exit = bt_jpeg_error_exit;

  if (setjmp(jerr.setjmp_buffer)) 
  {
    //    jpeg_destroy_compress(&cinfo);
    //    if (outfile != NULL)
    //      fclose(outfile);
    return false;
  }

  jpeg_create_compress(&cinfo);
  if ((outfile = fopen(filename, "wb")) == NULL) 
    return false;

  jpeg_stdio_dest(&cinfo, outfile);

  cinfo.image_width = image->width;
  cinfo.image_height = image->height;
  cinfo.input_components = 3;		
  cinfo.in_color_space = JCS_RGB;

  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, quality, TRUE);

  if (progressive) 
    jpeg_simple_progression(&cinfo);

  jpeg_start_compress(&cinfo, TRUE);
  row_stride = image->width * 3;

  while (cinfo.next_scanline < cinfo.image_height) 
  {
    unsigned char* outRow = (unsigned char*)image->bits + (cinfo.next_scanline * image->width * 3);
    jpeg_write_scanlines(&cinfo, &outRow, 1);
  }

  jpeg_finish_compress(&cinfo);
  fclose(outfile);
  jpeg_destroy_compress(&cinfo);

  return true;
}