Fixing nits

- Const Handling of picture object, removed bitwriter from encoder.

Change-Id: Id943854de09324de81cca615ada960390c4b8152
This commit is contained in:
Vikas Arora 2012-04-02 10:58:36 +00:00 committed by James Zern
parent 227110c4c3
commit fdccaaddcf
6 changed files with 51 additions and 44 deletions

View File

@ -105,13 +105,15 @@ int WebPPictureAlloc(WebPPicture* const picture) {
mem += uv0_size; mem += uv0_size;
} }
} else { } else {
const uint64_t argb_size = (uint64_t)width * height;
const uint64_t total_size = argb_size * sizeof(*picture->argb);
if (width <= 0 || height <= 0 || if (width <= 0 || height <= 0 ||
width >= 0x4000 || height >= 0x4000) { argb_size >= (1ULL << 40) ||
(size_t)total_size != total_size) {
return 0; return 0;
} }
WebPPictureFree(picture); // erase previous buffer WebPPictureFree(picture); // erase previous buffer
picture->argb = (uint32_t*)malloc(width * height * picture->argb = (uint32_t*)malloc(total_size);
sizeof(*picture->argb));
if (picture->argb == NULL) return 0; if (picture->argb == NULL) return 0;
picture->argb_stride = width; picture->argb_stride = width;
} }
@ -526,8 +528,11 @@ static int Import(WebPPicture* const picture,
for (y = 0; y < height; ++y) { for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) { for (x = 0; x < width; ++x) {
const int offset = step * x + y * rgb_stride; const int offset = step * x + y * rgb_stride;
const uint32_t argb = 0xff000000 | (r_ptr[offset] << 16) | const uint32_t argb =
(g_ptr[offset] << 8) | (b_ptr[offset]); 0xff000000 |
(r_ptr[offset] << 16) |
(g_ptr[offset] << 8) |
(b_ptr[offset]);
picture->argb[x + y * picture->argb_stride] = argb; picture->argb[x + y * picture->argb_stride] = argb;
} }
} }
@ -537,8 +542,11 @@ static int Import(WebPPicture* const picture,
for (y = 0; y < height; ++y) { for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) { for (x = 0; x < width; ++x) {
const int offset = step * x + y * rgb_stride; const int offset = step * x + y * rgb_stride;
const uint32_t argb = (a_ptr[offset] << 24) | (r_ptr[offset] << 16) | const uint32_t argb =
(g_ptr[offset] << 8) | (b_ptr[offset]); (a_ptr[offset] << 24) |
(r_ptr[offset] << 16) |
(g_ptr[offset] << 8) |
(b_ptr[offset]);
picture->argb[x + y * picture->argb_stride] = argb; picture->argb[x + y * picture->argb_stride] = argb;
} }
} }

View File

@ -5,7 +5,7 @@
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/ // Additional IP Rights Grant: http://www.webmproject.org/license/additional/
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// //
// main entry for the decoder // main entry for the lossless encoder.
// //
// Author: Vikas Arora (vikaas.arora@gmail.com) // Author: Vikas Arora (vikaas.arora@gmail.com)
// //
@ -52,7 +52,7 @@ static int ApplyCrossColorFilter(VP8LEncoder* const enc) {
return 1; return 1;
} }
static void EvalEmergingPalette(VP8LEncoder* const enc) { static void EvalColorCache(VP8LEncoder* const enc) {
(void)enc; (void)enc;
} }
@ -81,13 +81,14 @@ static WebPEncodingError WriteRiffHeader(VP8LEncoder* const enc,
return VP8_ENC_OK; return VP8_ENC_OK;
} }
static WebPEncodingError WriteImage(VP8LEncoder* const enc) { static WebPEncodingError WriteImage(VP8LEncoder* const enc,
VP8LBitWriter* const bw) {
size_t riff_size, vp8l_size, webpll_size, pad; size_t riff_size, vp8l_size, webpll_size, pad;
WebPPicture* const pic = enc->pic_; const WebPPicture* const pic = enc->pic_;
WebPEncodingError err = VP8_ENC_OK; WebPEncodingError err = VP8_ENC_OK;
const uint8_t* const webpll_data = VP8LBitWriterFinish(&enc->bw_); const uint8_t* const webpll_data = VP8LBitWriterFinish(bw);
webpll_size = VP8LBitWriterNumBytes(&enc->bw_); webpll_size = VP8LBitWriterNumBytes(bw);
vp8l_size = SIGNATURE_SIZE + webpll_size; vp8l_size = SIGNATURE_SIZE + webpll_size;
pad = vp8l_size & 1; pad = vp8l_size & 1;
vp8l_size += pad; vp8l_size += pad;
@ -111,14 +112,12 @@ static WebPEncodingError WriteImage(VP8LEncoder* const enc) {
return VP8_ENC_OK; return VP8_ENC_OK;
Error: Error:
WebPEncodingSetError(pic, err);
return err; return err;
} }
static VP8LEncoder* InitVP8LEncoder(const WebPConfig* const config, static VP8LEncoder* InitVP8LEncoder(const WebPConfig* const config,
WebPPicture* const picture) { WebPPicture* const picture) {
VP8LEncoder* enc; VP8LEncoder* enc;
const int kEstmatedEncodeSize = (picture->width * picture->height) >> 1;
(void)config; (void)config;
enc = (VP8LEncoder*)malloc(sizeof(*enc)); enc = (VP8LEncoder*)malloc(sizeof(*enc));
@ -133,23 +132,18 @@ static VP8LEncoder* InitVP8LEncoder(const WebPConfig* const config,
// TODO: Use config.quality to initialize histo_bits_ and transform_bits_. // TODO: Use config.quality to initialize histo_bits_ and transform_bits_.
enc->histo_bits_ = 4; enc->histo_bits_ = 4;
enc->transform_bits_ = 4; enc->transform_bits_ = 4;
VP8LBitWriterInit(&enc->bw_, kEstmatedEncodeSize);
return enc; return enc;
} }
static WebPEncodingError WriteImageSize(VP8LEncoder* const enc) { static void WriteImageSize(VP8LEncoder* const enc, VP8LBitWriter* const bw) {
WebPEncodingError status = VP8_ENC_OK;
WebPPicture* const pic = enc->pic_; WebPPicture* const pic = enc->pic_;
const int width = pic->width - 1; const int width = pic->width - 1;
const int height = pic->height - 1; const int height = pic->height -1;
if (width < 0x4000 && height < 0x4000) { assert(width < WEBP_MAX_DIMENSION && height < WEBP_MAX_DIMENSION);
VP8LWriteBits(&enc->bw_, kImageSizeBits, width);
VP8LWriteBits(&enc->bw_, kImageSizeBits, height); VP8LWriteBits(bw, kImageSizeBits, width);
} else { VP8LWriteBits(bw, kImageSizeBits, height);
status = VP8_ENC_ERROR_BAD_DIMENSION;
}
return status;
} }
static void DeleteVP8LEncoder(VP8LEncoder* enc) { static void DeleteVP8LEncoder(VP8LEncoder* enc) {
@ -161,11 +155,22 @@ int VP8LEncodeImage(const WebPConfig* const config,
int ok = 0; int ok = 0;
VP8LEncoder* enc = NULL; VP8LEncoder* enc = NULL;
WebPEncodingError err = VP8_ENC_OK; WebPEncodingError err = VP8_ENC_OK;
VP8LBitWriter bw;
if (config == NULL || picture == NULL) return 0; if (config == NULL || picture == NULL) return 0;
if (picture->argb == NULL) return 0;
if (picture->argb == NULL) {
err = VP8_ENC_ERROR_NULL_PARAMETER;
goto Error;
}
enc = InitVP8LEncoder(config, picture); enc = InitVP8LEncoder(config, picture);
if (enc == NULL) goto Error; if (enc == NULL) {
err = VP8_ENC_ERROR_NULL_PARAMETER;
goto Error;
}
VP8LBitWriterInit(&bw, (picture->width * picture->height) >> 1);
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Analyze image (entropy, num_palettes etc) // Analyze image (entropy, num_palettes etc)
@ -177,11 +182,7 @@ int VP8LEncodeImage(const WebPConfig* const config,
} }
// Write image size. // Write image size.
err = WriteImageSize(enc); WriteImageSize(enc, &bw);
if (err != VP8_ENC_OK) {
ok = 0;
goto Error;
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Apply transforms and write transform data. // Apply transforms and write transform data.
@ -196,8 +197,8 @@ int VP8LEncodeImage(const WebPConfig* const config,
if (!ApplyCrossColorFilter(enc)) goto Error; if (!ApplyCrossColorFilter(enc)) goto Error;
} }
if (enc->use_emerging_palette_) { if (enc->use_color_cache) {
EvalEmergingPalette(enc); EvalColorCache(enc);
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -206,14 +207,14 @@ int VP8LEncodeImage(const WebPConfig* const config,
ok = EncodeImageInternal(enc); ok = EncodeImageInternal(enc);
if (!ok) goto Error; if (!ok) goto Error;
err = WriteImage(enc); err = WriteImage(enc, &bw);
if (err != VP8_ENC_OK) { if (err != VP8_ENC_OK) {
ok = 0; ok = 0;
goto Error; goto Error;
} }
Error: Error:
VP8LBitWriterDestroy(&enc->bw_); VP8LBitWriterDestroy(&bw);
DeleteVP8LEncoder(enc); DeleteVP8LEncoder(enc);
if (!ok) { if (!ok) {
// TODO(vikasa): err is not set for all error paths. Set default err. // TODO(vikasa): err is not set for all error paths. Set default err.

View File

@ -44,9 +44,7 @@ typedef struct {
int num_palette_colors; int num_palette_colors;
int use_predict_; int use_predict_;
int use_cross_color_; int use_cross_color_;
int use_emerging_palette_; int use_color_cache;
VP8LBitWriter bw_;
} VP8LEncoder; } VP8LEncoder;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -367,7 +367,7 @@ int WebPEncode(const WebPConfig* const config, WebPPicture* const pic) {
if (pic->argb == NULL) if (pic->argb == NULL)
return WebPEncodingSetError(pic, VP8_ENC_ERROR_NULL_PARAMETER); return WebPEncodingSetError(pic, VP8_ENC_ERROR_NULL_PARAMETER);
ok = VP8LEncodeImage(config, pic); ok = VP8LEncodeImage(config, pic); // Sets pic->error in case of problem.
} }
return ok; return ok;

View File

@ -90,7 +90,7 @@ static WEBP_INLINE uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw) {
return bw->buf_; return bw->buf_;
} }
// Returns 1 on success. // Returns 0 in case of memory allocation error.
int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size); int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size);
void VP8LBitWriterDestroy(VP8LBitWriter* const bw); void VP8LBitWriterDestroy(VP8LBitWriter* const bw);
@ -111,7 +111,7 @@ void VP8LBitWriterDestroy(VP8LBitWriter* const bw);
// For n bits, we take the last 5 bytes, OR that with high bits in BYTE-0, // For n bits, we take the last 5 bytes, OR that with high bits in BYTE-0,
// and locate the rest in BYTE+1 and BYTE+2. // and locate the rest in BYTE+1 and BYTE+2.
// //
// returns 1 on success. // VP8LBitWriter's error_ flag is set in case of memory allocation error.
void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits); void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -218,7 +218,7 @@ struct WebPPicture {
WebPProgressHook progress_hook; // if not NULL, called while encoding. WebPProgressHook progress_hook; // if not NULL, called while encoding.
int use_argb_input; // Flag for encoder to read argb pixels as input. int use_argb_input; // Flag for encoder to use argb pixels as input.
uint32_t* argb; // Pointer to argb (32 bit) plane. uint32_t* argb; // Pointer to argb (32 bit) plane.
int argb_stride; // This is stride in pixels units, not bytes. int argb_stride; // This is stride in pixels units, not bytes.
}; };