我想使用raylib显示28x28的灰度图像.我找不到有关如何从数组执行此操作的文档.大多数示例展示了如何从预格式化的图像(png、jpeg等)执行此操作.
我看了cheatsheet个,但大多数例子都来自于文件.以下是重现我的问题所需的最少代码.
#include <stdlib.h>
#include <stdio.h>
#include "raylib.h"
typedef struct Sample {
int name;
int *data;
} sample;
void show_sample(sample *smp) {
int scH = 800;
int scW = 450;
InitWindow(scH, scW,"label");
int index = 0;
Color *pixels = (Color*)malloc(28 * 28 * sizeof(Color));
for (int i = 0; i < 28 * 28; i++) {
int c = (int) smp->data[index];
Color grayscale = (Color){c, c, c};
pixels[index] = grayscale;
index++;
}
Image img = {
.data = pixels,
.width = 28,
.height = 28,
.format = PIXELFORMAT_UNCOMPRESSED_R32G32B32,
.mipmaps = 1};
Texture2D texture = LoadTextureFromImage(img);
UnloadImage(img);
SetTargetFPS(60);
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(GRAY);
DrawTexture(texture, scH / 2, scW / 2, GRAY);
DrawText("this IS a texture loaded from an image!", 300, 370, 10, BLACK);
EndDrawing();
}
CloseWindow();
}
int main(int argc, char* argv[]){
int data[]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,3,18,18,18,126,136,175,26,166,255,247,127,0,0,0,0, 0,0,0,0,0,0,0,0,30,36,94,154,170,253,253,253,253,253,225,172,253,242,195,64,0,0,0,0, 0,0,0,0,0,0,0,49,238,253,253,253,253,253,253,253,253,251,93,82,82,56,39,0,0,0,0,0, 0,0,0,0,0,0,0,18,219,253,253,253,253,253,198,182,247,241,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,80,156,107,253,253,205,11,0,43,154,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,14,1,154,253,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,139,253,190,2,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,11,190,253,70,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,35,241,225,160,108,1,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,81,240,253,253,119,25,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,45,186,253,253,150,27,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,93,252,253,187,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,253,249,64,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,46,130,183,253,253,207,2,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,39,148,229,253,253,253,250,182,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,24,114,221,253,253,253,253,201,78,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,23,66,213,253,253,253,253,198,81,2,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,18,171,219,253,253,253,253,195,80,9,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,55,172,226,253,253,253,253,244,133,11,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,136,253,253,253,212,135,132,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
sample smp;
smp.name= 5;
smp.data = data;
show_sample(&smp);
}
文本看起来很好,但图像/纹理现在确实以预期的格式显示.似乎有一些彩色像素,但我想要灰度级.我猜我使用的格式是不正确的. 我知道这可以用Python语言完成,但我不想用那个.存储在SMP中的数组如下所示(显然它是一个整型数组,此处使用逗号来区分这些值):
它可能看起来很眼熟,因为它是MNIST数据库中的5.