- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一 block 包含 JPEG 原始字节的内存,我想将其转换为 24 位 RGB,以便我可以使用 StretchDIBits 渲染它。我环顾了 msdn,但是天哪……我真的不想使用任何第三方库,如果不需要的话,甚至不想使用 C++。有什么建议么?我希望只有一些像 DecodeJpeg()
之类的函数......
最佳答案
我想出了一个使用 WIC 的解决方案,它似乎没有使用 C++,但我无法将以下代码编译为 C,所以不确定。花了很多时间将各个部分拼凑在一起,但就是这样!
#include <Wincodec.h>
#include <stdio.h>
#pragma comment(lib, "Ole32.lib")
#pragma comment(lib, "Windowscodecs.lib")
#define MIN(A, B) (A < B ? A : B)
void
Win32DecodeJpeg(unsigned int ImageDataSize, void *ImageData,
unsigned int DestSize, void *Dest)
{
static IWICImagingFactory *IWICFactory;
if(IWICFactory == NULL)
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&IWICFactory));
}
IWICStream *Stream;
IWICFactory->CreateStream(&Stream);
Stream->InitializeFromMemory((unsigned char *)ImageData, ImageDataSize);
IWICBitmapDecoder *BitmapDecoder;
IWICFactory->CreateDecoderFromStream(Stream, NULL, WICDecodeMetadataCacheOnDemand, &BitmapDecoder);
IWICBitmapFrameDecode *FrameDecode;
BitmapDecoder->GetFrame(0, &FrameDecode);
IWICFormatConverter *FormatConverter;
IWICFactory->CreateFormatConverter(&FormatConverter);
FormatConverter->Initialize(FrameDecode,
GUID_WICPixelFormat24bppBGR,
WICBitmapDitherTypeNone,
nullptr,
0.0f,
WICBitmapPaletteTypeCustom);
IWICBitmap *Bitmap;
IWICFactory->CreateBitmapFromSource(FormatConverter, WICBitmapCacheOnDemand, &Bitmap);
unsigned int Width, Height;
Bitmap->GetSize(&Width, &Height);
WICRect Rect = {0, 0, (int)Width, (int)Height};
IWICBitmapLock *Lock;
Bitmap->Lock(&Rect, WICBitmapLockRead, &Lock);
unsigned int PixelDataSize = 0;
unsigned char *PixelData;
Lock->GetDataPointer(&PixelDataSize, &PixelData);
memcpy(Dest, PixelData, MIN(DestSize, PixelDataSize));
Stream->Release();
BitmapDecoder->Release();
FrameDecode->Release();
FormatConverter->Release();
Bitmap->Release();
Lock->Release();
}
int
main()
{
int ImageWidth = 640;
int ImageHeight = 480;
int DecodedBufferSize = ImageWidth * ImageHeight * 3;
void *DecodedBuffer = malloc(DecodedBufferSize);
FILE *ImageFile = fopen("test.jpg", "rb");
fseek(ImageFile, 0, SEEK_END);
int ImageSize = ftell(ImageFile);
rewind(ImageFile);
void *ImageData = malloc(ImageSize);
fread(ImageData, 1, ImageSize, ImageFile);
fclose(ImageFile);
Win32DecodeJpeg(ImageSize, ImageData, DecodedBufferSize, DecodedBuffer);
}
编辑:应大众需求,这里有一个带有基本错误检查和一些基本解释的版本:
#include <Wincodec.h>
#include <stdio.h>
#pragma comment(lib, "Ole32.lib")
#pragma comment(lib, "Windowscodecs.lib")
void
Win32DecodeJpeg(unsigned int ImageDataSize, void *ImageData,
unsigned int DestSize, void *Dest)
{
// IWICImagingFactory is a structure containing the function pointers of the WIC API
static IWICImagingFactory *IWICFactory;
if(IWICFactory == NULL)
{
if(CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK)
{
printf("failed to initialize the COM library\n");
return;
}
if(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&IWICFactory)) != S_OK)
{
printf("failed to create an instance of WIC\n");
return;
}
}
IWICStream *Stream;
if(IWICFactory->CreateStream(&Stream) != S_OK)
{
printf("failed to create stream\n");
return;
}
if(Stream->InitializeFromMemory((unsigned char *)ImageData, ImageDataSize) != S_OK)
{
printf("failed to initialize stream from memory\n");
return;
}
IWICBitmapDecoder *BitmapDecoder;
if(IWICFactory->CreateDecoderFromStream(Stream, NULL, WICDecodeMetadataCacheOnDemand, &BitmapDecoder) != S_OK)
{
printf("failed to create bitmap decoder from stream\n");
return;
}
IWICBitmapFrameDecode *FrameDecode;
// frames apply mostly to GIFs and other animated media. JPEGs just have a single frame.
if(BitmapDecoder->GetFrame(0, &FrameDecode) != S_OK)
{
printf("failed to get 0th frame from frame decoder\n");
return;
}
IWICFormatConverter *FormatConverter;
if(IWICFactory->CreateFormatConverter(&FormatConverter) != S_OK)
{
printf("failed to create format converter\n");
return;
}
// this function does not do any actual decoding
if(FormatConverter->Initialize(FrameDecode,
GUID_WICPixelFormat24bppBGR,
WICBitmapDitherTypeNone,
nullptr,
0.0f,
WICBitmapPaletteTypeCustom) != S_OK)
{
printf("failed to initialize format converter\n");
return;
}
IWICBitmap *Bitmap;
if(IWICFactory->CreateBitmapFromSource(FormatConverter, WICBitmapCacheOnDemand, &Bitmap) != S_OK)
{
printf("failed to create bitmap from format converter\n");
return;
}
unsigned int Width, Height;
if(Bitmap->GetSize(&Width, &Height) != S_OK)
{
printf("failed to get the size of the bitmap\n");
return;
}
WICRect Rect = {0, 0, (int)Width, (int)Height};
IWICBitmapLock *Lock;
// this is the function that does the actual decoding. seems like they defer the decoding until it's actually needed
if(Bitmap->Lock(&Rect, WICBitmapLockRead, &Lock) != S_OK)
{
printf("failed to lock bitmap\n");
return;
}
unsigned int PixelDataSize = 0;
unsigned char *PixelData;
if(Lock->GetDataPointer(&PixelDataSize, &PixelData) != S_OK)
{
printf("failed to get data pointer\n");
return;
}
memcpy(Dest, PixelData, DestSize < PixelDataSize ? DestSize : PixelDataSize);
Stream->Release();
BitmapDecoder->Release();
FrameDecode->Release();
FormatConverter->Release();
Bitmap->Release();
Lock->Release();
}
int
main()
{
int ImageWidth = 640;
int ImageHeight = 480;
int DecodedBufferSize = ImageWidth * ImageHeight * 3;
void *DecodedBuffer = malloc(DecodedBufferSize);
FILE *ImageFile = fopen("test.jpg", "rb");
fseek(ImageFile, 0, SEEK_END);
int ImageSize = ftell(ImageFile);
rewind(ImageFile);
void *ImageData = malloc(ImageSize);
fread(ImageData, 1, ImageSize, ImageFile);
fclose(ImageFile);
Win32DecodeJpeg(ImageSize, ImageData, DecodedBufferSize, DecodedBuffer);
}
关于c++ - 如何使用 Win32 解码 JPEG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45809347/
在 Google 和许多 DICOM 章节中可以轻松找到有关 JPEG-LS 的信息。 但是,也有提到 JPEG-LL 的链接/页面/阅读资料。但是,我更深入地研究了 DICOM 标准,没有一章提到过
我有一个关于 DICOM 标准和 libjpeg 库的问题。在 DICOM 标准中,除其他外,还有传输语法: JPEG Lossless, Nonhierarchical, First- Order
APP0 到 APP15 标记每个只支持 65535 字节(我从 libjpeg.doc 中读到的)。如果要在 jpeg 文件中保存更大的数据 block 怎么办? 最佳答案 没有限制使用多个相同类型
我正在尝试构建一个上传器,它分两步上传渐进式文件: 上传最小字节数以创建缩略图 (0-10%) 上传缩略图的其余字节。 (11%-100%) 我想这样做是为了让缩略图更早可用,而不必上传单独的缩略图。
所以,假设我有一个大版本的图像,它只显示为缩略图。是否可以通过使用渐进式 jpeg 来避免为缩略图创建单独的文件,在达到一定数量的扫描时停止加载,并且仅在用户选择完全打开它时继续加载? 如果是这样,如
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。 Improve this
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 9年前关闭
我想从 jpeg 中提取缩略图,无需任何外部库。我的意思是这并不太困难,因为我需要知道缩略图在文件中的开始位置和结束位置,然后简单地剪切它。我研究了许多文档(即: http://www.media.m
当 decoding entropy encoded DC values in JPEG (或 entropy encoded prediction differences in lossless J
问题是:如何区分两个文件?一个用 JPEG 编码,另一个用 JPEG2000 编码。 我需要特定于格式的文件读/写函数,我无法在不读取的情况下找到文件编码。JPEG 现在工作正常,但 JPEG fun
是否有结束 exif/end-of-xmp/end-of-iptc/start-of-data 标记,我可以用它来获取 jpg/jpeg(和其他图像格式)的数据部分的校验和? 最佳答案 我认为这个问题
不久前我收到一封电子邮件,其中包含图像附件。从那时起,hotmail 似乎已停止为我托管图像,因为当我打开邮件时,图像不再可用。 但是,消息源仍然完好无损,如果我没记错的话,消息源 - 以文本形式 -
我有一个提供高质量 MJPEG 的网络摄像头。 我需要通过网络发送小的、低质量的 JPEG。我的硬件是树莓派(700MHz ARM)。我希望代码使用尽可能少的 CPU 能力,并尽可能少地增加延迟。我可
有一个question with the same title但不幸的是它对我没有帮助。 我正在尝试解析 SOS 标记的数据。我能找到的所有文档都说在标记( 0xFFDA )之后是一个两字节的数字,它
每个人! 我处理来自 IP 摄像机的视频,并编写了基于解压缩视频分析的运动检测算法。但我真的更快。我找到了几篇关于压缩域分析的论文,但没有找到任何实现。 谁能推荐我一些代码? 找到的 Material
我在一个网站上工作,该网站涉及显示来自各种在线零售商的大量产品图片。由于大部分页面权重都在图像中,因此我认为值得研究一下减少文件大小的技术。 图像已经是 JPEG。我知道 PNG 有很多多余的东西,可
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我有一个网络画廊,我在其中显示用户上传的文件大小和分辨率各不相同的图像。目前所有图像都是基线。所以我想知道如果我将它们转换为逐行图像是否真的会产生重大影响。使用渐进式图像的优点和缺点是什么。 最佳答案
我在许多链接和网站上保持头脑,但未能得到答案。我不想问这个,我知道 JPEG 压缩,它只制作压缩图像。甚至 Motion JPEG 也会制作压缩图像 I 帧。我的问题是有什么区别。我正在为需要发送视频
我是一名优秀的程序员,十分优秀!