【问题标题】:What is causing these array values to not match?是什么导致这些数组值不匹配?
【发布时间】:2017-03-27 00:47:45
【问题描述】:

我有一个函数,它从 PNG 文件中获取加载的(原始)值并将它们存储在一个新数组中,这样每个颜色通道都是连续的——即数组的前三分之一应该是红色值,然后是绿色,然后是蓝色。

目前,我让它输出刚刚存储的每个值:

*array_length = (NUMBER_PIXELS * 3); //record array size--one value per pixel per colour channel
unsigned char *loaded_image = malloc(sizeof(char) * *array_length); //allocate array memory
if (loaded_image != NULL) 
{
    for (int i = 0; i < NUMBER_PIXELS; i+=3)
    {
        *(loaded_image + i) = *(load_output + i); //red colour channel. 
        printf("%d ", *(load_output + i));
        *(loaded_image + NUMBER_PIXELS + i) = *(load_output + 1 + i); //green colour channel
        printf("%d ", *(load_output + 1 + i));
        *(loaded_image + (2 * NUMBER_PIXELS) + i) = *(load_output + 2 + i); //blue colour channel
        printf("%d \n", *(load_output + 2 + i));
    }
    free(load_output);
    return loaded_image; //return the array

然后,在另一种方法中,我按顺序打印出数组的全部内容:

int array_length;
unsigned char *image = load_image("TestImage.png", &array_length);
if (image != NULL) {
    for (int i = 0; i < array_length; i++)
    {
        printf("%d \n", *(image + i));
    }
    free(image);
}

输出与输入不匹配:我显然无法判断,数组有 205 出现很多时间(这对于完全红色的测试图像没有意义)并且无序。据我所知,没有类型不匹配或重叠区域,但打印输出看起来 like this.

EDIT:可编译版本,生成一个与图像加载和打印输出相同的输出:

#include <stdio.h>
#include <stdlib.h>

unsigned char
*load_image(char* file_location, int *array_length)
{
    int num_pixels = 64 * 64;
    *array_length = (num_pixels * 3); //record array size--one value per pixel per colour channel
    unsigned char *load_output = malloc(sizeof(char) * *array_length);
    unsigned char *loaded_image = malloc(sizeof(char) * *array_length); //allocate array memory
    for (int i = 0; i < *array_length; i++) {
        if (i % 3 == 0)
            *(load_output + i) = 255;
        else
            *(load_output + i) = 0;
    }
    if (loaded_image != NULL) 
    {
        for (int i = 0; i < num_pixels; i+=3)
        {
            *(loaded_image + i) = *(load_output + i); //red colour channel. 
            printf("%d ", *(load_output + i));
            *(loaded_image + num_pixels + i) = *(load_output + 1 + i); //green colour channel
            printf("%d ", *(load_output + 1 + i));
            *(loaded_image + (2 * num_pixels) + i) = *(load_output + 2 + i); //blue colour channel
            printf("%d \n", *(load_output + 2 + i));
        }
        free(load_output);
        return loaded_image; //return the array
    }
    else 
    {
        return NULL;
    }
}

int main(void)
{
    int array_length;
    unsigned char *image = load_image("TestImage.png", &array_length);
    if (image != NULL) {
        for (int i = 0; i < array_length; i++)
        {
            printf("%d \n", *(image + i));
        }
        free(image);
    }
}

【问题讨论】:

  • 错误出现在您没有向我们展示的代码中。请提供一个最小的、完整的、可编译的示例。
  • 唯一没有包含的代码是读取图像的函数调用,我不确定如何提供一个使用 6,000 行实用程序的最小示例。
  • 如果您在 Visual Studio 中运行调试版本,205 (0xcd) 是一个有趣的数字。这就是您将在新分配但从未更改的内存块中看到的值。
  • 大部分loaded_image 未初始化。你在做i += 3,每次跳过两个元素。
  • 注意:大多数人更喜欢索引符号,因为它更容易阅读:*(loaded_image + i) = *(load_output + i); -->> loaded_image[ i ] = load_output[ i ];

标签: c arrays


【解决方案1】:

在将load_output 转换为loaded_image 的循环中,循环索引处于关闭状态。您需要在每次交互时将 i 递增 1,并将 load_output 递增 3*i

for (int i = 0; i < num_pixels; i++)
        {
            *(loaded_image + i) = *(load_output + 3*i); //red colour channel. 
            printf("%d ", *(load_output + i));
            *(loaded_image + num_pixels + i) = *(load_output + 1 + 3*i); //green colour channel
            printf("%d ", *(load_output + 1 + i));
            *(loaded_image + (2 * num_pixels) + i) = *(load_output + 2 + 3*i); //blue colour channel
            printf("%d \n", *(load_output + 2 + i));
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2011-07-09
    • 2020-07-06
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多