Skip to content

Commit

Permalink
fix encode config
Browse files Browse the repository at this point in the history
  • Loading branch information
le0pard committed Nov 23, 2013
1 parent 595d55e commit 6517ac0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
45 changes: 23 additions & 22 deletions ext/webp_ffi/webp_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ int webp_encode(const char *in_file, const char *out_file, const FfiWebpEncodeCo
int keep_alpha = 1;
WebPPicture picture;
WebPConfig config;

if (!WebPPictureInit(&picture) ||
!WebPConfigInit(&config)) {
//fprintf(stderr, "Error! Version mismatch!\n");
return 1;
}

// OPTIONS BEGIN
if (encode_config->lossless == 0 || encode_config->lossless == 1){
config.lossless = encode_config->lossless;
Expand Down Expand Up @@ -120,25 +127,19 @@ int webp_encode(const char *in_file, const char *out_file, const FfiWebpEncodeCo
picture.height = encode_config->height;
}
// OPTIONS END

if (!WebPPictureInit(&picture) ||
!WebPConfigInit(&config)) {
//fprintf(stderr, "Error! Version mismatch!\n");
return 1;
}


if (!WebPValidateConfig(&config)) {
//fprintf(stderr, "Error! Invalid configuration.\n");
return_value = 2;
goto Error;
}

if (!UtilReadPicture(in_file, &picture, keep_alpha)) {
//fprintf(stderr, "Error! Cannot read input picture file '%s'\n", in_file);
return_value = 3;
goto Error;
}

out = fopen(out_file, "wb");
if (out == NULL) {
//fprintf(stderr, "Error! Cannot open output file '%s'\n", out_file);
Expand All @@ -147,34 +148,34 @@ int webp_encode(const char *in_file, const char *out_file, const FfiWebpEncodeCo
}
picture.writer = EncodeWriter;
picture.custom_ptr = (void*)out;

if ((encode_config->crop_w | encode_config->crop_h) > 0){
if (!WebPPictureView(&picture, encode_config->crop_x, encode_config->crop_y, encode_config->crop_w, encode_config->crop_h, &picture)) {
//fprintf(stderr, "Error! Cannot crop picture\n");
return_value = 5;
goto Error;
}
}

if ((encode_config->resize_w | encode_config->resize_h) > 0) {
if (!WebPPictureRescale(&picture, encode_config->resize_w, encode_config->resize_h)) {
//fprintf(stderr, "Error! Cannot resize picture\n");
return_value = 6;
goto Error;
}
}

if (picture.extra_info_type > 0) {
AllocExtraInfo(&picture);
}

if (!WebPEncode(&config, &picture)) {
//fprintf(stderr, "Error! Cannot encode picture as WebP\n");
return_value = 7;
goto Error;
}
return_value = 0;

Error:
free(picture.extra_info);
WebPPictureFree(&picture);
Expand All @@ -198,7 +199,7 @@ int webp_decode(const char *in_file, const char *out_file, const FfiWebpDecodeCo
//fprintf(stderr, "Library version mismatch!\n");
return 1;
}

if (decode_config->output_format != format){
format = decode_config->output_format;
}
Expand All @@ -223,20 +224,20 @@ int webp_decode(const char *in_file, const char *out_file, const FfiWebpDecodeCo
config.options.scaled_width = decode_config->resize_w;
config.options.scaled_height = decode_config->resize_h;
}

VP8StatusCode status = VP8_STATUS_OK;
size_t data_size = 0;
const uint8_t* data = NULL;

if (!UtilReadFile(in_file, &data, &data_size)) return -1;

status = WebPGetFeatures(data, data_size, bitstream);
if (status != VP8_STATUS_OK) {
//fprintf(stderr, "This is invalid webp image!\n");
return_value = 2;
goto Error;
}

switch (format) {
case PNG:
output_buffer->colorspace = bitstream->has_alpha ? MODE_RGBA : MODE_RGB;
Expand All @@ -258,16 +259,16 @@ int webp_decode(const char *in_file, const char *out_file, const FfiWebpDecodeCo
return 3;
}
status = WebPDecode(data, data_size, &config);

if (status != VP8_STATUS_OK) {
//fprintf(stderr, "Decoding of %s failed.\n", in_file);
return_value = 4;
goto Error;
}
UtilSaveOutput(output_buffer, format, out_file);
return_value = 0;
Error:

Error:
free((void*)data);
WebPFreeDecBuffer(output_buffer);
return return_value;
Expand Down
12 changes: 6 additions & 6 deletions spec/webp_ffi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
}
}

before :all do
@out_dir = File.expand_path(File.join(File.dirname(__FILE__), "../tmp/"))
Dir.mkdir(@out_dir) unless File.exists?(@out_dir)
Expand All @@ -45,17 +45,17 @@
expect(WebP::C.test_c(100)).to eq(200)
expect(WebP::C.test_c(150)).to eq(250)
end

it "decoder version" do
expect(WebP.decoder_version).not_to be_nil
expect(WebP.decoder_version).to match(/^([0-9]+)\.([0-9]+)\.([0-9]+)$/)
end

it "encoder version" do
expect(WebP.encoder_version).not_to be_nil
expect(WebP.decoder_version).to match(/^([0-9]+)\.([0-9]+)\.([0-9]+)$/)
end

context "webp_size" do
factories[:webp].each do |image|
it "#{image} image size == #{factories[:info][image][:size]}" do
Expand All @@ -73,7 +73,7 @@
expect { WebP.webp_size(data) }.to raise_error WebP::InvalidImageFormatError
end
end

context "encode" do
factories[:png].each do |image|
it "#{image}.png image" do
Expand Down Expand Up @@ -122,7 +122,7 @@
end
end
end

context "decode" do
factories[:webp].each do |image|
it "#{image}.webp image" do
Expand Down

0 comments on commit 6517ac0

Please sign in to comment.