2009-06-04 6 views
0

Ich versuche, einen Zeiger rgb übergeben, der mit Memset auf 0 initialisiert und dann durchgeschleift wird, um eine 32-Bit-Integer nur in den Grenzen, die ich mit Höhe und erstellen Breiteneingabe (h und w) sowie Offset von der oberen linken Ecke des 2d-Arrays (x und y). Nach dem Kompilieren scheint mir der Wert von printf des Zeigers unmittelbar nachdem er erzeugt wurde, den richtigen Wert zu geben (in meinem Fall 0xFFFFFF mit und Eingabe von 255 255 255 für rgb), aber nachdem er an die rgb2yuv Funktion übergeben wurde wird auf 0 gesetzt, wenn ich dort drucke.Übergabe eines Zeigers an ein Array in einer anderen Funktion in C

Alle Vorschläge wären genial!

#include <stdio.h> 
#include<stdlib.h> 
#include<unistd.h> 
#include<fcntl.h> 
#include<errno.h> 
#include<string.h> 

/*global definitions*/ 
#define WIDTH 480 
#define HEIGHT 240 

/*global declarations*/ 
int w,h,scrn, bytewrite; 
unsigned char red, green, blue; 
static unsigned long rgb[WIDTH][HEIGHT]; 


/*function declarations*/ 
void colorq(); 
void rgb_rectdraw(int x, int y, int w, int h, unsigned char red , 
    unsigned char green, unsigned char blue, unsigned long *rgb); 
void rgb2yuv(unsigned long *rgb); 


/* 
    Function Name: main 
    Purpose: main function 
*/ 

int main(int argc, char** argv){ 
printf("\n"); 
int x, y;  

/*call colorq to create a 32bit number of RGB*/ 
colorq();  

/*call rgb_rectdraw to draw a rectangle RGB array*/ 
rgb_rectdraw(x, y, w, h, red, green, blue, rgb); 

/*call rgb2yuv to take the RGB array and covert it to a YUV array*/ 
rgb2yuv(rgb);  
return 0; 
} 

/* 
    Function name: color q 
    Purpose: asks user to input colors from 0 to 255 in RGB format 
*/ 

void colorq(){  
printf("Please enter a color for Red Green and Blue from 0 to 255:\n"); 
scanf("%hu", &red); 
scanf("%hu", &green); 
scanf("%hu", &blue); 
printf("\n");  
return; 
}   

/* 
    Function name: rectdraw 
    Purpose: Draws a rectangle array 
*/ 

void rgb_rectdraw(int x, int y, int w, int h,unsigned char red, 
    unsigned char green, unsigned char blue,unsigned long *rgb){ 
unsigned long rgbpixel; 

/* testing only take out when 
    finished debugging why red is always 0 after scanf */ 
red = 255; 
printf("red set to 255 for debugging\n"); 

/*creates a 32-bit number of RGB*/ 
rgbpixel = (red<<16)|(green<<8)|blue; 
printf("%#x\n",rgbpixel); 

/*create array of width w height h*/ 
/*initialize array*/ 

memset (rgb, 0,sizeof(HEIGHT*WIDTH)); 
int i, j, startx, stopx, starty, stopy; 


printf("enter width and height of rectangle in pixels\n"); 
scanf("%d %d", &w, &h); 

printf("enter offset x pixels and y pixels of rectangle:\n"); 
scanf("%d %d", &x, &y); 

startx=x; 
starty=y; 
stopx=x+w; 
stopy=y+w; 

/* creates array of w and h of int rgh */ 
for(i=startx; i <= stopx; i++){ 
    for(j=starty; j <= stopy; j++){ 
     rgb = rgbpixel; 
     } 
    j = 0; 
    } 
printf("original rgb %#x\n",rgb); 
return ; 
} 

/* 
*Function Name: rgb2yuv 
*Purpose: convert the RGB array to a YUV array 
*/ 

void rgb2yuv(unsigned long *rgb){  
int i,j; 
printf("ptrpassed = %#x\n",*rgb); 
for(i=0; i<=WIDTH;i++){ 
    for(j=0; j<=HEIGHT; j++){ 
    } 
} 
printf("\n"); 
return; 
} 
+0

Code Formatierung ist durcheinander –

+0

Wird jemand bitte die Frage neu formatieren? – kbyrd

+0

Es ist so schmerzhaft, diesen Code zu lesen! Könnten Sie es bitte bearbeiten und den Code als Codebeispiel einfügen. Prost. – Artur

Antwort

2

Ich ging durch und im Grunde alle Warzen sortiert, und erklärte warum. Eine Menge davon ist die Tatsache, dass Wenn Ihr Compiler Warnungen ausspuckt, müssen Sie auf sie hören.

/* Changed: Code formatted for my sanity */ 

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <errno.h> 
#include <string.h> 

/*global definitions*/ 
#define WIDTH 480 
#define HEIGHT 240 

/*global declarations*/ 
int w,h,scrn, bytewrite; 
/* Changed: to a short because I don't like the fact that I might be overwriting 
    memory on accident (in colorq). */ 
unsigned short red, green, blue; 
static unsigned long rgb[WIDTH][HEIGHT]; 

/* Changed: Some format strings have been changed to get rid of compiler 
    warnings. */ 

/*function declarations*/ 
void colorq(); 
/* Changed: Specify the second dimension when you're passing an array. */ 
void rgb_rectdraw(int x, int y, int w, int h, unsigned char red, 
        unsigned char green, unsigned char blue, 
        unsigned long rgb[][HEIGHT]); 
/* Changed: always pass an array of arrays. */ 
void rgb2yuv(unsigned long rgb[][HEIGHT]); 


/* 
    Function Name: main 
    Purpose: main function 
*/ 

int main(int argc, char** argv) 
{ 
    printf("\n"); 
    int x, y; 


    /*call colorq to create a 32bit number of RGB*/ 
    colorq(); 


    /* call rgb_rectdraw to draw a rectangle RGB array */ 
    rgb_rectdraw(x, y, w, h, red, green, blue, rgb); 

    /* call rgb2yuv to take the RGB array and covert it to a YUV array */ 
    rgb2yuv(rgb); 

    return 0; 
} 

/* 
    Function name: color q 
    Purpose: asks user to input colors from 0 to 255 in RGB format 
*/ 

void colorq(){ 
    /* Suggestion: restructure this method to just take in all its input 
     locally, then return a rgbpixel. */ 

    printf("Please enter a color for Red Green and Blue from 0 to 255:\n"); 
    scanf("%hu", &red); 
    scanf("%hu", &green); 
    scanf("%hu", &blue); 
    printf("\n"); 

    return; 
} 



/* 
    Function name: rectdraw 
    Purpose: Draws a rectangle array 
*/ 

void rgb_rectdraw(int x, int y, int w, int h,unsigned char red, 
        unsigned char green, unsigned char blue, 
        unsigned long rgb[][HEIGHT]) 
{ 
    unsigned long rgbpixel; 

    /* testing only take out when 
     finished debugging why red is always 0 after scanf */ 
    red = 255; 
    printf("red set to 255 for debugging\n"); 

    /*creates a 32-bit number of RGB*/ 
    /* Changed: Added the extra 0xFF masking because of shortness rather than 
     charness. */ 
    rgbpixel = ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF); 
    printf("%#lx\n",rgbpixel); 

    /*create array of width w height h*/ 
    /*initialize array*/ 

    /* Changed: fill the size of one element times the number of elements */ 
    memset(rgb, 0, sizeof(unsigned long) * HEIGHT * WIDTH); 
    int i, j, startx, stopx, starty, stopy; 


    printf("enter width and height of rectangle in pixels\n"); 
    scanf("%d %d", &w, &h); 

    printf("enter offset x pixels and y pixels of rectangle:\n"); 
    scanf("%d %d", &x, &y); 

    startx=x; 
    starty=y; 
    stopx=x+w; 
    stopy=y+w; 

    /* creates array of w and h of int rgh */ 
    for(i=startx; i <= stopx; i++){ 
     for(j=starty; j <= stopy; j++){ 
      rgb[i][j] = rgbpixel; 
     } 
     j = 0; 
    } 
    printf("original rgb %#lx\n", (long unsigned int) rgb); 
    return ; 
} 

/* 
*Function Name: rgb2yuv 
*Purpose: convert the RGB array to a YUV array 
*/ 

void rgb2yuv(unsigned long rgb[][HEIGHT]){ 

    int i,j; 
    /* Changed: You can't just dereference rgb twice -- you have to use array 
     notation here. */ 
    printf("ptrpassed = %#lx\n", rgb[0][0]); 
    for(i=0; i<=WIDTH; i++){ 
     for(j=0; j<=HEIGHT; j++){ 
     } 
    } 
    printf("\n"); 
    return; 
} 
+0

Danke Paul und allen, die geholfen haben! – bryce

+0

@Paul: Wow, du hast eine Menge Zeit auf deinen Händen. Ich brauche meine Garage gemalt, wenn Sie nach etwas nicht-Code verwandten Arbeit suchen ... – Jamie

0

Sieht aus wie Sie Ihren einzigen Pixelwert in dem Zeiger auf Ihre Daten zu mir zu speichern:

rgb = rgbpixel; 
2

Dies hat tatsächlich eine Reihe von Bugs, aber Ihre erste Frage ist die Zuordnung des Pixelwertes auf das Array:

for(i=startx; i <= stopx; i++){ 
    for(j=starty; j <= stopy; j++){ 
     rgb = rgbpixel; 
     } 
    j = 0; 
    } 

Sie bedeutete wahrscheinlich so etwas wie folgt aus:

rgb[i][j] = rgbpixel; 

Sie müssen j nicht auf 0 zurücksetzen - die innere For-Schleife wird j sofort auf Start zurücksetzen.

Außerdem missbrauchen Sie sizeof(). Wahrscheinlich möchtest du stattdessen sizeof (rgb).

1

Warum nicht eine Struktur verwenden? Wenn der Code nicht die Absicht, tragbar hat könnte man leicht weg mit:

struct rgb_bits { 
    int red : 8; 
    int green : 8; 
    int blue : 8; 
}; 
union rgb { 
    rgb_bits colour; 
    long  array; 
} 

das Pixel dann einfach wird Ausblendung:

union rgb pixel; 
pixel.array = 0; 

und individuelle Farben einstellen wird:

union rgb pixel; 
pixel.colour.red = ...; 
pixel.colour.green = ...; 
pixel.colour.blue = ...; 
Verwandte Themen