Class: PNG::Encoder

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# File 'ext/png/png.c', line 912

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

  /*
   * initialize
   */
  exc = Qnil;

  /*
   * strip object
   */
  TypedData_Get_Struct(self, png_encoder_t, &png_encoder_data_type, ptr);

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

  /*
   * check argument
   */
  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), opts);
  }

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

  return self;
}

Instance Method Details

#encode(data) ⇒ Object Also known as: compress, <<



1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
# File 'ext/png/png.c', line 1029

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

  /*
   * initialize
   */
  ret   = rb_str_buf_new(0);
  exc   = Qnil;
  state = 0;

  /*
   * strip object
   */
  TypedData_Get_Struct(self, png_encoder_t, &png_encoder_data_type, ptr);

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

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

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

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

  /*
   * do encode
   */
  if (!RTEST(exc)) {
    rb_protect(do_encode, (VALUE)ptr, &state);
  }

  /*
   * post process
   */
  if(state == 0) {
    rb_ivar_set(ret, rb_intern("warn"), ptr->warn_msg);
  }  
 
  CLR_DATA(ptr);
 
  if (state != 0) {
    rb_jump_tag(state);
  }

  /*
   * post process
   */
  ptr->ibuf = Qnil;
  ptr->obuf = Qnil;
  
  if (RTEST(exc)) rb_exc_raise(exc);

  return ret;
}