Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从图像中提取缩略图的最快方法是什么?_C++_Winapi_Thumbnails_Gdi+ - Fatal编程技术网

C++ 从图像中提取缩略图的最快方法是什么?

C++ 从图像中提取缩略图的最快方法是什么?,c++,winapi,thumbnails,gdi+,C++,Winapi,Thumbnails,Gdi+,我想从图像中提取缩略图,所以我尝试使用GDI+。我所做的是创建一个新的小型位图来保存缩略图,并使用Graphics::DrawImage()将位图绘制到其中(从而获得缩略图) 但是在大量图像上使用这种方法时,速度会变得非常慢。因此,有没有一种方法可以加速它(只使用Windows API而不使用外部库) 注意:我知道有些图像会在其中存储缩略图,但我希望从没有存储缩略图的图像中提取缩略图。您可以通过从EXIF加载缩略图来提高速度(对于JPEG和其他一些格式)。但是,GetThumbnailImage

我想从图像中提取缩略图,所以我尝试使用GDI+。我所做的是创建一个新的小型
位图来保存缩略图,并使用
Graphics::DrawImage()
将位图绘制到其中(从而获得缩略图)

但是在大量图像上使用这种方法时,速度会变得非常慢。因此,有没有一种方法可以加速它(只使用Windows API而不使用外部库)


注意:我知道有些图像会在其中存储缩略图,但我希望从没有存储缩略图的图像中提取缩略图。

您可以通过从EXIF加载缩略图来提高速度(对于JPEG和其他一些格式)。但是,
GetThumbnailImage
的结果并不好。下面是另一个实现:

用法:

Gdiplus::Bitmap* thumb = GetThumbnail(filename, thumbWidth, thumbHeight);
代码:

PropertyItem*GetPropertyItemFromImage(Gdiplus::Image*bm,PROPID-PROPID){
UINT itemSize=bm->GetPropertyItemSize(propId);
如果(!itemSize){
返回0;
}
PropertyItem*item=reinterpret_cast(malloc(itemSize));
如果(bm->GetPropertyItem(propId,itemSize,item)!=Ok){
免费(项目);
返回0;
}
退货项目;
}
UINT VoidToInt(void*数据,无符号整数大小){
开关(尺寸){
案例8:
返回*重新解释(数据);
案例4:
返回*重新解释(数据);
案例2:
返回*重新解释(数据);
违约:
返回*重新解释(数据);
}
}
typedef IStream*(STDAPICALLTYPE*SHCreateMemStreamFuncType)(常量字节*pInit,UINT cbInit);
SHCreateMemStreamFuncType SHCreateMemStreamFunc=0;
boolisvista(){
静态int-isVista=-1;
如果(isVista==-1)
{
OSVERSIONINFO osver;
osver.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
isVista=(::GetVersionEx(&osver)&&
osver.dwPlatformId==VER\u平台\u WIN32\n&&
(osver.dwMajorVersion>=6));
}
返回isVista!=FALSE;
}
Gdiplus::位图*BitmapFromMemory(字节*数据,无符号整数imageSize){
if(WinUtils::IsVista()){
如果(!SHCreateMemStreamFunc){
HMODULE lib=LoadLibrary(_T(“Shlwapi.dll”);
SHCreateMemStreamFunc=reinterpret_cast(GetProcAddress(lib,“SHCreateMemStream”);
如果(!SHCreateMemStreamFunc){
返回0;
}
}
Gdiplus::位图*位图;
IStream*pStream=SHCreateMemStreamFunc(数据,图像大小);
中频(pStream){
bitmap=Gdiplus::bitmap::FromStream(pStream);
pStream->Release();
如果(位图){
如果(位图->GetLastStatus()==Gdiplus::Ok){
返回位图;
}
删除位图;
}
}
}否则{
HGLOBAL buffer=::GlobalAlloc(GMEM_MOVEABLE,imageSize);
if(缓冲区){
void*pBuffer=::GlobalLock(缓冲区);
if(pBuffer){
Gdiplus::位图*位图;
CopyMemory(pBuffer、数据、图像大小);
IStream*pStream=NULL;
如果(::CreateStreamOnHGlobal(缓冲区、假和pStream)==S_OK){
bitmap=Gdiplus::bitmap::FromStream(pStream);
pStream->Release();
如果(位图){
如果(位图->GetLastStatus()==Gdiplus::Ok){
返回位图;
}
删除位图;
}
}
::GlobalUnlock(缓冲区);
}
::全局自由(缓冲区);
}
}
返回0;
}
//基于原始方法http://danbystrom.se/2009/01/05/imagegetthumbnailimage-and-beyond/
Gdiplus::Bitmap*GetThumbnail(Gdiplus::Image*bm,int-width,int-height,Gdiplus::Size*realSize=0){
使用名称空间Gdiplus;
if(realSize){
realSize->Width=bm->GetWidth();
realSize->Height=bm->GetHeight();
}
大小sz=自适应比例大小(大小(宽度,高度),大小(bm->GetWidth(),bm->GetHeight());
位图*res=新位图(sz.宽度,sz.高度);
图形gr(res);
gr.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
UINT size=bm->GetPropertyItemSize(PropertyTagThumbnailData);
如果(尺寸){
//从EXIF数据加载缩略图(快速)
枚举ThumbCompression{ThumbCompressionJPEG,ThumbCompressionRGB,ThumbCompressionYCbCr,ThumbCompressionUnknown}
压缩=拇指压缩JPEG;
PropertyItem*thumbnailFormatItem=GetPropertyItemFromImage(bm,PropertyTagThumbnailFormat);
如果(thumbnailFormatItem){
UINT format=VoidToInt(thumbnailFormatItem->value,thumbnailFormatItem->length);
如果(格式==0){
压缩=拇指压缩RGB;
}else if(格式==1){
压缩=拇指压缩JPEG;
}否则{
压缩=拇指压缩未知;
}
免费(图钉格式项目);
}否则{
PropertyItem*compressionItem=GetPropertyItemFromImage(bm,PropertyTagThumbnailCompression);
if(压缩项){
WORD compressionTag=*重新解释(压缩项->值);
如果(压缩标签==1){
压缩=拇指压缩RGB;
PropertyItem*photometricInterpretationItem=GetPropertyItemFromImage(bm,PropertyTagPhotometricInterp);
if(光度测量解释项目){
单词photommetricinterpretationtag=VoidToInt(photommetricinterpretationItem->value,photommetricinterpretationItem->length);
免费(光度测量解释技术);
如果(光度计解释标记==6){
压缩=拇指压缩YCBCR;
PropertyItem* GetPropertyItemFromImage(Gdiplus::Image* bm, PROPID propId) {
    UINT itemSize = bm->GetPropertyItemSize(propId);
    if (!itemSize) {
        return 0;
    }
    PropertyItem* item = reinterpret_cast<PropertyItem*>(malloc(itemSize));
    if (bm->GetPropertyItem(propId, itemSize, item) != Ok) {
        free(item);
        return 0;
    }
    return item;
}

UINT VoidToInt(void* data, unsigned int size) {
    switch (size) {
        case 8:
            return *reinterpret_cast<UINT*>(data);
        case 4:
            return *reinterpret_cast<DWORD*>(data);
        case 2:
            return *reinterpret_cast<WORD*>(data);
        default:
            return *reinterpret_cast<BYTE*>(data);
    }
}

typedef IStream * (STDAPICALLTYPE *SHCreateMemStreamFuncType)(const BYTE *pInit, UINT cbInit);
SHCreateMemStreamFuncType SHCreateMemStreamFunc = 0;

bool IsVista() {
    static int isVista = -1;
    if (isVista == -1)
    {
        OSVERSIONINFO osver;
        osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

        isVista = (::GetVersionEx(&osver) &&
            osver.dwPlatformId == VER_PLATFORM_WIN32_NT &&
            (osver.dwMajorVersion >= 6));
    }
    return isVista != FALSE;
}

Gdiplus::Bitmap* BitmapFromMemory(BYTE* data, unsigned int imageSize) {
    if (WinUtils::IsVista()) {
        if (!SHCreateMemStreamFunc) {
            HMODULE lib = LoadLibrary(_T("Shlwapi.dll"));
            SHCreateMemStreamFunc = reinterpret_cast<SHCreateMemStreamFuncType>(GetProcAddress(lib, "SHCreateMemStream"));
            if (!SHCreateMemStreamFunc) {
                return 0;
            }
        }

        Gdiplus::Bitmap * bitmap;
        IStream* pStream = SHCreateMemStreamFunc(data, imageSize);
        if (pStream) {
            bitmap = Gdiplus::Bitmap::FromStream(pStream);
            pStream->Release();
            if (bitmap) {
                if (bitmap->GetLastStatus() == Gdiplus::Ok) {
                    return bitmap;
                }
                delete bitmap;
            }
        }
    } else {
        HGLOBAL buffer = ::GlobalAlloc(GMEM_MOVEABLE, imageSize);
        if (buffer) {
            void* pBuffer = ::GlobalLock(buffer);
            if (pBuffer) {
                Gdiplus::Bitmap * bitmap;
                CopyMemory(pBuffer, data, imageSize);

                IStream* pStream = NULL;
                if (::CreateStreamOnHGlobal(buffer, FALSE, &pStream) == S_OK) {
                    bitmap = Gdiplus::Bitmap::FromStream(pStream);
                    pStream->Release();
                    if (bitmap) {
                        if (bitmap->GetLastStatus() == Gdiplus::Ok) {
                            return bitmap;
                        }

                        delete bitmap;
                    }
                }
                ::GlobalUnlock(buffer);
            }
            ::GlobalFree(buffer);
        }
    }

    return 0;
}

// Based on original method from http://danbystrom.se/2009/01/05/imagegetthumbnailimage-and-beyond/
Gdiplus::Bitmap* GetThumbnail(Gdiplus::Image* bm, int width, int height, Gdiplus::Size* realSize = 0) {
    using namespace Gdiplus;
    if (realSize) {
        realSize->Width = bm->GetWidth();
        realSize->Height = bm->GetHeight();
    }
    Size sz = AdaptProportionalSize(Size(width, height), Size(bm->GetWidth(), bm->GetHeight()));
    Bitmap* res = new Bitmap(sz.Width, sz.Height);
    Graphics gr(res);

    gr.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
    UINT size = bm->GetPropertyItemSize(PropertyTagThumbnailData);
    if (size) {
        // Loading thumbnail from EXIF data (fast)
        enum ThumbCompression { ThumbCompressionJPEG, ThumbCompressionRGB, ThumbCompressionYCbCr, ThumbCompressionUnknown }
            compression = ThumbCompressionJPEG;

        PropertyItem* thumbnailFormatItem = GetPropertyItemFromImage(bm, PropertyTagThumbnailFormat);
        if (thumbnailFormatItem) {
            UINT format = VoidToInt(thumbnailFormatItem->value, thumbnailFormatItem->length);
            if (format == 0) {
                compression = ThumbCompressionRGB;
            } else if (format == 1) {
                compression = ThumbCompressionJPEG;
            } else {
                compression = ThumbCompressionUnknown;
            }
            free(thumbnailFormatItem);
        } else {
            PropertyItem* compressionItem = GetPropertyItemFromImage(bm, PropertyTagThumbnailCompression);
            if (compressionItem) {
                WORD compressionTag = *reinterpret_cast<WORD*>(compressionItem->value);
                if (compressionTag == 1) {
                    compression = ThumbCompressionRGB;
                    PropertyItem* photometricInterpretationItem = GetPropertyItemFromImage(bm, PropertyTagPhotometricInterp);
                    if (photometricInterpretationItem) {
                        WORD photoMetricInterpretationTag = VoidToInt(photometricInterpretationItem->value, photometricInterpretationItem->length);
                        free(photometricInterpretationItem);
                        if (photoMetricInterpretationTag == 6) {
                            compression = ThumbCompressionYCbCr;
                        }
                    }

                } else if (compressionTag == 6) {
                    compression = ThumbCompressionJPEG;
                }

                free(compressionItem);
            }
        }

        int originalThumbWidth = 0, originalThumbHeight = 0;
        if (compression == ThumbCompressionJPEG || compression == ThumbCompressionRGB) {
            PropertyItem* thumbDataItem = GetPropertyItemFromImage(bm, PropertyTagThumbnailData);
            if (thumbDataItem) {
                if (compression == ThumbCompressionJPEG) {
                    Bitmap* src = BitmapFromMemory(reinterpret_cast<BYTE*>(thumbDataItem->value), thumbDataItem->length);

                    if (src) {
                        gr.DrawImage(src, 0, 0, sz.Width, sz.Height);
                        delete src;
                        free(thumbDataItem);
                        return res;
                    }
                } else if (compression == ThumbCompressionRGB) {
                    PropertyItem* widthItem = GetPropertyItemFromImage(bm, PropertyTagThumbnailImageWidth);
                    if (widthItem) {
                        originalThumbWidth = VoidToInt(widthItem->value, widthItem->length);
                        free(widthItem);
                    }
                    PropertyItem* heightItem = GetPropertyItemFromImage(bm, PropertyTagThumbnailImageHeight);
                    if (heightItem) {
                        originalThumbHeight = VoidToInt(heightItem->value, heightItem->length);
                        free(heightItem);
                    }
                    if (originalThumbWidth && originalThumbHeight) {
                        BITMAPINFOHEADER bih;
                        memset(&bih, 0, sizeof(bih));
                        bih.biSize = sizeof(bih);
                        bih.biWidth = originalThumbWidth;
                        bih.biHeight = -originalThumbHeight;
                        bih.biPlanes = 1;
                        bih.biBitCount = 24;

                        BITMAPINFO bi;
                        memset(&bi, 0, sizeof(bi));
                        bi.bmiHeader = bih;

                        BYTE* data = reinterpret_cast<BYTE*>(thumbDataItem->value);
                        BYTE temp;
                        // Convert RGB to BGR
                        for (unsigned int offset = 0; offset < thumbDataItem->length; offset += 3) {
                            temp = data[offset];
                            data[offset] = data[offset + 2];
                            data[offset + 2] = temp;
                        }
                        Bitmap src(&bi, thumbDataItem->value);

                        if (src.GetLastStatus() == Ok) {
                            gr.DrawImage(&src, 0, 0, sz.Width, sz.Height);
                            free(thumbDataItem);
                            return res;
                        }
                    }

                } else {
                    // other type of compression not implemented
                }
                free(thumbDataItem);
            }
        }
    } 
    // Fallback - Load full image and draw it  (slow)
    gr.DrawImage(bm, 0, 0, sz.Width, sz.Height);

    return res;
}

Gdiplus::Bitmap* GetThumbnail(const CString& filename, int width, int height, Gdiplus::Size* realSize) {
    using namespace Gdiplus;
    Image bm(filename);
    if (bm.GetLastStatus() != Ok) {
        return 0;
    }
    return GetThumbnail(&bm, width, height, realSize);
}

Gdiplus::Size AdaptProportionalSize(const Gdiplus::Size& szMax, const Gdiplus::Size& szReal)
{
    int nWidth;
    int nHeight;
    double sMaxRatio;
    double sRealRatio;

    if (szMax.Width < 1 || szMax.Height < 1 || szReal.Width < 1 || szReal.Height < 1)
        return Size();

    sMaxRatio = szMax.Width / static_cast<double>(szMax.Height);
    sRealRatio = szReal.Width / static_cast<double>(szReal.Height);

    if (sMaxRatio < sRealRatio) {
        nWidth = min(szMax.Width, szReal.Width);
        nHeight = static_cast<int>(round(nWidth / sRealRatio));
    } else {
        nHeight = min(szMax.Height, szReal.Height);
        nWidth = static_cast<int>(round(nHeight * sRealRatio));
    }

    return Size(nWidth, nHeight);
}