sciapp / gr

GR framework: a graphics library for visualisation applications
Other
329 stars 54 forks source link

how to get the RGBA imge data ,i do this,but get nothing #189

Open cyberdstar opened 1 month ago

cyberdstar commented 1 month ago

double x[] = { 0, 0.2, 0.4, 0.6, 0.8, 1.0 }; double y[] = { 0.3, 0.5, 0.4, 0.2, 0.6, 0.7 }; / Draw something into the memory using GR / // 'RGBA' // gr_openws(0, 0, 100); char filename[64]; sprintf_s(filename, "!%dx%d@%p.mem", width, height, data); // gr_setlinewidth(1); gr_beginprint(filename); gr_polyline(6, x, y); gr_endprint(); std::vector vvc; for (int j = 0; j < height; j++) { unsigned char pdata = &data[width 4 j]; for (uint64_t i = 0; i < (width 4); i = i + 4) { vvc.push_back({pdata[i + 3],pdata[i + 0],pdata[i + 1] ,pdata[i + 2] }); } }

jheinen commented 1 month ago

Could you pls send a complete code snippet, including all declarations?

cyberdstar commented 1 month ago
int width = 300;
int height = 300;
unsigned char* data = (unsigned char*)malloc(height * width * 4);       
memset(data, 0, height * width * 4);

double x[] = { 0, 0.2, 0.4, 0.6, 0.8, 1.0 }; double y[] = { 0.3, 0.5, 0.4, 0.2, 0.6, 0.7 };

/ Draw something into the memory using GR / // 'RGBA'
char filename[64]; sprintf_s(filename, "!%dx%d@%p.mem", width, height, data); // gr_setlinewidth(1); gr_beginprint(filename); gr_polyline(6, x, y);
gr_endprint(); std::vector vvc; for (int j = 0; j < height; j++) { unsigned char pdata = &data[width 4 j]; for (uint64_t i = 0; i < (width 4); i = i + 4) { vvc.push_back({ pdata[i + 3],pdata[i + 0],pdata[i + 1] ,pdata[i + 2] });
} }

winrt::array_view clr(vvc); bitmap_image bi; CanvasBitmap bp = CanvasBitmap::CreateFromColors(sender, clr, width, height); Windows::Foundation::Numerics::float3x2 transform = Windows::Foundation::Numerics::make_float3x2_scale(sender.ActualWidth() / width, sender.ActualHeight() / height); args.DrawingSession().Transform(transform); args.DrawingSession().DrawImage(bp, 0, 0); free(data);

jheinen commented 1 month ago

The following example works as expected. I used this include file, to save the byte array as a (transparent) PNG file.

/*
 cc -I /usr/local/gr/include tmem.c \
 -L/usr/local/gr/lib -lGR -Wl,-rpath,/usr/local/gr/lib
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "gr.h"
#include "svpng.inc"

int main(void)
{
    char filename[64];
    int i, width = 500, height = 500;
    unsigned char *data;
    double x[] = { 0, 0.2, 0.4, 0.6, 0.8, 1.0 };
    double y[] = { 0.3, 0.5, 0.4, 0.2, 0.6, 0.7 };

    data = (unsigned char *) malloc(height * width * 4);
    memset(data, 0, height * width * 4);

    sprintf(filename, "!%dx%d@%p.mem", width, height, data);
    gr_beginprint(filename);
    gr_polyline(6, x, y);
    gr_endprint();

    FILE *fp = fopen("rgba.png", "wb");
    svpng(fp, width, height, data, 1);
    fclose(fp);
}
cyberdstar commented 1 month ago

rgba double x[] = { 0, 0.2, 0.4, 0.6, 0.8, 1.0 }; double y[] = { 0.3, 0.5, 0.4, 0.2, 0.6, 0.7 };

/ Draw something into the memory using GR / // 'RGBA'
char filename[64]; sprintf_s(filename, "!%dx%d@%p.mem", width, height, data); gr_setlinewidth(3); gr_beginprint(filename); gr_polyline(6, x, y);
gr_endprint(); std::vector vvc; for (int j = 0; j < height; j++) { unsigned char pdata = &data[width 4 j]; for (uint64_t i = 0; i < (width 4); i = i + 4) { vvc.push_back({ pdata[i + 3],pdata[i + 0],pdata[i + 1] ,pdata[i + 2] });
} }

FILE* fp = fopen("rgba.png", "wb");//严重性 代码 说明 项目 文件 行 禁止显示状态 详细信息 //错误 C4996 'fopen': This function or variable may be unsafe.Consider using fopen_s instead.To disable deprecation, use _CRT_SECURE_NO_WARNINGS.See online help for details.WaveFrontExperts E : \pwork\ky\WaveFrontExperts\MainWindow.xaml.cpp 157

svpng(fp, width, height, data, 1); fclose(fp);

I do this,but still get a dark png not a curve imge

cyberdstar commented 1 month ago

think very much. i use msvc lib get noting,but with Windows (MinGW),get the correct result

IngoMeyer441 commented 2 weeks ago

So you downloaded a pre-compiled version of GR on Windows?

It would be interesting for us to investigate, why the MSVC version behaves differently.