2017-03-26 4 views
1

私は、PNGファイルからロードされた(生の)値を受け取り、それらを新しい配列に格納して、各カラーチャンネルが連続しているようにします。配列の最初の3分の1は赤の値、次に緑の値、青の値です。これらの配列の値が一致しない原因は何ですか?

瞬間について、私は記憶され、それが出力毎に値を有する:そして

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

出力は入力と一致しません。なぜなら、私は明らかに、配列には多くの時間が表示されています(テストイメージが完全に赤色でないことがわかります)。私の知る限り見ることができるように、オーバーラップのないタイプの不一致または領域が存在しない、まだ印刷された出力は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); 
    } 
} 
+0

:あなたはすべてinteration上のいずれかによってiをインクリメントし、load_output3*iずつ増加する必要があります。最小限の、完全な、コンパイル可能な例を提供してください。 – Jens

+0

唯一のコードはイメージを読むための関数呼び出しですが、6,000行のユーティリティを使った最小限の例を提供する方法はわかりません。 – Rain

+0

Visual Studioでデバッグビルドを実行している場合、205(0xcd)は​​興味深い数値です。それは新たに割り当てられたものの、決して変更されていないメモリブロックで見られる価値です。 –

答えて

1

ループインデックスはloaded_imageload_outputを変換ループにオフです。バグはあなたが私たちを示していないコードである

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)); 
     } 
関連する問題