简体   繁体   English

从float数组在Visual C ++中创建灰度图像

[英]Creating a Grayscale image in Visual C++ from a float array

I have an array of grayscale pixel values (floats as a fraction of 1) that I need to display, and then possibly save. 我有一个灰度像素值数组(浮点数为1的一部分),我需要显示,然后可能保存。 The values just came from computations, so I have no libraries currently installed or anything. 这些值来自计算,所以我目前没有安装任何库或任何东西。 I've been trying to figure out the CImage libraries, but can't make much sense of what I need to do to visualize this data. 我一直在试图找出CImage库,但无法理解我需要做些什么来可视化这些数据。 Any help would be appreciated! 任何帮助,将不胜感激!

Thank you. 谢谢。

One possible approach which I've used with some success is to take D3DX's texture functions to create a Direct3D texture and fill it. 我已经成功使用的一种可能的方法是使用D3DX的纹理函数来创建Direct3D纹理并填充它。 There is some overhead in starting up D3D, but it provides you with multi-thread-able texture creation and built-in-ish viewing, as well as saving to files without much more fuss. 启动D3D有一些开销,但它为您提供了多线程纹理创建和内置查看,以及保存到文件而不用大惊小怪。

If you're not interested in using D3D(X), some of the specifics here won't be useful, but the generator should help figure out how to output data for any other library. 如果您对使用D3D(X)不感兴趣,这里的一些细节将没有用,但生成器应该有助于弄清楚如何为任何其他库输出数据。

For example, assuming an existing D3D9 device pDevice and a noise generator (or other texture data source) pGen : 例如,假设现有的D3D9设备pDevice和噪声发生器(或其他纹理数据源) pGen

IDirect3DTexture9 * pTexture = nullptr;
D3DXCreateTexture(pDevice, 255, 255, 0, 0, D3DFMT_R8G8B8, D3DPOOL_DEFAULT, &pTexture);
D3DXFillTexture(pTexture, &texFill, pGen);
D3DXSaveTexture("texture.png", D3DXIFF_PNG, pTexture, NULL);

The generator function: 发电机功能:

VOID WINAPI texFill(
    D3DXVECTOR4* pOut, 
    CONST D3DXVECTOR2* pTexCoord, 
    CONST D3DXVECTOR2* pTexelSize, 
    LPVOID pData,  
) {
    // For a prefilled array:
    float * pArray = (float *)pData;
    float initial = pArray[(pTexCoord->y*255)+pTexCoord->x];

    // For a generator object:
    Generator * pGen = (Generator*)pData; // passed in as the third param to fill
    float initial = pGen->GetPixel(pTexCoord->x, pTexCoord->y);

    pOut->x = pOut->y = pOut->z = (initial * 255);
    pOut->w = 255; // set alpha to opaque
}

Corresponding functions are available for volume/3D textures. 相应的功能可用于体积/ 3D纹理。 As they are already set up for D3D, you can simply render the texture to a flat quad to view, or use as a source in whatever graphical application you may want. 由于它们已经设置为D3D,您可以简单地将纹理渲染为平面四边形以进行查看,或者在您可能想要的任何图形应用程序中用作源。

So long as your generator is thread-safe, you can run the create/fill/save in one thread per texture, and generate multiple slices or frames simultaneously. 只要您的生成器是线程安全的,您就可以在每个纹理的一个线程中运行create / fill / save,并同时生成多个切片或帧。

I found that the best solution for this problem was to use the SFML library (www.sfml-dev.org). 我发现这个问题的最佳解决方案是使用SFML库(www.sfml-dev.org)。 Very simple to use, but must be compiled from source if you want to use it with VS2010. 使用非常简单,但如果要与VS2010一起使用,必须从源代码编译。

You can use the PNM image format without any libraries whatsoever. 您可以使用PNM图像格式,而无需任何库。 (The format itself is trivial). (格式本身很简单)。 However it's pretty archaic and you'll have to have an image viewer that supports it. 然而,这是相当古老的,你必须有一个支持它的图像查看器。 IvanView, for example, supports it on Windows. 例如,IvanView在Windows上支持它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM