是什么导致这些数组值不匹配?

时间:2017-03-26 15:35:47

标签: c arrays

我有一个函数从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.

编辑:可编译版本,生成的数组输出与图像加载相同且打印输出相同:

#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);
    }
}

1 个答案:

答案 0 :(得分: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));
        }