some functions done
This commit is contained in:
parent
b4d2569f46
commit
7b1c1cedb0
|
@ -3,7 +3,7 @@
|
|||
// NLMSvariants.c
|
||||
//
|
||||
// Created by FBRDNLMS on 26.04.18.
|
||||
// Copyright © 2018 FBRDNLMS. All rights reserved.
|
||||
// Copyright © 2018 FBRDNLMS. All rights reserved.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
|
@ -22,10 +22,26 @@
|
|||
#define DIRECT_PREDECESSOR 2
|
||||
#define LOCAL_MEAN 4
|
||||
#define RGB_COLOR 255
|
||||
double x[] = {0};
|
||||
double _x[M] = {0};
|
||||
double w [M][M]={{0},{0}};
|
||||
#if defined(_MSC_VER)
|
||||
#include <BaseTsd.h>
|
||||
#include <codecvt> // std::codecvt_utf8_utf16
|
||||
#include <locale> // std::wstring_convert
|
||||
#include <string> // std::wstring
|
||||
typedef SSIZE_T ssize_t;
|
||||
#endif
|
||||
double x[] = { 0 };
|
||||
double _x[M] = { 0 };
|
||||
double w[M][M] = { { 0 },{ 0 } };
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
/* UTF-8 to UTF-16 win Format*/
|
||||
auto wstring_from_utf8(char const* const utf8_string)
|
||||
-> std::wstring
|
||||
{
|
||||
std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > converter;
|
||||
return converter.from_bytes(utf8_string);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* *svg graph building* */
|
||||
typedef struct {
|
||||
|
@ -35,58 +51,58 @@ typedef struct {
|
|||
|
||||
point_t points[M]; // [0]=xActual, [1]=xPredicted from directPredecessor, [2]=xPredicted from localMean
|
||||
|
||||
/* *ppm reader/writer* */
|
||||
/* *ppm reader/writer* */
|
||||
typedef struct {
|
||||
unsigned char red, green, blue;
|
||||
}colorChannel_t;
|
||||
|
||||
typedef struct {
|
||||
int x,y;
|
||||
int x, y;
|
||||
colorChannel_t *data;
|
||||
}imagePixel_t;
|
||||
|
||||
static imagePixel_t * readPPM( char *fileName );
|
||||
void mkPpmFile( char *fileNamem, imagePixel_t *image );
|
||||
int * ppmColorChannel( imagePixel_t *image );
|
||||
static imagePixel_t * readPPM(char *fileName);
|
||||
void mkPpmFile(char *fileNamem, imagePixel_t *image);
|
||||
int * ppmColorChannel(imagePixel_t *image);
|
||||
|
||||
/* *file handling* */
|
||||
char * mkFileName( char* buffer, size_t max_len, int suffixId );
|
||||
char *fileSuffix( int id );
|
||||
void myLogger( FILE* fp, point_t points[]);
|
||||
char * mkFileName(char* buffer, size_t max_len, int suffixId);
|
||||
char *fileSuffix(int id);
|
||||
void myLogger(FILE* fp, point_t points[]);
|
||||
#ifdef _WIN32
|
||||
size_t getline( char **lineptr, size_t *n, FILE *stream );
|
||||
size_t getline(char **lineptr, size_t *n, FILE *stream);
|
||||
#endif
|
||||
void mkSvgGraph( point_t points[]);
|
||||
void mkSvgGraph(point_t points[]);
|
||||
|
||||
/* *rand seed* */
|
||||
double r2( void );
|
||||
double rndm( void );
|
||||
double r2(void);
|
||||
double rndm(void);
|
||||
|
||||
/* *math* */
|
||||
double sum_array( double x[], int length );
|
||||
void directPredecessor( void );
|
||||
void localMean( void );
|
||||
double sum_array(double x[], int length);
|
||||
void directPredecessor(void);
|
||||
void localMean(void);
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv ) {
|
||||
int main(int argc, char **argv) {
|
||||
char fileName[50];
|
||||
int i;
|
||||
|
||||
srand( (unsigned int) time(NULL) );
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
for (i = 0; i < M; i++) {
|
||||
_x[i] += ((255.0 / M) * i); // Init test values
|
||||
for (int k = 0; k < M; k++){
|
||||
for (int k = 0; k < M; k++) {
|
||||
w[k][i] = rndm(); // Init weights
|
||||
}
|
||||
}
|
||||
|
||||
mkFileName( fileName, sizeof(fileName), PURE_WEIGHTS);
|
||||
mkFileName(fileName, sizeof(fileName), PURE_WEIGHTS);
|
||||
// save plain test_array before math magic happens
|
||||
FILE *fp0 = fopen(fileName,"w");
|
||||
for (i = 0; i <= tracking; i++){
|
||||
for ( int k = 0; k < tracking; k++ ){
|
||||
FILE *fp0 = fopen(fileName, "w");
|
||||
for (i = 0; i <= tracking; i++) {
|
||||
for (int k = 0; k < tracking; k++) {
|
||||
fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]);
|
||||
}
|
||||
}
|
||||
|
@ -99,11 +115,11 @@ int main(int argc, char **argv ) {
|
|||
|
||||
// save test_array after math magic happened
|
||||
// memset( fileName, '\0', sizeof(fileName) );
|
||||
mkFileName( fileName, sizeof(fileName), USED_WEIGHTS);
|
||||
FILE *fp1 = fopen(fileName,"w");
|
||||
mkFileName(fileName, sizeof(fileName), USED_WEIGHTS);
|
||||
FILE *fp1 = fopen(fileName, "w");
|
||||
for (i = 0; i < tracking; i++) {
|
||||
for (int k = 0; k < tracking; k++) {
|
||||
fprintf(fp1, "[%d][%d] %lf\n", k,i, w[k][i]);
|
||||
fprintf(fp1, "[%d][%d] %lf\n", k, i, w[k][i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -116,51 +132,51 @@ int main(int argc, char **argv ) {
|
|||
|
||||
|
||||
/*
|
||||
=======================================================================================
|
||||
=======================================================================================
|
||||
|
||||
localMean
|
||||
localMean
|
||||
|
||||
|
||||
Variant (1/3), substract local mean.
|
||||
Variant (1/3), substract local mean.
|
||||
|
||||
=======================================================================================
|
||||
=======================================================================================
|
||||
*/
|
||||
|
||||
void localMean( void ) {
|
||||
void localMean(void) {
|
||||
char fileName[50];
|
||||
double xError[M]; // includes e(n)
|
||||
memset(xError, 0, M);// initialize xError-array with Zero
|
||||
int xCount = 0; // runtime var
|
||||
int i;
|
||||
mkFileName(fileName, sizeof(fileName), LOCAL_MEAN );
|
||||
mkFileName(fileName, sizeof(fileName), LOCAL_MEAN);
|
||||
FILE* fp4 = fopen(fileName, "w");
|
||||
fprintf(fp4, "\n\n\n\n*********************LocalMean*********************\n");
|
||||
|
||||
for (xCount = 1; xCount < M; xCount++){
|
||||
for (xCount = 1; xCount < M; xCount++) {
|
||||
|
||||
//double xPartArray[xCount]; //includes all values at the size of runtime var
|
||||
|
||||
double xMean = ( xCount > 0 ) ? ( sum_array(_x, xCount) / xCount ) : 0;// xCount can not be zero
|
||||
double xMean = (xCount > 0) ? (sum_array(_x, xCount) / xCount) : 0;// xCount can not be zero
|
||||
|
||||
double xPredicted = 0.0;
|
||||
double xActual = _x[xCount + 1];
|
||||
|
||||
for ( i = 1; i < xCount; i++ ){ //get predicted value
|
||||
xPredicted += ( w[i][xCount] * ( _x[xCount - i] - xMean )) ;
|
||||
for (i = 1; i < xCount; i++) { //get predicted value
|
||||
xPredicted += (w[i][xCount] * (_x[xCount - i] - xMean));
|
||||
}
|
||||
|
||||
xPredicted += xMean;
|
||||
xError [xCount] = xActual - xPredicted;
|
||||
xError[xCount] = xActual - xPredicted;
|
||||
points[xCount].xVal[2] = xCount;
|
||||
points[xCount].yVal[2] = xPredicted;
|
||||
double xSquared = 0.0;
|
||||
|
||||
for ( i = 1; i < xCount; i++ ){ //get x squared
|
||||
xSquared =+ pow(_x[xCount-i],2);
|
||||
for (i = 1; i < xCount; i++) { //get x squared
|
||||
xSquared = +pow(_x[xCount - i], 2);
|
||||
}
|
||||
|
||||
for ( i - 1; i < xCount; i++ ){ //update weights
|
||||
w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * (_x[xCount - i] / xSquared);
|
||||
for (i - 1; i < xCount; i++) { //update weights
|
||||
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * (_x[xCount - i] / xSquared);
|
||||
}
|
||||
|
||||
fprintf(fp4, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
|
||||
|
@ -170,14 +186,14 @@ void localMean( void ) {
|
|||
double deviation = 0.0;
|
||||
|
||||
// Mean square
|
||||
for( i = 0; i < M - 1; i++ ){
|
||||
deviation += pow( xError[i], 2 );
|
||||
for (i = 0; i < M - 1; i++) {
|
||||
deviation += pow(xError[i], 2);
|
||||
}
|
||||
deviation /= xErrorLength;
|
||||
|
||||
|
||||
// write in file
|
||||
mkFileName( fileName, sizeof(fileName), RESULTS );
|
||||
mkFileName(fileName, sizeof(fileName), RESULTS);
|
||||
FILE *fp2 = fopen(fileName, "w");
|
||||
fprintf(fp2, "quadr. Varianz(x_error): {%f}\nMittelwert:(x_error): {%f}\n\n", deviation, mean);
|
||||
fclose(fp2);
|
||||
|
@ -186,36 +202,36 @@ void localMean( void ) {
|
|||
|
||||
|
||||
/*
|
||||
===================================
|
||||
===================================
|
||||
|
||||
directPredecessor
|
||||
directPredecessor
|
||||
|
||||
|
||||
Variant (2/3),
|
||||
substract direct predecessor
|
||||
Variant (2/3),
|
||||
substract direct predecessor
|
||||
|
||||
===================================
|
||||
===================================
|
||||
*/
|
||||
|
||||
void directPredecessor( void ) {
|
||||
char fileName[50];
|
||||
double xError [M];
|
||||
void directPredecessor(void) {
|
||||
char fileName[512];
|
||||
double xError[2048];
|
||||
int xCount = 0, i;
|
||||
double xActual;
|
||||
|
||||
// File handling
|
||||
mkFileName( fileName, sizeof(fileName), DIRECT_PREDECESSOR);
|
||||
mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR);
|
||||
FILE *fp3 = fopen(fileName, "w");
|
||||
fprintf(fp3, "\n\n\n\n*********************DirectPredecessor*********************\n");
|
||||
|
||||
for ( xCount = 1; xCount < M+1; xCount++ ){
|
||||
xActual = _x[xCount+1];
|
||||
for (xCount = 1; xCount < M + 1; xCount++) {
|
||||
xActual = _x[xCount + 1];
|
||||
double xPredicted = 0.0;
|
||||
|
||||
for ( i = 1; i < xCount; i++ ){
|
||||
xPredicted += ( w[i][xCount] * ( _x[xCount - i] - _x[xCount - i - 1] ));
|
||||
for (i = 1; i < xCount; i++) {
|
||||
xPredicted += (w[i][xCount] * (_x[xCount - i] - _x[xCount - i - 1]));
|
||||
}
|
||||
xPredicted += _x[xCount-1];
|
||||
xPredicted += _x[xCount - 1];
|
||||
xError[xCount] = xActual - xPredicted;
|
||||
|
||||
fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
|
||||
|
@ -226,11 +242,11 @@ void directPredecessor( void ) {
|
|||
|
||||
double xSquared = 0.0;
|
||||
|
||||
for ( i = 1; i < xCount; i++ ){
|
||||
xSquared += pow( _x[xCount - i] - _x[xCount - i - 1], 2); // substract direct predecessor
|
||||
for (i = 1; i < xCount; i++) {
|
||||
xSquared += pow(_x[xCount - i] - _x[xCount - i - 1], 2); // substract direct predecessor
|
||||
}
|
||||
for ( i = 1; i < xCount; i++){
|
||||
w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * ( ( _x[xCount - i] - _x[xCount - i - 1] ) / xSquared ); //TODO: double val out of bounds
|
||||
for (i = 1; i < xCount; i++) {
|
||||
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((_x[xCount - i] - _x[xCount - i - 1]) / xSquared); //TODO: double val out of bounds
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -238,12 +254,12 @@ void directPredecessor( void ) {
|
|||
double mean = sum_array(xError, xErrorLength) / xErrorLength;
|
||||
double deviation = 0.0;
|
||||
|
||||
for ( i = 0; i < xErrorLength -1; i++ ){
|
||||
deviation += pow( xError[i] - mean, 2);
|
||||
for (i = 0; i < xErrorLength - 1; i++) {
|
||||
deviation += pow(xError[i] - mean, 2);
|
||||
}
|
||||
|
||||
|
||||
mkSvgGraph( points);
|
||||
mkSvgGraph(points);
|
||||
fprintf(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean);
|
||||
fclose(fp3);
|
||||
}
|
||||
|
@ -252,108 +268,128 @@ void directPredecessor( void ) {
|
|||
|
||||
|
||||
/*
|
||||
=========================================================================
|
||||
=========================================================================
|
||||
|
||||
mkFileName
|
||||
mkFileName
|
||||
|
||||
|
||||
Writes the current date plus the suffix with index suffixId
|
||||
into the given buffer. If[M ?K the total length is longer than max_len,
|
||||
only max_len characters will be written.
|
||||
Writes the current date plus the suffix with index suffixId
|
||||
into the given buffer. If[M ?K the total length is longer than max_len,
|
||||
only max_len characters will be written.
|
||||
|
||||
=========================================================================
|
||||
=========================================================================
|
||||
*/
|
||||
|
||||
char *mkFileName( char* buffer, size_t max_len, int suffixId) {
|
||||
char *mkFileName(char* buffer, size_t max_len, int suffixId) {
|
||||
const char * format_str = "%Y-%m-%d_%H_%M_%S";
|
||||
size_t date_len;
|
||||
const char * suffix = fileSuffix( suffixId );
|
||||
const char * suffix = fileSuffix(suffixId);
|
||||
time_t now = time(NULL);
|
||||
|
||||
strftime( buffer, max_len, format_str, localtime(&now) );
|
||||
strftime(buffer, max_len, format_str, localtime(&now));
|
||||
date_len = strlen(buffer);
|
||||
strncat( buffer, suffix, max_len - date_len );
|
||||
strncat(buffer, suffix, max_len - date_len);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
=========================================================================
|
||||
=========================================================================
|
||||
|
||||
fileSuffix
|
||||
fileSuffix
|
||||
|
||||
Contains and returns every suffix for all existing filenames
|
||||
Contains and returns every suffix for all existing filenames
|
||||
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
char * fileSuffix( int id ) {
|
||||
char * suffix[] = {"_weights_pure.txt", "_weights_used.txt", "_direct_predecessor.txt", "_ergebnisse.txt", "_localMean.txt"};
|
||||
char * fileSuffix(int id) {
|
||||
char * suffix[] = { "_weights_pure.txt", "_weights_used.txt", "_direct_predecessor.txt", "_ergebnisse.txt", "_localMean.txt" };
|
||||
return suffix[id];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
|
||||
svgGraph
|
||||
svgGraph
|
||||
|
||||
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
*/
|
||||
/*
|
||||
void Graph ( ) {
|
||||
char fileName[50];
|
||||
mkFileName(fileName, sizeof(fileName), GRAPH);
|
||||
FILE* fp4 = fopen(fileName, "w");
|
||||
pfrintf
|
||||
char fileName[50];
|
||||
mkFileName(fileName, sizeof(fileName), GRAPH);
|
||||
FILE* fp4 = fopen(fileName, "w");
|
||||
pfrintf
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
|
||||
myLogger
|
||||
myLogger
|
||||
|
||||
|
||||
Logs on filepointer, used for svg graphing
|
||||
Logs on filepointer, used for svg graphing
|
||||
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
void myLogger ( FILE* fp, point_t points[] ){
|
||||
==========================================================================
|
||||
*/
|
||||
/*
|
||||
void myLogger(FILE* fp, point_t points[]) {
|
||||
int i;
|
||||
for( i = 0; i <= M; i++ ){ // xActual
|
||||
fprintf( fp, "L %f %f\n", points[i].xVal[0], points[i].yVal[0]);
|
||||
for (i = 0; i <= M; i++) { // xActual
|
||||
fprintf(fp, "L %f %f\n", points[i].xVal[0], points[i].yVal[0]);
|
||||
}
|
||||
fprintf(fp, "\" fill=\"none\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
|
||||
for( i = 0; i < M-1; i++ ) { // xPred from directPredecessor
|
||||
fprintf( fp, "L %f %f\n", points[i].xVal[1], points[i].yVal[1]);
|
||||
for (i = 0; i < M - 1; i++) { // xPred from directPredecessor
|
||||
fprintf(fp, "L %f %f\n", points[i].xVal[1], points[i].yVal[1]);
|
||||
}
|
||||
fprintf(fp, "\" fill=\"none\" stroke=\"green\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
|
||||
for( i = 0; i <= M; i++ ) { //xPred from lastMean
|
||||
for (i = 0; i <= M; i++) { //xPred from lastMean
|
||||
fprintf(fp, "L %f %f\n", points[i].xVal[2], points[i].yVal[2]);
|
||||
}
|
||||
}
|
||||
*/
|
||||
void bufferLogger(char *buffer, point_t points[]) {
|
||||
int i;
|
||||
char _buffer[512] = "";
|
||||
|
||||
for (i = 0; i <= M; i++) { // xActual
|
||||
sprintf(_buffer, "L %f %f\n", points[i].xVal[0], points[i].yVal[0]);
|
||||
strcat(buffer, _buffer);
|
||||
}
|
||||
strcat(buffer, "\" fill=\"none\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
|
||||
for (i = 0; i < M - 1; i++) { // xPred from directPredecessor
|
||||
sprintf(_buffer, "L %f %f\n", points[i].xVal[1], points[i].yVal[1]);
|
||||
strcat(buffer, _buffer);
|
||||
}
|
||||
strcat(buffer, "\" fill=\"none\" stroke=\"green\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
|
||||
for (i = 0; i <= M; i++) { //xPred from lastMean
|
||||
sprintf(_buffer, "L %f %f\n", points[i].xVal[2], points[i].yVal[2]);
|
||||
strcat(buffer, _buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
=========================================================================
|
||||
=========================================================================
|
||||
|
||||
sum_array
|
||||
sum_array
|
||||
|
||||
|
||||
Sum of all elements in x within a defined length
|
||||
Sum of all elements in x within a defined length
|
||||
|
||||
=========================================================================
|
||||
*/
|
||||
=========================================================================
|
||||
*/
|
||||
|
||||
double sum_array( double x[], int length ){
|
||||
double sum_array(double x[], int length) {
|
||||
int i = 0;
|
||||
double sum = 0.0;
|
||||
|
||||
for( i=0; i< length; i++ ) {
|
||||
for (i = 0; i< length; i++) {
|
||||
sum += x[i];
|
||||
}
|
||||
return sum;
|
||||
|
@ -361,47 +397,47 @@ double sum_array( double x[], int length ){
|
|||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
|
||||
r2
|
||||
r2
|
||||
|
||||
returns a random double value between 0 and 1
|
||||
returns a random double value between 0 and 1
|
||||
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
double r2( void ) {
|
||||
double r2(void) {
|
||||
return((rand() % 10000) / 10000.0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
|
||||
rndm
|
||||
rndm
|
||||
|
||||
fills a double variable with random value and returns it
|
||||
fills a double variable with random value and returns it
|
||||
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
double rndm( void ) {
|
||||
double rndmval= r2();
|
||||
double rndm(void) {
|
||||
double rndmval = r2();
|
||||
return rndmval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
|
||||
getline
|
||||
getline
|
||||
|
||||
This code is public domain -- Will Hartung 4/9/09
|
||||
Microsoft Windows is not POSIX conform and does not support getline.
|
||||
This code is public domain -- Will Hartung 4/9/09 //edited by Kevin Becker
|
||||
Microsoft Windows is not POSIX conform and does not support getline.
|
||||
|
||||
=========================================================================
|
||||
=========================================================================
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
size_t getline(char **lineptr, size_t *n, FILE *stream) {
|
||||
|
@ -427,17 +463,19 @@ size_t getline(char **lineptr, size_t *n, FILE *stream) {
|
|||
return -1;
|
||||
}
|
||||
if (bufptr == NULL) {
|
||||
bufptr = malloc(128);
|
||||
char c[128];
|
||||
memset(c, 0, sizeof(c));
|
||||
bufptr = c;
|
||||
if (bufptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
size = 128;
|
||||
}
|
||||
p = bufptr;
|
||||
while(c != EOF) {
|
||||
while (c != EOF) {
|
||||
if ((p - bufptr) > (size - 1)) {
|
||||
size = size + 128;
|
||||
bufptr = realloc(bufptr, size);
|
||||
realloc(bufptr, size);
|
||||
if (bufptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
@ -459,103 +497,124 @@ size_t getline(char **lineptr, size_t *n, FILE *stream) {
|
|||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
|
||||
mkSvgGraph
|
||||
mkSvgGraph
|
||||
|
||||
parses template.svg and writes results in said template
|
||||
parses template.svg and writes results in said template
|
||||
|
||||
==========================================================================
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
void mkSvgGraph( point_t points[] ) {
|
||||
FILE *input = fopen ( "template.svg", "r" );
|
||||
FILE *target = fopen ( "output.svg", "w" );
|
||||
char *line = NULL;
|
||||
void mkSvgGraph(point_t points[]) {
|
||||
FILE *input = fopen("template.svg", "r");
|
||||
FILE *target = fopen("output.svg", "w");
|
||||
char line[512];
|
||||
// char *ptr;
|
||||
size_t len = 0;
|
||||
ssize_t read;
|
||||
char values[64];
|
||||
char firstGraph[15] = {"<path d=\"M0 0"}; /* There is a space behind the last zero,
|
||||
but windows does not recognize it in strtsr() ?!
|
||||
so no output will return*/
|
||||
//size_t len = 0;
|
||||
//ssize_t read;
|
||||
//char values[64];
|
||||
char firstGraph[15] = { "<path d=\"M0 0" };
|
||||
|
||||
if( input == NULL ) {
|
||||
if (input == NULL) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while ( ( read = getline(&line, &len, input) ) != -1) {
|
||||
//printf("Retrieved line of length %zu :\n", read);
|
||||
//puts(line); // debug purpose
|
||||
fprintf(target, "%s",line);
|
||||
if( strstr(line, firstGraph) != NULL ) {
|
||||
//fprintf(target,"HECK!!!\n");
|
||||
myLogger( target, points );
|
||||
}
|
||||
char buffer[131072] = "";
|
||||
|
||||
memset(buffer, '\0', sizeof(buffer));
|
||||
//int length = 0;
|
||||
while(!feof(input)) {
|
||||
fgets(line, 512, input);
|
||||
strncat(buffer, line,strlen(line));
|
||||
printf("%s\n", line);
|
||||
if (strstr(line, firstGraph) != NULL) {
|
||||
bufferLogger(buffer, points);
|
||||
}
|
||||
|
||||
free(line);
|
||||
// free(target);
|
||||
// free(input);
|
||||
}
|
||||
fprintf(target, buffer);
|
||||
//int c = 0;
|
||||
puts(buffer);
|
||||
//getchar();
|
||||
|
||||
//if (strstr(line, firstGraph) != NULL) {
|
||||
// //fprintf(target,"HECK!!!\n");
|
||||
// bufferLogger(strstr(line, firstGraph),sizeof(firstGraph), points);
|
||||
//}
|
||||
|
||||
/*while ((read = getline(&line, &len, input)) != -1) {
|
||||
//printf("Retrieved line of length %zu :\n", read);
|
||||
//puts(line); // debug purpose
|
||||
fprintf(target, "%s", line);
|
||||
if (strstr(line, firstGraph) != NULL) {
|
||||
fprintf(target,"HECK!!!\n");
|
||||
myLogger(target, points);
|
||||
}
|
||||
}*/
|
||||
|
||||
/*free(line);
|
||||
free(target);
|
||||
free(input);*/
|
||||
//exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
===========================================================================
|
||||
===========================================================================
|
||||
|
||||
rdPPM
|
||||
rdPPM
|
||||
|
||||
reads data from file of type PPM, stores colorchannels in a struct in the
|
||||
size of given picture
|
||||
reads data from file of type PPM, stores colorchannels in a struct in the
|
||||
size of given picture
|
||||
|
||||
==========================================================================
|
||||
*/
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
static imagePixel_t *rdPPM ( const char *fileName ) {
|
||||
char buffer [16];
|
||||
static imagePixel_t *rdPPM(const char *fileName) {
|
||||
char buffer[16];
|
||||
imagePixel_t *image;
|
||||
int c, rgbColor;
|
||||
|
||||
FILE *fp = fopen ( fileName, "rb" );
|
||||
if ( !fp ) {
|
||||
exit (EXIT_FAILURE);
|
||||
FILE *fp = fopen(fileName, "rb");
|
||||
if (!fp) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if ( !fgets (buffer, sizeof(buffer), fp) ) {
|
||||
if (!fgets(buffer, sizeof(buffer), fp)) {
|
||||
perror(fileName);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if ( buffer[0] != 'P' || buffer[1] != '6' ) {
|
||||
fprintf(stderr,"No PPM file format\n");
|
||||
if (buffer[0] != 'P' || buffer[1] != '6') {
|
||||
fprintf(stderr, "No PPM file format\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
image = (imagePixel_t *) malloc (sizeof(imagePixel_t));
|
||||
if (!image ) {
|
||||
image = (imagePixel_t *)malloc(sizeof(imagePixel_t));
|
||||
if (!image) {
|
||||
fprintf(stderr, "malloc() failed");
|
||||
}
|
||||
c = getc(fp);
|
||||
while ( c == '#' ) {
|
||||
while ( getc(fp) != '\n' );
|
||||
while (c == '#') {
|
||||
while (getc(fp) != '\n');
|
||||
c = getc(fp);
|
||||
}
|
||||
ungetc(c, fp);
|
||||
if ( fscanf(fp, "%d %d", &image->x, &image->y) !=2 ) {
|
||||
if (fscanf(fp, "%d %d", &image->x, &image->y) != 2) {
|
||||
fprintf(stderr, "Invalid image size in %s\n", fileName);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if ( fscanf(fp, "%d", &rgbColor) != 1 ) {
|
||||
if (fscanf(fp, "%d", &rgbColor) != 1) {
|
||||
fprintf(stderr, "Invalid rgb component in %s\n", fileName);
|
||||
}
|
||||
if ( rgbColor != RGB_COLOR ) {
|
||||
fprintf(stderr, "Invalid image color range in %s\n", fileName );
|
||||
if (rgbColor != RGB_COLOR) {
|
||||
fprintf(stderr, "Invalid image color range in %s\n", fileName);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
while ( fgetc(fp) != '\n' );
|
||||
image->data = (imagePixel_t *) malloc(image->x * image->y * sizeof(imagePixel_t));
|
||||
if ( !image ) {
|
||||
fprintf(stderr, "malloc() failed" );
|
||||
while (fgetc(fp) != '\n');
|
||||
image->data = (colorChannel_t *)malloc(image->x * image->y * sizeof(imagePixel_t));
|
||||
if (!image) {
|
||||
fprintf(stderr, "malloc() failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if ( fread(image->data, 3 * image->x, image->y, fp) != image->y ) {
|
||||
if (fread(image->data, 3 * image->x, image->y, fp) != image->y) {
|
||||
fprintf(stderr, "Loading image failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
@ -566,46 +625,48 @@ static imagePixel_t *rdPPM ( const char *fileName ) {
|
|||
|
||||
|
||||
/*
|
||||
=======================================================================================
|
||||
=======================================================================================
|
||||
|
||||
mkPpmFile
|
||||
mkPpmFile
|
||||
|
||||
gets output from the result of rdPpmFile and writes a new mkPpmFile. Best Case is a
|
||||
carbon copy of the source image
|
||||
gets output from the result of rdPpmFile and writes a new mkPpmFile. Best Case is a
|
||||
carbon copy of the source image
|
||||
|
||||
=======================================================================================
|
||||
=======================================================================================
|
||||
*/
|
||||
|
||||
void mkPpmFile ( char *fileName, imagePixel_t *image ) {
|
||||
FILE* fp = fopen(fileName, "wb" );
|
||||
if ( !fp ) {
|
||||
fprintf(stderr, "Opening file failed." );
|
||||
void mkPpmFile(char *fileName, imagePixel_t *image) {
|
||||
FILE* fp = fopen(fileName, "wb");
|
||||
if (!fp) {
|
||||
fprintf(stderr, "Opening file failed.");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fprintf(fp, "P6\n");
|
||||
fprintf(fp, "%d %d\n", image->x, image->y);
|
||||
fprintf(fp, "%d\n", RGB_COLOR);
|
||||
fwrite(image->data, 3* image->x, image->y, fp);
|
||||
fwrite(image->data, 3 * image->x, image->y, fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/*
|
||||
======================================================================================
|
||||
======================================================================================
|
||||
|
||||
ppmColorChannel
|
||||
ppmColorChannel
|
||||
|
||||
gets one of the rgb color channels and returns the array
|
||||
gets one of the rgb color channels and returns the array
|
||||
|
||||
======================================================================================
|
||||
*/
|
||||
======================================================================================
|
||||
*/
|
||||
|
||||
int * ppmColorChannel( imagePixel_t *image ) {
|
||||
int length = (image->x * image->y)/3, i;
|
||||
int buffer[length];
|
||||
int * ppmColorChannel(imagePixel_t *image) {
|
||||
int length = (image->x * image->y) / 3;
|
||||
int i = 0;
|
||||
int buffer[10];
|
||||
realloc(buffer,length);
|
||||
|
||||
printf("%d\n", length);
|
||||
if ( image ){
|
||||
for ( i = 0; i < length; i++ ) {
|
||||
if (image) {
|
||||
for (i = 0; i < length; i++) {
|
||||
buffer[i] = image->data[i].green;
|
||||
//output[i] = image->data[i].blue;
|
||||
//output[i] = image->data[i].red;
|
||||
|
@ -613,5 +674,3 @@ int * ppmColorChannel( imagePixel_t *image ) {
|
|||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue