0

float * 배열에서 이미지를 만들려고합니다. 그러나 MagickConstituteImage는 끊임없이 세분화됩니다. 여기에 관련 코드의 요약이다 (문제) (함수 writeRawImage에서 발생) :MagickConstituteImage() 오류 코드

#include <stdio.h> 
#include <stdlib.h> 
#include <wand/MagickWand.h> 

#define COLOR_MAX 65535.0 

typedef struct { 
     float   **px; 
     unsigned long width; 
     unsigned long height; 
} RawPixels; 

RawPixels getRawImage (char *path) 
{ 
    MagickWand  *mw; 
    MagickBooleanType status; 
    PixelIterator  *iter; 
    MagickPixelPacket pixel; 
    PixelWand   **pixels; 
    RawPixels   rp; 

    long   x; 
    long   y; 
    unsigned long width; 

    unsigned long count; 

    MagickWandGenesis(); 

    mw  = NewMagickWand(); 
    status = MagickReadImage(mw, path); 
    rp.px = NULL; 

    if (status == MagickFalse) { 
     return rp; 
    } 

    iter = NewPixelIterator(mw); 

    if (iter == (PixelIterator *) NULL) { 
     return rp; 
    } 

    rp.width = 0; 
    rp.height = (unsigned long) MagickGetImageHeight(mw); 
    count  = 0; 

    for (y = 0; y < rp.height; y++) { 
     pixels = PixelGetNextIteratorRow(iter, &width); 
     rp.width = (unsigned long) width; 

     if (rp.px == NULL) { 
      rp.px = malloc(sizeof(float *) * (width * rp.height + 1)); 
     } 

     if (pixels == (PixelWand **) NULL) { 
      break; 
     } 

     for (x = 0; x < (long) width; x++) { 
      count++; 
      rp.px[count - 1] = malloc(sizeof(float) * 3); 

      PixelGetMagickColor(pixels[x], &pixel); 

      rp.px[count - 1][0] = pixel.red/COLOR_MAX; 
      rp.px[count - 1][1] = pixel.green/COLOR_MAX; 
      rp.px[count - 1][2] = pixel.blue/COLOR_MAX; 
     } 
    } 

    rp.px[count] = NULL; 

    return rp; 
} 

void freeRawImage (RawPixels rp) 
{ 
    for (int i = 0; rp.px[i] != NULL; i++) { 
     free(rp.px[i]); 
    } 

    free(rp.px); 
} 

void writeRawImage (RawPixels rp, char *path) 
{ 
    // This function is the one that gives me a headache. 
    // Basically, I take float **rp.px, which has the following structure 
    // at this point: 
    // 
    // { 
    //  {float red, float green, float blue}, 
    //  {float red, float green, float blue}, 
    //  ... 
    // } 
    // 
    // Now, the documentation at https://www.imagemagick.org/api/magick-image.php#MagickConstituteImage 
    // says the following: 
    // 
    //  "The pixel data must be in scanline order top-to-bottom" 
    // 
    // So up until the call to MagickConstituteImage() I am trying 
    // to restructure the data and write it into float *scanline 
    // to satisfy that requirement. However, once I call 
    // MagickConstituteImage(), my program segfaults. 


    MagickWand *mw; 
    float   *scanline; 
    unsigned long pxcount; 

    for (pxcount = 0; rp.px[pxcount] != NULL; pxcount++); 
    pxcount *= 3; 
    scanline = malloc(sizeof(float) * pxcount); 

    pxcount = 0; 

    for (int i = 0; rp.px[i] != NULL; i++) { 
     for (int j = 0; j < 3; j++) { 
      scanline[pxcount++] = rp.px[i][j]; 
      printf("%f\n", scanline[pxcount - 1]); 
     } 
    } 

    // This function causes a segfault 
    MagickConstituteImage(mw, rp.width, rp.height, "RGB", FloatPixel, scanline); 

    free(scanline); 
} 

int main() 
{ 
     RawPixels rp; 

     if ((rp = getRawImage("samples/tiny-white.png")).px == NULL) { 
       fprintf(stderr, "ERROR: Failed to process image\n"); 
       return 1; 
     } 


     // Some image processing using raw pixels here 


     writeRawImage(rp, "previews/preview.jpg"); 

     freeRawImage(rp); 

     return 0; 
} 
+0

segfault가 보이면 먼저 디버거에 놓고 그 위치를 파악하십시오. 뜻하지 않게 'NULL'값이 참조 해제되는 것을보십시오. – tadman

+0

답장을 보내 주셔서 감사합니다. tadman, gdb를 사용했으며 segfault가 내 대답에서 발생한 위치를 명시했습니다. MagickWand lib의 잘못된 사용법이었습니다. – afdggfh

답변

0

을 당신은 MagickConstituteImage(...)

에서 사용하기 전에 기능 writeRawImage(...)에서
MagickWand *mw; 

를 초기화하지 않는
+0

빠르고 정확한 대답을 해주셔서 감사합니다. 너는 내 하루를 보냈다! – afdggfh

+0

그리고 궁금한 분은 : 누락 된 줄은 'mw = NewMagickWand()'입니다. – afdggfh