显示色彩的C中的数组 [英] Display an array of color in C

查看:99
本文介绍了显示色彩的C中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序都写了一个读取像这样的颜色数组:

My program both writes an reads arrays of colors like this one :

struct Image {
    size_t width;
    size_t height;
    struct Color *data;
}

struct Color {
    char r;
    char g;
    char b;
}

我如何可以在C在屏幕上显示这样的阵列?

How can I display such an array on the screen in C ?

推荐答案

图形的绘制:

我习惯到Win32和Borland C ++环境下,所以我坚持下去,但在其他环境的差异大多只在类名。首先是一些方法:

I am used to win32 and Borland C++ environments so I stick to it but the differences on other environments are mostly only in Class names. First some approaches:


  1. 控制台/文本模式

您可以用文字图形( ASCII艺术我觉得英文)。其中,重新$ P $通过的字符psented 强度被或多或少填充字符的。通常有按强度分类,比如字符表..:+ *#并使用它的颜色。打印出来的东西可以使用的iostream COUT<< 文本<< ENDL; 的printf STDIO 我觉得(不使用旧式的控制台输出超过十年)。

you can use text graphics (ASCII art I think in English). Where point is represented by character. Intensity is made by more or less filled chars. Usually have a table of characters sorted by intensity like " ..:+*#" and use that instead of colors. For printing out something can use iostream like cout << "text" << endl; or printf from stdio I think (not using old-style console output for more than a decade).

文本模式videoram( VRAM )开始于 0B000:0000 如果你有它,你可以像这样直接访问权限的:

Text modes videoram(VRAM) starts at 0B000:0000 if you have the priviledges for it you can do direct access like this:

char far *scr=(char far*)0x0B0000000;
scr[0]='A'; // print A to left upper corner

在Windows 您可以约忘记的直接进入

VGA GFX模式

在Windows中可以这个也...这里小例子忘了:

on Windows you can forget about this also... Here small example:

    //==============================================================================
    char far* scr;              // VGA screen
    const _sx= 320;             // physical screen size
    const _sy= 200;
    //==============================================================================
    void gfxinit();
    void cls();
    void pnt(int x,int y,char c);
    //==============================================================================
    void gfxinit()
        {
        asm {   mov ax,19               // this swith VGA to 320*200*256 color mode (fits inside single 64KB segment so no funny stuff is needed)
            int 16
            }
        for (int i=0;i<256;i++) asm { // this overwrites 256 color palette with some BW gradients
            mov dx,0x3C8
            mov ax,i
            out dx,al              // overwrite color al = i 
            inc dx
            shr al,2               // al=al>>2
            out dx,al              // r,g,b or b,g,r not sure now 
            out dx,al              // all values are 6bit long therefore the shr al,2 
            out dx,al
            }
        scr=(char far*)0xA0000000;     // VRAM start address
        }
    //==============================================================================
    void cls()   // this clear screen with zero
        {
        asm {   push    es
            mov ax,0xA000
            mov es,ax
            mov di,0x0000
            sub ax,ax
            mov cx,32000
            rep stosw
            pop es
            }
        }
    //==============================================================================
    void pnt(int x,int y,char c) // this draw single point of color c
        {
        unsigned int adr;
        if (x<_sx)
         if (x>=0)
          if (y<_sy)
           if (y>=0)
            {
            y=y*_sx;
            adr=x+y;
            scr[adr]=c;
            }
        }
    //==============================================================================

VESA 访问类似,但你必须处理段交叉和分页。

VESA access is similar but you have to deal with segment crossing and paging.

GDI

画布是Windows可视化组件的图形子。在Borland的是类的TCanvas 名为画布。所有窗口都有它也 PaintBoxes,位图... 。它是在 GDI Windows和你的App之间的接口。它有一个像画笔,刷子,字体的线条,填充或文本文件,文字油墨。

Canvas is graphic subcomponent of visual components on Windows. In borland is the class TCanvas named Canvas. All windows has it also PaintBoxes,Bitmaps,.... It is the GDI interface between Windows and your App. It has subcomponents like Pen,Brush,Font for lines,fills or text paper,texts ink.

Form1->Canvas->Pen->Color=clYellow;
Form1->Canvas->MoveTo(10,10);
Form1->Canvas->LineTo(100,150);

其中, Form1中是我的 VCL 窗口此code绘制黄线。

where Form1 is my VCL window this code draws a yellow line.

GDI 有一个像圆弧,椭圆,像素[] [] ... 看到建立在你的IDE帮助更多的许多功能信息。

GDI has many functions like Arc,Ellipse,Pixels[][],... see build in help of your IDE for more info.

GDI的位图

这是特殊的对象,它是是操作系统图形处理位图( DC 设备上下文)。这使得位图是这样的窗口,并有机会获得 GDI

this is special object it is a bitmap with OS graphic handle (DC device context). This allows bitmap to be something like window and have access to GDI

Graphics::TBitmap *bmp=new Graphics::TBitmap;
bmp->Width=100;
bmp->Height=100;
bmp->HandleType=bmDIB;    // allows use of ScanLine
bmp->PixelFormat=pf32bit; // 32bit the same as int so we can use int* for pixels pointer

这将创建一个 VCL 的位图,并将其设置为 100x100x32bit 直接访问。现在,您可以访问扫描线属性。此外 BMP-方式&gt;帆布是present这样你就可以做所有的 GDI 的东西太多

this creates a VCL bitmap and sets it to 100x100x32bit with direct access. Now you can access ScanLine property. Also bmp->Canvas is present so you can do all GDI stuff too.

int *p=bmp->ScanLine[10]; // p = pointer to y=10 line of bitmap
p[20]=0;                    // draw dot on x=20,y=10   color=0x00000000 which is black
int c = p[15];              // read pixel x=15,y=10 from bitmap to c

小心留在 X,Y 里面的位图或将引发异常。颜色编码取决于像素格式通常 0x00RRGGBB 0x00BBGGRR 。我觉得这个方法是给你,你也可以得出任何的 GDI 对象的最佳选择任何其他 GDI 对象

Be careful to stay with x,y inside bitmap or exception will be thrown. Color coding depends on pixelformat usually is 0x00RRGGBB or 0x00BBGGRR. I think this approach is the best option for you also you can draw any GDI object to any other GDI object

Form1->Canvas->Draw(0,0,bmp);

这吸引您的位图窗口,这样你可以看到它。

this draws your bitmap to window so you can see it actually.

图形库

有很多,但最常用的是的的OpenGL 的DirectX 即可。我preFER 的OpenGL ,因为它更简单的实现(至少对于初学者),也的OpenGL 是跨平台的的DirectX 是仅适用于Windows。此外,当我开始编码没有 DirecX 。当我开始使用的的OpenGL 所有的厂商都有它的驱动程序包括在内。现在唯一的厂商目前仍在最新的的nVidia ATI(AMD)。几乎总是在它们之间,但在一般的的nVidia 是为了更好的的OpenGL (在DirectX的执行错误)和 ATI(AMD版本)是为了更好的的DirectX (在的OpenGL 执行错误)。但对于基本的操作你的罚款(问题被更先进的功能)

There are many but the most used are OpenGL an DirectX. I prefer OpenGL because it is more simple to implement (at least for starters) and also OpenGL is cross-platform an DirectX is windows only. Also when I start coding there was no DirecX. When I started using OpenGL all vendors has it in drivers included. Now the only vendors which are still up to date are nVidia and ATI(AMD). There is almost always some driver issue between them but in general nVidia is better for OpenGL (has bugs in DirectX implementation) and ATI(AMD versions only) is better for DirectX (has bugs in OpenGL implementation). But for basic operations you are fine (problems gets on more advanced functions)

厂商,如英特尔,矽统,......已经停止对新的的OpenGL 版本至少我不知道任何驱动程序的更好,然后的OpenGL 3.3的实现他们

Vendors like Intel,SiS,... has stopped their implementations on newer OpenGL versions at least I do not know of any driver better then OpenGL 3.3 for them

我强烈建议开始与GDI +位图先。你可以与他们无关了很多,我仍然使用它的非复杂的渲染。

由于我的Borland( VCL 风格)前面提到的友好所以如果你用不同的编译器/ IDE,然后更改 GDI 对象名称来对应你的环境。我认为,画布是相同的位图 HBITMAP ,但最好先检查一下你的帮助/文档至少你知道该怎么寻找。

As mentioned before I am borland (VCL style) friendly so if you use different compiler/IDE then change the GDI object names to correspond your environment. I think Canvas is the same and bitmap is HBitmap but better check your help/docs at least you know what to search for.

希望它帮助了一点。

其他平台

  • simple gfx in linux here: X11/Xlib.h example

这篇关于显示色彩的C中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆