Files
neuralnet/nntiff.c
T
jens 82fbd5d19d Initial import
git-svn-id: http://moon:8086/svn/software/trunk/libsrc/nn@1 b431acfa-c32f-4a4a-93f1-934dc6c82436
2014-07-19 07:44:42 +00:00

246 lines
4.2 KiB
C
Executable File

/**********************************************************************
* nntiff.c
*
* (C) 2000 J. Ahrensfeld
*
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "nntypes.h"
#include "nnet.h"
#include "nntiff.h"
#include "tiffio.h"
#define TIFF_FILENAME "test"
#define TIFF_PASS_WIDTH 200
#define TIFF_PASS_HEIGHT 200
#define TIFF_NUM_FRAMES 1
/**********************************************************************/
UINT32 CpyChar2Dbl(double *pDoubleArr, UINT8 *pCharArr, UINT32 nChars)
{
UINT32 i;
for (i=0; i <nChars; i++)
pDoubleArr[i] = (double)pCharArr[i];
return i;
}
UINT32 CpyDbl2Char(UINT8 *pCharArr, double *pDoubleArr, UINT32 nDoubles)
{
UINT32 i;
for (i=0; i <nDoubles; i++)
pCharArr[i] = (UINT8)(fabs(pDoubleArr[i])*0xFF);
return i;
}
double dmax(double v1, double v2)
{
if (v1 > v2)
return v1;
else
return v2;
}
void Normalize(double *pBuffer, double normVal, UINT32 len)
{
UINT32 i;
double maxVal = normVal;
for (i=0; i <len; i++)
maxVal = dmax(maxVal, pBuffer[i]);
for (i=0; i <len; i++)
pBuffer[i] = normVal * pBuffer[i] / maxVal;
}
void SwapRowCol(UINT8 *pBuffer, UINT32 width, UINT32 height)
{
UINT32 row, col;
UINT8 *pNewBuf = (UINT8*)_TIFFmalloc(width*height);
for (row=0; row <height; row++)
{
for (col=0; col <width; col++)
pNewBuf[width*col+row] = pBuffer[col+row*width];
}
_TIFFmemcpy(pBuffer, pNewBuf, width*height);
_TIFFfree(pNewBuf);
}
void ViewAlign(UINT8 **ppBuffer, UINT32 width, UINT32 height)
{
UINT8 temp;
UINT32 col, row;
UINT8 *pTemp = (UINT8*)_TIFFmalloc(width);
for (row=0; row <height; row++)
{
for (col=0; col <width/2; col++)
{
temp = ppBuffer[row][col];
ppBuffer[row][col] = ppBuffer[row][col+width/2];
ppBuffer[row][col+width/2] = temp;
}
}
for (row=0; row <height/2; row++)
{
_TIFFmemcpy(pTemp, ppBuffer[row+height/2], width);
_TIFFmemcpy(ppBuffer[row+height/2], ppBuffer[row], width);
_TIFFmemcpy(ppBuffer[row], pTemp, width);
}
ppBuffer[height/2][width/2] /= 2;
_TIFFfree(pTemp);
}
UINT32 TiffOutInit(TIFFOUT *pObj)
{
pObj->frameCnt=0;
return EXIT_SUCCESS;
}
UINT32 TiffOut(TIFFOUT *pObj, NNET *pNet)
{
TIFF *pTifOut;
ttag_t config, nBits, nSamples, photometric;
UINT8 *pLineBuf;
UINT32 width, height, tx, ty;
INT32 status, ci;
char tiffName[100], framenum[8];
FLOAT64 *pIn, *pOut, xVal, yVal;
pLineBuf = NULL;
pLineBuf = (char*)_TIFFmalloc(TIFF_PASS_WIDTH*sizeof(UINT8));
pIn = (double*) malloc(pNet->nIn*sizeof(double));
pOut = (double*) malloc(TIFF_PASS_HEIGHT*pNet->nOut*sizeof(double));
if (pNet->nIn == 2)
{
photometric = PHOTOMETRIC_MINISBLACK;
width = 200;
height = 200;
config = PLANARCONFIG_CONTIG;
nBits = 8;
nSamples = 1;
sprintf(framenum,"%4d",pObj->frameCnt);
ci=0;
while (framenum[ci])
{
if(framenum[ci]==0x20)
framenum[ci]=0x30;
ci++;
}
sprintf(tiffName,"%s%s.tiff",TIFF_FILENAME,framenum);
pTifOut = TIFFOpen(tiffName,"w");
status = TIFFSetField(pTifOut, TIFFTAG_IMAGEWIDTH, TIFF_PASS_WIDTH);
status = TIFFSetField(pTifOut, TIFFTAG_IMAGELENGTH, TIFF_PASS_HEIGHT);
status = TIFFSetField(pTifOut, TIFFTAG_PLANARCONFIG, config);
status = TIFFSetField(pTifOut, TIFFTAG_PHOTOMETRIC, photometric);
status = TIFFSetField(pTifOut, TIFFTAG_BITSPERSAMPLE, nBits);
status = TIFFSetField(pTifOut, TIFFTAG_SAMPLESPERPIXEL, nSamples);
status = TIFFSetField(pTifOut, TIFFTAG_ORIENTATION, 1);
}
for (ty=0; ty < TIFF_PASS_HEIGHT; ty++)
{
yVal = (FLOAT64)ty / TIFF_PASS_WIDTH;
for (tx=0; tx < TIFF_PASS_WIDTH; tx++)
{
xVal = (FLOAT64)tx / TIFF_PASS_WIDTH;
pIn[0] = xVal;
pIn[1] = yVal;
NetFeedForward(pNet, pIn, &pOut[tx]);
}
Normalize(pOut, 1.0, TIFF_PASS_WIDTH);
CpyDbl2Char(pLineBuf, pOut, TIFF_PASS_WIDTH);
status = TIFFWriteScanline(pTifOut, pLineBuf, ty, 0);
}
printf("Write frame %d\n",pObj->frameCnt++);
TIFFClose(pTifOut);
if (pLineBuf)
_TIFFfree(pLineBuf);
if (pIn)
free(pIn);
if (pOut)
free(pOut);
return EXIT_SUCCESS;
}