转载:http://www.cnblogs.com/imlucky/archive/2012/08/01/2617851.html


今天编译skia库,增加图片解码库时总是无效。按照此博客的方法修改后成功,特此转载。


android编译skia静态库时,图片解码库无法注册的问题

经过千辛万苦将skia编译成了静态库,但是发现图片解码都不成功,后来发现是图片解码库没有注册成功,可能是代码优化导致的,但是加上-O0编译选项也不行。后来就在SkImageDecoder_Factory.cpp中直接调用各个解码库的注册文件,结果png解码可以了,但是jpeg和gif编译不通过,后来发现时需要一个-fvisibility=hidden编译选项,增加后就OK了。修改的skia文件如下:

SkImageDecoder_Factory.cpp

/* libs/graphics/ports/SkImageDecoder_Factory.cpp
**
** Copyright 2006, 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 "SkImageDecoder.h"
#include "SkMovie.h"
#include "SkStream.h"
#include "SkTRegistry.h"
#include "dhlog.h" typedef SkTRegistry<SkImageDecoder*, SkStream*> DecodeReg; // N.B. You can't use "DecodeReg::gHead here" due to complex C++
// corner cases.
template DecodeReg* SkTRegistry<SkImageDecoder*, SkStream*>::gHead; #ifdef SK_ENABLE_LIBPNG
extern SkImageDecoder* sk_libpng_dfactory(SkStream*);
#endif
#ifdef SK_ENABLE_LIBJPEG
extern SkImageDecoder* sk_libjpeg_dfactory(SkStream*);
#endif
#ifdef SK_ENABLE_LIBGIF
extern SkImageDecoder* sk_libgif_dfactory(SkStream*);
#endif SkImageDecoder* SkImageDecoder::Factory(SkStream* stream) {
SkImageDecoder* codec = NULL;
const DecodeReg* curr = DecodeReg::Head();
logdh("SkImageDecoder::Factory curr=0x%x", curr);
while (curr) {
codec = curr->factory()(stream);
logdh("SkImageDecoder::Factory stream=0x%x codec=0x%x", stream, codec);
// we rewind here, because we promise later when we call "decode", that
// the stream will be at its beginning.
stream->rewind();
if (codec) {
return codec;
}
curr = curr->next();
}
#ifdef SK_ENABLE_LIBPNG
codec = sk_libpng_dfactory(stream);
if (codec) {
stream->rewind();
return codec;
}
#endif
#ifdef SK_ENABLE_LIBJPEG
logdh("SkImageDecoder::Factory enter SK_ENABLE_LIBJPEG");
//for jpeg decode codec = sk_libjpeg_dfactory(stream);
if (codec) {
logdh("SkImageDecoder::Factory codec=0x%x ok", codec);
stream->rewind();
return codec;
}
#endif
#ifdef SK_ENABLE_LIBGIF
//for gif decode
codec = sk_libgif_dfactory(stream);
if (codec) {
stream->rewind();
return codec;
}
#endif
return NULL;
} ///////////////////////////////////////////////////////////////////////// typedef SkTRegistry<SkMovie*, SkStream*> MovieReg; SkMovie* SkMovie::DecodeStream(SkStream* stream) {
const MovieReg* curr = MovieReg::Head();
while (curr) {
SkMovie* movie = curr->factory()(stream);
if (movie) {
return movie;
}
// we must rewind only if we got NULL, since we gave the stream to the
// movie, who may have already started reading from it
stream->rewind();
curr = curr->next();
}
return NULL;
}

SkImageDecoder_libgif.cpp

/* libs/graphics/images/SkImageDecoder_libgif.cpp
**
** Copyright 2006, 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 "SkImageDecoder.h"
#include "SkColor.h"
#include "SkColorPriv.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "SkPackBits.h" #include "gif_lib.h" class SkGIFImageDecoder : public SkImageDecoder {
public:
virtual Format getFormat() const {
return kGIF_Format;
} protected:
virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode);
}; static const uint8_t gStartingIterlaceYValue[] = {
0, 4, 2, 1
};
static const uint8_t gDeltaIterlaceYValue[] = {
8, 8, 4, 2
}; /* Implement the GIF interlace algorithm in an iterator.
1) grab every 8th line beginning at 0
2) grab every 8th line beginning at 4
3) grab every 4th line beginning at 2
4) grab every 2nd line beginning at 1
*/
class GifInterlaceIter {
public:
GifInterlaceIter(int height) : fHeight(height) {
fStartYPtr = gStartingIterlaceYValue;
fDeltaYPtr = gDeltaIterlaceYValue; fCurrY = *fStartYPtr++;
fDeltaY = *fDeltaYPtr++;
} int currY() const {
SkASSERT(fStartYPtr);
SkASSERT(fDeltaYPtr);
return fCurrY;
} void next() {
SkASSERT(fStartYPtr);
SkASSERT(fDeltaYPtr); int y = fCurrY + fDeltaY;
// We went from an if statement to a while loop so that we iterate
// through fStartYPtr until a valid row is found. This is so that images
// that are smaller than 5x5 will not trash memory.
while (y >= fHeight) {
if (gStartingIterlaceYValue +
SK_ARRAY_COUNT(gStartingIterlaceYValue) == fStartYPtr) {
// we done
SkDEBUGCODE(fStartYPtr = NULL;)
SkDEBUGCODE(fDeltaYPtr = NULL;)
y = 0;
} else {
y = *fStartYPtr++;
fDeltaY = *fDeltaYPtr++;
}
}
fCurrY = y;
} private:
const int fHeight;
int fCurrY;
int fDeltaY;
const uint8_t* fStartYPtr;
const uint8_t* fDeltaYPtr;
}; /////////////////////////////////////////////////////////////////////////////// //#define GIF_STAMP "GIF" /* First chars in file - GIF stamp. */
//#define GIF_STAMP_LEN (sizeof(GIF_STAMP) - 1) static int DecodeCallBackProc(GifFileType* fileType, GifByteType* out,
int size) {
SkStream* stream = (SkStream*) fileType->UserData;
return (int) stream->read(out, size);
} void CheckFreeExtension(SavedImage* Image) {
if (Image->ExtensionBlocks) {
FreeExtension(Image);
}
} // return NULL on failure
static const ColorMapObject* find_colormap(const GifFileType* gif) {
const ColorMapObject* cmap = gif->Image.ColorMap;
if (NULL == cmap) {
cmap = gif->SColorMap;
} if (NULL == cmap) {
// no colormap found
return NULL;
}
// some sanity checks
if (cmap && ((unsigned)cmap->ColorCount > 256 ||
cmap->ColorCount != (1 << cmap->BitsPerPixel))) {
cmap = NULL;
}
return cmap;
} // return -1 if not found (i.e. we're completely opaque)
static int find_transpIndex(const SavedImage& image, int colorCount) {
int transpIndex = -1;
for (int i = 0; i < image.ExtensionBlockCount; ++i) {
const ExtensionBlock* eb = image.ExtensionBlocks + i;
if (eb->Function == 0xF9 && eb->ByteCount == 4) {
if (eb->Bytes[0] & 1) {
transpIndex = (unsigned char)eb->Bytes[3];
// check for valid transpIndex
if (transpIndex >= colorCount) {
transpIndex = -1;
}
break;
}
}
}
return transpIndex;
} static bool error_return(GifFileType* gif, const SkBitmap& bm,
const char msg[]) {
#if 0
SkDebugf("libgif error <%s> bitmap [%d %d] pixels %p colortable %p\n",
msg, bm.width(), bm.height(), bm.getPixels(), bm.getColorTable());
#endif
return false;
} bool SkGIFImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* bm, Mode mode) {
GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc);
if (NULL == gif) {
return error_return(gif, *bm, "DGifOpen");
} SkAutoTCallIProc<GifFileType, DGifCloseFile> acp(gif); SavedImage temp_save;
temp_save.ExtensionBlocks=NULL;
temp_save.ExtensionBlockCount=0;
SkAutoTCallVProc<SavedImage, CheckFreeExtension> acp2(&temp_save); int width, height;
GifRecordType recType;
GifByteType *extData;
int transpIndex = -1; // -1 means we don't have it (yet) do {
if (DGifGetRecordType(gif, &recType) == GIF_ERROR) {
return error_return(gif, *bm, "DGifGetRecordType");
} switch (recType) {
case IMAGE_DESC_RECORD_TYPE: {
if (DGifGetImageDesc(gif) == GIF_ERROR) {
return error_return(gif, *bm, "IMAGE_DESC_RECORD_TYPE");
} if (gif->ImageCount < 1) { // sanity check
return error_return(gif, *bm, "ImageCount < 1");
} width = gif->SWidth;
height = gif->SHeight;
if (width <= 0 || height <= 0 ||
!this->chooseFromOneChoice(SkBitmap::kIndex8_Config,
width, height)) {
return error_return(gif, *bm, "chooseFromOneChoice");
} bm->setConfig(SkBitmap::kIndex8_Config, width, height);
if (SkImageDecoder::kDecodeBounds_Mode == mode)
return true; SavedImage* image = &gif->SavedImages[gif->ImageCount-1];
const GifImageDesc& desc = image->ImageDesc; // check for valid descriptor
if ( (desc.Top | desc.Left) < 0 ||
desc.Left + desc.Width > width ||
desc.Top + desc.Height > height) {
return error_return(gif, *bm, "TopLeft");
} // now we decode the colortable
int colorCount = 0;
{
const ColorMapObject* cmap = find_colormap(gif);
if (NULL == cmap) {
return error_return(gif, *bm, "null cmap");
} colorCount = cmap->ColorCount;
SkColorTable* ctable = SkNEW_ARGS(SkColorTable, (colorCount));
SkPMColor* colorPtr = ctable->lockColors();
for (int index = 0; index < colorCount; index++)
colorPtr[index] = SkPackARGB32(0xFF,
cmap->Colors[index].Red,
cmap->Colors[index].Green,
cmap->Colors[index].Blue); transpIndex = find_transpIndex(temp_save, colorCount);
if (transpIndex < 0)
ctable->setFlags(ctable->getFlags() | SkColorTable::kColorsAreOpaque_Flag);
else
colorPtr[transpIndex] = 0; // ram in a transparent SkPMColor
ctable->unlockColors(true); SkAutoUnref aurts(ctable);
if (!this->allocPixelRef(bm, ctable)) {
return error_return(gif, *bm, "allocPixelRef");
}
} SkAutoLockPixels alp(*bm); // time to decode the scanlines
//
uint8_t* scanline = bm->getAddr8(0, 0);
const int rowBytes = bm->rowBytes();
const int innerWidth = desc.Width;
const int innerHeight = desc.Height; // abort if either inner dimension is <= 0
if (innerWidth <= 0 || innerHeight <= 0) {
return error_return(gif, *bm, "non-pos inner width/height");
} // are we only a subset of the total bounds?
if ((desc.Top | desc.Left) > 0 ||
innerWidth < width || innerHeight < height)
{
int fill;
if (transpIndex >= 0) {
fill = transpIndex;
} else {
fill = gif->SBackGroundColor;
}
// check for valid fill index/color
if (static_cast<unsigned>(fill) >=
static_cast<unsigned>(colorCount)) {
fill = 0;
}
memset(scanline, fill, bm->getSize());
// bump our starting address
scanline += desc.Top * rowBytes + desc.Left;
} // now decode each scanline
if (gif->Image.Interlace)
{
GifInterlaceIter iter(innerHeight);
for (int y = 0; y < innerHeight; y++)
{
uint8_t* row = scanline + iter.currY() * rowBytes;
if (DGifGetLine(gif, row, innerWidth) == GIF_ERROR) {
return error_return(gif, *bm, "interlace DGifGetLine");
}
iter.next();
}
}
else
{
// easy, non-interlace case
for (int y = 0; y < innerHeight; y++) {
if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) {
return error_return(gif, *bm, "DGifGetLine");
}
scanline += rowBytes;
}
}
goto DONE;
} break; case EXTENSION_RECORD_TYPE:
if (DGifGetExtension(gif, &temp_save.Function,
&extData) == GIF_ERROR) {
return error_return(gif, *bm, "DGifGetExtension");
} while (extData != NULL) {
/* Create an extension block with our data */
if (AddExtensionBlock(&temp_save, extData[0],
&extData[1]) == GIF_ERROR) {
return error_return(gif, *bm, "AddExtensionBlock");
}
if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) {
return error_return(gif, *bm, "DGifGetExtensionNext");
}
temp_save.Function = 0;
}
break; case TERMINATE_RECORD_TYPE:
break; default: /* Should be trapped by DGifGetRecordType */
break;
}
} while (recType != TERMINATE_RECORD_TYPE); DONE:
return true;
} /////////////////////////////////////////////////////////////////////////////// #include "SkTRegistry.h" static SkImageDecoder* Factory(SkStream* stream) {
char buf[GIF_STAMP_LEN];
if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) {
if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
return SkNEW(SkGIFImageDecoder);
}
}
return NULL;
} #ifdef SK_ENABLE_LIBGIF
SkImageDecoder* sk_libgif_dfactory(SkStream*);
#endif SkImageDecoder* sk_libgif_dfactory(SkStream* stream) {
char buf[GIF_STAMP_LEN];
if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) {
if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
return SkNEW(SkGIFImageDecoder);
}
}
return NULL;
} static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libgif_dfactory);

SkImageDecoder_libjpeg.cpp(略)

SkImageDecoder_libpng.cpp(略)

然后再skia的android.mk文件中增加:

LOCAL_CFLAGS += -DSK_ENABLE_LIBPNG

LOCAL_CFLAGS += -DSK_ENABLE_LIBJPEG

LOCAL_CFLAGS += -DSK_ENABLE_LIBGIF

编译skia静态库时,图片解码库无法注册的问题的更多相关文章

  1. gcc新版本号引起的编译错误(命令运行时的外部库输入位置)

    昨天,遇到一个比較bug的错误,用gcc来编译几个简单的文件出错,编译环境为x86_64的Ubuntu12.04.gcc版本号号例如以下: gcc (Ubuntu/Linaro 4.6.3-1ubun ...

  2. opencv编译静态库时选择MD模式无效的原因

    在Cmake-gui上看到的明明是MD运行库依赖,生成MS项目时却变成了MT运行库依赖. 原因在于编译静态库时内部做了自动替换.

  3. [转]Linux下g++编译与使用静态库(.a)和动态库(.os) (+修正与解释)

    在windows环境下,我们通常在IDE如VS的工程中开发C++项目,对于生成和使用静态库(*.lib)与动态库(*.dll)可能都已经比较熟悉,但是,在linux环境下,则是另一套模式,对应的静态库 ...

  4. VS2015——命令行下编译、静态库动态库制作以及断点调试

    c程序编译流程 程序的基本流程如图: 1. 预处理 预处理相当于根据预处理指令组装新的C/C++程序.经过预处理,会产生一个没有宏定义,没有条件编译指令,没有特殊符号的输出文件,这个文件的含义同原本的 ...

  5. 不用第三方解码库取得图片宽高 附完整C++算法实现代码

    在特定的应用场景下,有时候我们只是想获取图片的宽高, 但不想通过解码图片才取得这个信息. 预先知道图片的宽高信息,进而提速图片加载,预处理等相关操作以提升体验. 在stackoverflow有一篇相关 ...

  6. linux下 GCC编译链接静态库&动态库

    静态库 有时候需要把一组代码编译成一个库,这个库在很多项目中都要用到,例如libc就是这样一个库, 我们在不同的程序中都会用到libc中的库函数(例如printf),也会用到libc中的变量(例如以后 ...

  7. ORTP编译为静态库的问题

    项目中需要用到ORTP,我采用的编译环境是 VC2013,当我在项目设置中将设置为静态库是,发现没有导出函数,比如在需要连接 oRTP.lib库时提示 找不到 ORTP_init; 解决办法是 :在O ...

  8. 编译 Android 版本的 Opus 音频编解码库的方法

    Opus 音频编解码库是 Speex 音频编解码库的下一代版本,从编解码性能以及质量上来讲都有了长足的进步.Opus 的编译非常简单,但是官方并未给出详细的 Android 版本编译指南,查找了大量资 ...

  9. EasyDarwin开源音频解码项目EasyAudioDecoder:基于ffmpeg的安卓音频(AAC、G726)解码库(第一部分,ffmpeg-android的编译)

    ffmpeg是一套开源的,完整的流媒体解决方案.基于它可以很轻松构建一些强大的应用程序.对于流媒体这个行业,ffmpeg就像圣经一样的存在.为了表达敬意,在这里把ffmpeg官网的一段简介搬过来,ff ...

随机推荐

  1. Reversing Encryption(模拟水题)

    A string ss of length nn can be encrypted(加密) by the following algorithm: iterate(迭代) over all divis ...

  2. c# 有无符号值进一步了解

    1.编写过程中用到了short类型(有符号型,值范围含负值).两个正数之和得负. 改为int或unsigned short 均可. 2.注意,short型(-32768,32767)举例:做自加运算, ...

  3. osg::Vec2 Vec3 Vec4

    osg::Vec2可以用于保存2D纹理坐标. osg::Vec3是一个三维浮点数数组. osg::Vec4用于保存颜色数据.

  4. arcgis api for javascript 各个版本的SDK下载

    1.首先,进入下载网站,需要登录才能下载.下载链接 2.选择需要下载的版本,进行下载.

  5. PAT 1058 选择题

    https://pintia.cn/problem-sets/994805260223102976/problems/994805270356541440 批改多选题是比较麻烦的事情,本题就请你写个程 ...

  6. HDU 2163 Palindromes

    http://acm.hdu.edu.cn/showproblem.php?pid=2163 Problem Description Write a program to determine whet ...

  7. Ubuntu启用root账号登录系统

    使用root账号登陆Ubuntu系统,实现起来本身没啥难度,运行passwd root即可,然后在/etc/ssh/sshd_config里面修改PermitRootLogin yes即可.不过研究的 ...

  8. 第四部分shell编程5项目二分发系统

    第一部分:expect讲解expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令.当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令.但当不能使用密钥验证的时候,我 ...

  9. windows与linux下执行.class(包含main方法)

    来源:http://blog.csdn.net/hanqunfeng/article/details/4327325 一般来说,执行一个java文件采用执行jar包的方式最为方便(java -jar ...

  10. 第52天:offset家族、scroll家族和client家族的区别

    一.offset家族 1.offsetWidth offsetHeight offsetLeft offsetTop offsetParent共同组成了offset家族,用来获取元素尺寸. offse ...