Class: JPEG::Encoder

Inherits:
Object
  • Object
show all
Defined in:
ext/jpeg/jpeg.c

Instance Method Summary collapse

Constructor Details

#initialize(width, height, opts) ⇒ Object

initialize encoder object

Parameters:

  • width (Integer)

    width of input image (px)

  • height (Integer)

    height of input image (px)

  • opts (Hash)

    options to initialize object

Options Hash (opts):

  • :pixel_format (Symbol)

    specifies the format of the input image. possible values are: YUV422 YUYV RGB565 RGB RGB24 BGR BGR24 YUV444 YCbCr RGBX RGB32 BGRX BGR32 GRAYSCALE

  • :quality (Integer)

    specifies the quality of the compressed image. You can specify from 0 (lowest) to 100 (best).

  • :dct_method (Symbol)

    specifies how encoding is handled. possible values are: FASTEST ISLOW IFAST FLOAT



992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'ext/jpeg/jpeg.c', line 992

static VALUE
rb_encoder_initialize(int argc, VALUE *argv, VALUE self)
{
  jpeg_encode_t* ptr;
  VALUE exc;
  VALUE wd;
  VALUE ht;
  VALUE opt;

  /*
   * initialize
   */
  exc = Qnil;

  TypedData_Get_Struct(self, jpeg_encode_t, &jpeg_encoder_data_type, ptr);

  /*
   * parse arguments
   */
  rb_scan_args(argc, argv, "2:", &wd, &ht, &opt);

  /*
   * argument check
   */
  do {
    if (TYPE(wd) != T_FIXNUM) {
      exc = create_argument_error("invalid width");
      break;
    }

    if (TYPE(ht) != T_FIXNUM) {
      exc = create_argument_error("invalid height");
      break;
    }
  } while (0);

  /*
   * set context
   */ 
  if (!RTEST(exc)) {
    exc = set_encoder_context(ptr, FIX2INT(wd), FIX2INT(ht), opt);
  }

  /*
   * post process
   */
  if (RTEST(exc)) rb_exc_raise(exc);

  return Qtrue;
}

Instance Method Details

#encode(raw) ⇒ String Also known as: compress, <<

encode data

Returns encoded JPEG data.

Parameters:

  • raw (String)

    raw image data to encode.

Returns:

  • (String)

    encoded JPEG data.



1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
# File 'ext/jpeg/jpeg.c', line 1265

static VALUE
rb_encoder_encode(VALUE self, VALUE data)
{
  VALUE ret;
  int state;
  jpeg_encode_t* ptr;

  /*
   * initialize
   */
  ret   = Qnil;
  state = 0;

  TypedData_Get_Struct(self, jpeg_encode_t, &jpeg_encoder_data_type, ptr);

  /*
   * argument check
   */
  Check_Type(data, T_STRING);

  if (RSTRING_LEN(data) < ptr->data_size) {
    ARGUMENT_ERROR("image data is too short");
  }

  if (RSTRING_LEN(data) > ptr->data_size) {
    ARGUMENT_ERROR("image data is too large");
  }

  /*
   * alloc memory
   */
  jpeg_mem_dest(&ptr->cinfo, &ptr->buf.mem, &ptr->buf.size); 
  if (ptr->buf.mem == NULL) {
    RUNTIME_ERROR("jpeg_mem_dest() failed");
  }

  /*
   * prepare
   */
  SET_DATA(ptr, data);

  /*
   * do encode
   */
  ret = rb_protect(do_encode, (VALUE)ptr, &state);

  /*
   * post process
   */
  CLR_DATA(ptr);

  if (ptr->buf.mem != NULL) {
    free(ptr->buf.mem);

    ptr->buf.mem  = NULL;
    ptr->buf.size = 0;
  }

  if (state != 0) rb_jump_tag(state);

  return ret;
}