Witam
Piszę kod który nałoży na obraz binarny(tif) operator dylatacji(rozszerzanie się pikseli) W kodzie użyłem 1 bufora czytającego na wejściu i 3 zapisujące na wyjściu,
kod wygląda tak:
#include <stdio.h>
#include <stdlib.h>
#include <gdal/gdal.h>
#include <gdal/cpl_conv.h>
int main()
{
GDALDatasetH in_dataset, out_dataset;
GDALRasterBandH in_band, out_band;
GDALDriverH driver;
int8_t *buff_1, *buff_2, *buff_3, *rd_buff;
double geo_transform[6] = {0};
int rows, row, cols, col, isnodata, nodata;
const char *wkt;
char *in_file, *out_file;
GDALAllRegister();
in_file = (char*)malloc(101);
out_file = (char*)malloc(101);
printf("Podaj nazwe pliku w warstwie binarnej: ");
scanf("%100s", in_file);
in_dataset = GDALOpen(in_file, GA_ReadOnly);
if (in_dataset == NULL)
{
printf("Problem z otwieraniem pliku wejsciowego!\n");
}
else
{
driver = GDALGetDriverByName("GTiff");
if(driver==NULL)
{
printf("Problem z driverem GeoTiff!\n");
}
else
{
in_band = GDALGetRasterBand(in_dataset, 1);
cols = GDALGetRasterBandXSize(in_band);
rows = GDALGetRasterBandYSize(in_band);
printf("Podaj nazwe pliku wynikowego: ");
scanf("%100s", out_file);
out_dataset = GDALCreate(driver, out_file, cols, rows, 1, GDT_Byte, NULL);
if(!out_dataset)
{
printf("Problem z utworzeniem pliku wyjsciowego!\n");
}
else
{
wkt = GDALGetProjectionRef(in_dataset);
GDALGetGeoTransform(in_dataset, geo_transform);
nodata=GDALGetRasterNoDataValue(in_band, & isnodata);
buff_1 = (int8_t *)CPLMalloc(cols*sizeof(int8_t));
buff_2 = (int8_t *)CPLMalloc(cols*sizeof(int8_t));
buff_3 = (int8_t *)CPLMalloc(cols*sizeof(int8_t));
rd_buff = (int8_t *)CPLMalloc(cols*sizeof(int8_t));
out_band = GDALGetRasterBand(out_dataset, 1);
GDALSetRasterNoDataValue(out_band, nodata);
GDALSetProjection(out_dataset, wkt);
GDALSetGeoTransform(out_dataset, geo_transform);
CPLErr err;
int s=0;
row=1;
while(s<rows)
{
err = GDALRasterIO(in_band, GF_Read, 0, s, cols, 1, rd_buff, cols, 1, GDT_Byte, 0, 0);
if(err>CE_Warning)
{
printf("Blad wczytywania rastra wejsciowego!\n");
break;
}
else
{
if (s==0)
{
for(col=0; col<cols; col++)
{
if (col==0)
{
if (rd_buff[col]==nodata)
{
buff_1[col]=nodata;
buff_1[col+1]=nodata;
buff_2[col]=nodata;
buff_2[col+1]=nodata;
}
else if (buff_1[col]!=nodata)
buff_1[col]=0;
}
else if (col>0 && col<cols)
{
if (rd_buff[col]==nodata)
{
buff_2[col-1]=nodata;
buff_2[col]=nodata;
buff_2[col+1]=nodata;
buff_1[col-1]=nodata;
buff_1[col]=nodata;
buff_1[col+1]=nodata;
}
else if (buff_1[col]!=nodata)
buff_1[col]=0;
}
else
{
if (rd_buff[col]==nodata)
{
buff_1[col-1]=nodata;
buff_1[col]=nodata;
buff_2[col-1]=nodata;
buff_2[col]=nodata;
}
else if (buff_1[col]!=nodata)
buff_1[col]=0;
}
s+=1;
}
}
else if (s>=1 && s<rows)
{
for(col=0; col<cols; col++)
{
if (col==0)
{
if (rd_buff[col]==nodata)
{
buff_1[col]=nodata;
buff_1[col+1]=nodata;
buff_2[col]=nodata;
buff_2[col+1]=nodata;
buff_3[col]=nodata;
buff_3[col+1]=nodata;
}
else if (buff_2[col]!=nodata)
buff_2[col]=0;
}
else if (col>0 && col<cols)
{
if (rd_buff[col]==nodata)
{
buff_1[col-1]=nodata;
buff_1[col]=nodata;
buff_1[col+1]=nodata;
buff_2[col-1]=nodata;
buff_2[col]=nodata;
buff_2[col+1]=nodata;
buff_3[col-1]=nodata;
buff_3[col]=nodata;
buff_3[col+1]=nodata;
}
else if (buff_2[col]!=nodata)
buff_2[col]=0;
}
else
{
if (rd_buff[col]==nodata)
{
buff_1[col-1]=nodata;
buff_1[col]=nodata;
buff_2[col-1]=nodata;
buff_2[col]=nodata;
buff_3[col-1]=nodata;
buff_3[col]=nodata;
}
else if (buff_2[col]!=nodata)
buff_2[col]=0;
}
}
if (s<rows&&s!=rows-2)
s+=1;
row+=1;
}
else
{
for(col=0; col<cols; col++)
{
if (col==0)
{
if (rd_buff[col]==nodata)
{
buff_3[col]=nodata;
buff_3[col+1]=nodata;
buff_2[col]=nodata;
buff_2[col+1]=nodata;
}
else if (buff_3[col]!=nodata)
buff_3[col]=0;
}
else if (col>0 && col<cols)
{
if (rd_buff[col]==nodata)
{
buff_3[col-1]=nodata;
buff_3[col]=nodata;
buff_3[col+1]=nodata;
buff_2[col-1]=nodata;
buff_2[col]=nodata;
buff_2[col+1]=nodata;
}
else if (buff_3[col]!=nodata)
buff_3[col]=0;
}
else
{
if (rd_buff[col]==nodata)
{
buff_3[col-1]=nodata;
buff_3[col]=nodata;
buff_2[col-1]=nodata;
buff_2[col]=nodata;
}
else if (buff_3[col]!=nodata)
buff_3[col]=0;
}
}
}
}
err=GDALRasterIO(out_band, GF_Write, 0, row-1, cols, 1, buff_1, cols, 1, GDT_Byte, 0, 0);
if(err>CE_Warning)
{
printf("Blad wczytywania pierwszego rastra!\n");
break;
}
else
{
err = GDALRasterIO(out_band, GF_Write, 0, row, cols, 1, buff_2, cols, 1, GDT_Byte, 0, 0);
if(err>CE_Warning)
{
printf("Blad wczytywania drugiego rastra!\n");
break;
}
else
{
err = GDALRasterIO(out_band, GF_Write, 0, row, cols, 1, buff_3, cols, 1, GDT_Byte, 0, 0);
if(err>CE_Warning)
{
printf("Blad wczytywania trzeciego rastra!\n");
break;
}
}
}
}
}
}
}
}
GDALClose(in_dataset);
GDALClose(out_dataset);
CPLFree(buff_1);
CPLFree(buff_2);
CPLFree(buff_3);
CPLFree(rd_buff);
free(in_file);
free(out_file);
}
tutaj daje screena z obrazem w czym jest problem, wydaje mi się, że chodzi o to że rd_buff (bufor czytający) zapisuje sobie wartość nodata(biały kolor) i już później jej nie zwalnia dlatego obraz wygląda jak wygląda,
czy wie ktoś może jak temu zaradzić, piksele się poszerzają prawidłowo ale jest problem z kolumnami rastra, jak już przeczyta w danej kolumnie wartość nodata(biały) tak już w tej samej kolumnie w każdym kolejnym wierszu (row) nakłada wartość nodata chociaż na obrazie wejściowym jest 1(czarny).