如何正确地将指针[]或*(指针+索引)传递给C中的查找表? [英] How do I correctly pass pointer[] or *(pointer+index) to lookup table in C ?

查看:108
本文介绍了如何正确地将指针[]或*(指针+索引)传递给C中的查找表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用查找表在openGL中使用输入图像进行像素计算,这是我的查找表:

I'm trying to use lookup table for pixel computation in openGL with input image, here's my lookup table :

float  pix_scaling [256];
static void scaling(void)
{
    for (int i = 0; i < 256; i++) 
        pix_scaling[i] = (i * 0.5) + 25;  
 }

我在主循环中调用这两个函数,但是我无法得到结果输出,它只是空白的黑色,并且终端没有错误。



我尝试了什么:



我在其他功能中调用我的查找表:



I call both function in my main loop, but I can't get the result output, it's just blank black and somehow no error in terminal.

What I have tried:

I call my lookup table in other function :

unsigned char       * image;
unsigned char       * image1;

static void makerShift(){
unsigned char *p, *p1;
float r, g, b;

    if(image1 == NULL) {
        image1 = (unsigned char *)malloc(width*height*4);
        if(image1 == NULL) exit(0);

p  = image;
p1 = image1;

for(int i = 0; i < height*width; i++) {
    r = pix_scaling[p[0]]; // here
    g = pix_scaling[p[1]];
    b = pix_scaling[p[2]];

    //.. other computation process..//

    //to set maximum and minimum pixel value
      if   (r < 0) *(p1+0) = 0;
      else if(r > 255) *(p1+0) = 255;
      else *(p1+0) = r;
        
      if   (g < 0) *(p1+1) = 0;
      else if(g > 255) *(p1+1) = 255;
      else *(p1+1) = g;
        
      if   (b < 0) *(p1+2) = 0;
      else if(b > 255) *(p1+2) = 255;
      else *(p1+2) = b;

      p +=4;
      p1 +=4;
}

图片来自:

Image comes from this :

image = SOIL_load_image("Images/sample.jpg", &width, &height, 0, SOIL_LOAD_RGBA);



我试过指针


Either I tried with pointer

pix_scaling[p[0]];

或指针和index

pix_scaling[*(p+0)];

两者都不起作用。并且不使用 pix_scaling ,只需

both don't work. And without using pix_scaling , just

r = (*(p+0) * 0.5) + 25;
g = (*(p+1) * 0.5) + 25; 
b = (*(p+2) * 0.5) + 25; 

图像输出正常,所以我猜问题出在我的查找表函数中。我如何正确地做到这一点?

the image output came normally, so I guess the problem is in my lookup table function. How do I correctly do this ?

推荐答案

Rick是绝对正确的,你不会总是访问相同的指针,但你需要更仔细地编写代码并且使用调试器的。不要错过:

Rick is absolutly right that you arent accessing always the same pointers, but you need write code more carefully and use the debugger. Dont miss:
unsigned char       * image = NULL;
unsigned char       * image1 = NULL;



例如,使用不完整的路径加载图像是不合适的。总是检查是否成功,否则在控制台上写一些东西。



我也会定义一些 struct 用于颜色数据,以便您可以更好地使用。当你有这样的结构时,你可以使用C的力量:


For instance it isnt a good to load the image with a incomplete path. Always check for success and else write something on the console.

I also would define some struct for the color data so you can better work with. When you have such structs you can use the power of C:

pData[n];//access to n-th data element
pData[n].r;//access to the r-value of the n-th data element



提示:使用一些非常类似的图片进行调试小十字或圆


tip: debug with some very sample picture like a small cross or circle


引用:

所以我猜这个问题出在我的查找表函数中。我如何正确地执行此操作?

so I guess the problem is in my lookup table function. How do I correctly do this ?

我发现您的缩放表没有任何问题。当然你必须在调用 makeshift 之前初始化它(调用 scaling )。

注意您要将 float 分配给 unsigned char 。我相信你的编辑警告过你。实际上你根本不需要为 pix_scaling 数组使用 float



[更新]

我想你没有展示的代码有问题。

以下程序

I find nothing wrong with your scaling table. Of course you have to initilialize it (calling scaling) before calling makeshift.
Note you are assigning a float to an unsigned char. I believe your compiled warned you about. You actually don't need at all to use floats for the pix_scaling array.

[update]
I suppose there is something wrong in the code you didn't show.
The following program

#include <stdlib.h>
#include <stdio.h>
float  pix_scaling [256];
static void scaling(void)
{
  for (int i = 0; i < 256; i++)  
    pix_scaling[i] = (i * 0.5) + 25;
}

#define H 4
#define W 4

int main()
{
  unsigned char image[H*W*4];
  unsigned char image_scaled[H*W*4];

  for (int y = 0; y < H; ++y)
  {
    for (int x = 0; x < W; ++x)
    {
      for (int c = 0; c < 3; ++c)
      {
        int i = y* W * 4 + x * 4 + c;
        image[i] = (unsigned char)rand();
      }
    }
  }

  scaling();

  unsigned char *p = image;
  unsigned char *p1 = image_scaled;
  float r,g,b;

  for(int i = 0; i < H*W; i++)
  {
    r = pix_scaling[p[0]]; // here
    g = pix_scaling[p[1]];
    b = pix_scaling[p[2]];

    //to set maximum and minimum pixel value
      if   (r < 0) *(p1+0) = 0;
      else if(r > 255) *(p1+0) = 255;
      else *(p1+0) = r;

      if   (g < 0) *(p1+1) = 0;
      else if(g > 255) *(p1+1) = 255;
      else *(p1+1) = g;

      if   (b < 0) *(p1+2) = 0;
      else if(b > 255) *(p1+2) = 255;
      else *(p1+2) = b;

      p +=4;
      p1 +=4;
  }

  for (int y = 0; y < H; ++y)
  {
    for (int x = 0; x < W; ++x)
    {
      int i = y* W * 4 + x * 4 ;
      printf("(%d,%d), image { %d, %d, %d }, image_scaled {%d, %d, %d}\n", x, y, image[i], image[i+1], image[i+2], image_scaled[i], image_scaled[i+1], image_scaled[i+2]);   

    }
  }

  return 0;
}

产生:

produces:

(0,0), image { 103, 198, 105 }, image_scaled {76, 124, 77}
(1,0), image { 115, 81, 255 }, image_scaled {82, 65, 152}
(2,0), image { 74, 236, 41 }, image_scaled {62, 143, 45}
(3,0), image { 205, 186, 171 }, image_scaled {127, 118, 110}
(0,1), image { 242, 251, 227 }, image_scaled {146, 150, 138}
(1,1), image { 70, 124, 194 }, image_scaled {60, 87, 122}
(2,1), image { 84, 248, 27 }, image_scaled {67, 149, 38}
(3,1), image { 232, 231, 141 }, image_scaled {141, 140, 95}
(0,2), image { 118, 90, 46 }, image_scaled {84, 70, 48}
(1,2), image { 99, 51, 159 }, image_scaled {74, 50, 104}
(2,2), image { 201, 154, 102 }, image_scaled {125, 102, 76}
(3,2), image { 50, 13, 183 }, image_scaled {50, 31, 116}
(0,3), image { 49, 88, 163 }, image_scaled {49, 69, 106}
(1,3), image { 90, 37, 93 }, image_scaled {70, 43, 71}
(2,3), image { 5, 23, 88 }, image_scaled {27, 36, 69}
(3,3), image { 233, 94, 212 }, image_scaled {141, 72, 131}



[/ update]

在我看来问题出在这里:
It appears to me the problem is here :
r = pix_scaling[p[0]];
g = pix_scaling[p[1]];
b = pix_scaling[p[2]];

r,g,b将始终具有相同的值,因为p [1]永远不会改变。我认为你必须增加p指向循环每次迭代的下一个像素。

r, g, b will ALWAYS have the same values because p[1] is a never changed. I think you have to increment p to point to the next pixel at every iteration of the loop.


这篇关于如何正确地将指针[]或*(指针+索引)传递给C中的查找表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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