Salve a tutti!!
Ho un immagine 320x240 a 24bpp, quello che devo fare è estrarre le componenti RGB e disporre ognuna di queste su un'immagine differente. Ovviamente ogni pixel è formato da 3 byte ognuno dei quali rappresenta una delle 3 componenti (ordinate sempre RGB RGB RGB...)
Quello che in pratica sto cercando di realizzare è un ciclo che prende il file raw e preleva (ad esempio per la componente R) i pixel partendo da zero e spostandosi di 3 in avanti per poi memorizzarli in un buffer...ovviamente non funziona:muro: !! Lascio che sia il codice a parlare :
codice:
int rows,cols,;
unsigned char **img1,**imgR,**imgG,**imgB;
unsigned char** allocabuffer(int r, int c);
void eliminabuffer(unsigned char **buffer,int r);
void readraw(char *filepath,unsigned char **buffer);
void readrawR(char *filepath,unsigned char **buffer);
void readrawG(char *filepath,unsigned char **buffer);
void readrawB(char *filepath,unsigned char **buffer);
void saveraw(char *filepath,unsigned char **buffer);
FILE *out;
main()
{
int i;
rows=240; cols=320;
imgR=allocabuffer(rows, cols);
readrawR("Immagine.raw",imgR);
saveraw("Immagine_R.raw",imgR);
eliminabuffer(imgR,rows);
}
unsigned char** allocabuffer(int r, int c)
{
int i;
unsigned char **buffer=(unsigned char**)malloc(r*sizeof(unsigned char*));
for(i=0;i<r;i++)
buffer[i]=(unsigned char*)calloc(c,sizeof(unsigned char));
return (buffer);
}
// readraw RGB
void readrawR(char *filepath,unsigned char **buffer)
{
int i;
FILE *source=fopen(filepath,"rb");
for(i=0;i<rows*cols*3;i+3) fread(buffer[i],1,1,source);
fclose(source);
}
void readrawG(char *filepath,unsigned char **buffer)
{
int i;
FILE *source=fopen(filepath,"rb");
for(i=1;i<rows;i+3) fread(buffer[i],1,cols,source);
fclose(source);
}
void readrawB(char *filepath,unsigned char **buffer)
{
int i;
FILE *source=fopen(filepath,"rb");
for(i=2;i<rows;i+3) fread(buffer[i],1,cols,source);
fclose(source);
}
void eliminabuffer(unsigned char **buffer,int r)
{
int i;
for(i=0;i<r;i++) free(buffer[i]);
free(buffer);
}
void saveraw(char *filepath,unsigned char **buffer)
{
int i;
FILE *source=fopen(filepath,"wb");
for(i=0;i<rows;i++) fwrite(buffer[i],1,1,source);
fclose(source);
}
Per il momento ho chiamato solo la funzione che estrae la componente R!!:help: