summaryrefslogtreecommitdiffstats
path: root/jni/jpegutil.cpp
blob: 02705beff6746df9c63b55a12bcaeffaf57153f4 (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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "jpegutil.h"
#include <memory.h>
#include <array>
#include <vector>
#include <cstring>
#include <cstdio>

#include <setjmp.h>

extern "C" {
#include "jpeglib.h"
}

using namespace std;
using namespace jpegutil;

template <typename T>
void safeDelete(T& t) {
  if (t != nullptr) {
    delete t;
    t = nullptr;
  }
}

template <typename T>
void safeDeleteArray(T& t) {
  if (t != nullptr) {
    delete[] t;
    t = nullptr;
  }
}

jpegutil::Transform::Transform(int orig_x, int orig_y, int one_x, int one_y)
    : orig_x_(orig_x), orig_y_(orig_y), one_x_(one_x), one_y_(one_y) {
  if (orig_x == one_x || orig_y == one_y) {
    // Handle the degenerate case of cropping to a 0x0 rectangle.
    mat00_ = 0;
    mat01_ = 0;
    mat10_ = 0;
    mat11_ = 0;
    return;
  }

  if (one_x > orig_x && one_y > orig_y) {
    // 0-degree rotation
    mat00_ = 1;
    mat01_ = 0;
    mat10_ = 0;
    mat11_ = 1;
    output_width_ = abs(one_x - orig_x);
    output_height_ = abs(one_y - orig_y);
  } else if (one_x < orig_x && one_y > orig_y) {
    // 90-degree CCW rotation
    mat00_ = 0;
    mat01_ = -1;
    mat10_ = 1;
    mat11_ = 0;
    output_width_ = abs(one_y - orig_y);
    output_height_ = abs(one_x - orig_x);
  } else if (one_x > orig_x && one_y < orig_y) {
    // 270-degree CCW rotation
    mat00_ = 0;
    mat01_ = 1;
    mat10_ = -1;
    mat11_ = 0;
    output_width_ = abs(one_y - orig_y);
    output_height_ = abs(one_x - orig_x);
  } else if (one_x < orig_x && one_y < orig_y) {
    // 180-degree CCW rotation
    mat00_ = -1;
    mat01_ = 0;
    mat10_ = 0;
    mat11_ = -1;
    output_width_ = abs(one_x - orig_x);
    output_height_ = abs(one_y - orig_y);
  }
}

jpegutil::Transform jpegutil::Transform::ForCropFollowedByRotation(
    int cropLeft, int cropTop, int cropRight, int cropBottom, int rot90) {
  // The input crop-region excludes cropRight and cropBottom, so transform the
  // crop rect such that it defines the entire valid region of pixels
  // inclusively.
  cropRight -= 1;
  cropBottom -= 1;

  int cropXLow = min(cropLeft, cropRight);
  int cropYLow = min(cropTop, cropBottom);
  int cropXHigh = max(cropLeft, cropRight);
  int cropYHigh = max(cropTop, cropBottom);
  rot90 %= 4;
  if (rot90 == 0) {
    return Transform(cropXLow, cropYLow, cropXHigh + 1, cropYHigh + 1);
  } else if (rot90 == 1) {
    return Transform(cropXHigh, cropYLow, cropXLow - 1, cropYHigh + 1);
  } else if (rot90 == 2) {
    return Transform(cropXHigh, cropYHigh, cropXLow - 1, cropYLow - 1);
  } else if (rot90 == 3) {
    return Transform(cropXLow, cropYHigh, cropXHigh + 1, cropYLow - 1);
  }
  // Impossible case.
  return Transform(cropXLow, cropYLow, cropXHigh + 1, cropYHigh + 1);
}

bool jpegutil::Transform::operator==(const Transform& other) const {
  return other.orig_x_ == orig_x_ &&  //
         other.orig_y_ == orig_y_ &&  //
         other.one_x_ == one_x_ &&    //
         other.one_y_ == one_y_;
}

/**
 * Transforms the input coordinates.  Coordinates outside the cropped region
 * are clamped to valid values.
 */
void jpegutil::Transform::Map(int x, int y, int* x_out, int* y_out) const {
  x = max(x, 0);
  y = max(y, 0);
  x = min(x, output_width() - 1);
  y = min(y, output_height() - 1);
  *x_out = x * mat00_ + y * mat01_ + orig_x_;
  *y_out = x * mat10_ + y * mat11_ + orig_y_;
}

int jpegutil::Compress(int img_width, int img_height,
                       jpegutil::RowIterator<16>& y_row_generator,
                       jpegutil::RowIterator<8>& cb_row_generator,
                       jpegutil::RowIterator<8>& cr_row_generator,
                       unsigned char* out_buf, size_t out_buf_capacity,
                       std::function<void(size_t)> flush, int quality) {
  // libjpeg requires the use of setjmp/longjmp to recover from errors.  Since
  // this doesn't play well with RAII, we must use pointers and manually call
  // delete. See POSIX documentation for longjmp() for details on why the
  // volatile keyword is necessary.
  volatile jpeg_compress_struct cinfov;

  jpeg_compress_struct& cinfo =
      *const_cast<struct jpeg_compress_struct*>(&cinfov);

  JSAMPROW* volatile yArr = nullptr;
  JSAMPROW* volatile cbArr = nullptr;
  JSAMPROW* volatile crArr = nullptr;

  JSAMPARRAY imgArr[3];

  // Error handling

  struct my_error_mgr {
    struct jpeg_error_mgr pub;
    jmp_buf setjmp_buffer;
  } err;

  cinfo.err = jpeg_std_error(&err.pub);

  // Default error_exit will call exit(), so override
  // to return control via setjmp/longjmp.
  err.pub.error_exit = [](j_common_ptr cinfo) {
    my_error_mgr* myerr = reinterpret_cast<my_error_mgr*>(cinfo->err);

    (*cinfo->err->output_message)(cinfo);

    // Return control to the setjmp point (see call to setjmp()).
    longjmp(myerr->setjmp_buffer, 1);
  };

  cinfo.err = (struct jpeg_error_mgr*)&err;

  // Set the setjmp point to return to in case of error.
  if (setjmp(err.setjmp_buffer)) {
    // If libjpeg hits an error, control will jump to this point (see call to
    // longjmp()).
    jpeg_destroy_compress(&cinfo);

    safeDeleteArray(yArr);
    safeDeleteArray(cbArr);
    safeDeleteArray(crArr);

    return -1;
  }

  // Create jpeg compression context
  jpeg_create_compress(&cinfo);

  // Stores data needed by our c-style callbacks into libjpeg
  struct ClientData {
    unsigned char* out_buf;
    size_t out_buf_capacity;
    std::function<void(size_t)> flush;
    int totalOutputBytes;
  } clientData{out_buf, out_buf_capacity, flush, 0};

  cinfo.client_data = &clientData;

  // Initialize destination manager
  jpeg_destination_mgr dest;

  dest.init_destination = [](j_compress_ptr cinfo) {
    ClientData& cdata = *reinterpret_cast<ClientData*>(cinfo->client_data);

    cinfo->dest->next_output_byte = cdata.out_buf;
    cinfo->dest->free_in_buffer = cdata.out_buf_capacity;
  };

  dest.empty_output_buffer = [](j_compress_ptr cinfo) -> boolean {
    ClientData& cdata = *reinterpret_cast<ClientData*>(cinfo->client_data);

    size_t numBytesInBuffer = cdata.out_buf_capacity;
    cdata.flush(numBytesInBuffer);
    cdata.totalOutputBytes += numBytesInBuffer;

    // Reset the buffer
    cinfo->dest->next_output_byte = cdata.out_buf;
    cinfo->dest->free_in_buffer = cdata.out_buf_capacity;

    return true;
  };

  dest.term_destination = [](j_compress_ptr cinfo) {
    // do nothing to terminate the output buffer
  };

  cinfo.dest = &dest;

  // Set jpeg parameters
  cinfo.image_width = img_width;
  cinfo.image_height = img_height;
  cinfo.input_components = 3;

  // Set defaults based on the above values
  jpeg_set_defaults(&cinfo);

  jpeg_set_quality(&cinfo, quality, true);

  cinfo.dct_method = JDCT_IFAST;

  cinfo.raw_data_in = true;

  jpeg_set_colorspace(&cinfo, JCS_YCbCr);

  cinfo.comp_info[0].h_samp_factor = 2;
  cinfo.comp_info[0].v_samp_factor = 2;
  cinfo.comp_info[1].h_samp_factor = 1;
  cinfo.comp_info[1].v_samp_factor = 1;
  cinfo.comp_info[2].h_samp_factor = 1;
  cinfo.comp_info[2].v_samp_factor = 1;

  jpeg_start_compress(&cinfo, true);

  yArr = new JSAMPROW[cinfo.comp_info[0].v_samp_factor * DCTSIZE];
  cbArr = new JSAMPROW[cinfo.comp_info[1].v_samp_factor * DCTSIZE];
  crArr = new JSAMPROW[cinfo.comp_info[2].v_samp_factor * DCTSIZE];

  imgArr[0] = const_cast<JSAMPARRAY>(yArr);
  imgArr[1] = const_cast<JSAMPARRAY>(cbArr);
  imgArr[2] = const_cast<JSAMPARRAY>(crArr);

  for (int y = 0; y < img_height; y += DCTSIZE * 2) {
    std::array<unsigned char*, 16> yData = y_row_generator.LoadAt(y);
    std::array<unsigned char*, 8> cbData = cb_row_generator.LoadAt(y / 2);
    std::array<unsigned char*, 8> crData = cr_row_generator.LoadAt(y / 2);

    for (int row = 0; row < DCTSIZE * 2; row++) {
      yArr[row] = yData[row];
    }
    for (int row = 0; row < DCTSIZE; row++) {
      cbArr[row] = cbData[row];
      crArr[row] = crData[row];
    }

    jpeg_write_raw_data(&cinfo, imgArr, DCTSIZE * 2);
  }

  jpeg_finish_compress(&cinfo);

  int numBytesInBuffer = cinfo.dest->next_output_byte - out_buf;

  flush(numBytesInBuffer);

  clientData.totalOutputBytes += numBytesInBuffer;

  safeDeleteArray(yArr);
  safeDeleteArray(cbArr);
  safeDeleteArray(crArr);

  jpeg_destroy_compress(&cinfo);

  return clientData.totalOutputBytes;
}

int jpegutil::Compress(
    /** Input image dimensions */
    int width, int height,
    /** Y Plane */
    unsigned char* yBuf, int yPStride, int yRStride,
    /** Cb Plane */
    unsigned char* cbBuf, int cbPStride, int cbRStride,
    /** Cr Plane */
    unsigned char* crBuf, int crPStride, int crRStride,
    /** Output */
    unsigned char* outBuf, size_t outBufCapacity,
    /** Jpeg compression parameters */
    int quality,
    /** Crop */
    int cropLeft, int cropTop, int cropRight, int cropBottom,
    /** Rotation (multiple of 90).  For example, rot90 = 1 implies a 90 degree
     * rotation. */
    int rot90) {
  int finalWidth;
  int finalHeight;
  finalWidth = cropRight - cropLeft;
  finalHeight = cropBottom - cropTop;

  rot90 %= 4;
  // for 90 and 270-degree rotations, flip the final width and height
  if (rot90 == 1) {
    finalWidth = cropBottom - cropTop;
    finalHeight = cropRight - cropLeft;
  } else if (rot90 == 3) {
    finalWidth = cropBottom - cropTop;
    finalHeight = cropRight - cropLeft;
  }

  const Plane yP = {width, height, yBuf, yPStride, yRStride};
  const Plane cbP = {width / 2, height / 2, cbBuf, cbPStride, cbRStride};
  const Plane crP = {width / 2, height / 2, crBuf, crPStride, crRStride};

  auto flush = [](size_t numBytes) {
    // do nothing
  };

  // Round up to the nearest multiple of 64.
  int y_row_length = (finalWidth + 16 + 63) & ~63;
  int cb_row_length = (finalWidth / 2 + 16 + 63) & ~63;
  int cr_row_length = (finalWidth / 2 + 16 + 63) & ~63;

  Transform yTrans = Transform::ForCropFollowedByRotation(
      cropLeft, cropTop, cropRight, cropBottom, rot90);

  Transform chromaTrans = Transform::ForCropFollowedByRotation(
      cropLeft / 2, cropTop / 2, cropRight / 2, cropBottom / 2, rot90);

  RowIterator<16> yIter(yP, yTrans, y_row_length);
  RowIterator<8> cbIter(cbP, chromaTrans, cb_row_length);
  RowIterator<8> crIter(crP, chromaTrans, cr_row_length);

  return Compress(finalWidth, finalHeight, yIter, cbIter, crIter, outBuf,
                  outBufCapacity, flush, quality);
}