/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * image_steg.c
 * Copyright (C) 2021 TikalonLLC <gualtieri@ieee.org>
 * 
 * image steganography is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * image steganography is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include <math.h>

#define width 50
#define height 50
#define image_dimension 1000
#define bins (image_dimension*image_dimension)/(width*height)

/* Prototypes */
char *strcpy(char *dest, const char *src);
char *strcat(char *dest, const char *src);
int rand(void);
void exit(int status);
char uppercase(char ch);

int x, y, intensity, size;
char ch;
char fn1[64];			//input text filename
char fn2[64];			//svg output filename
char st_format[255] = "";
FILE *indata;
FILE *outdata;
int i;


char uppercase(char ch)
{
//converts lower to upper case letters, passes all others
//ASCII codes: A=65,Z=90,a=97,z=122
    if ((ch > 96) && (ch < 123)) {
	ch = ch - 32;
    }
//printf("%d-",ch);
    return ch;
}

int main(int argc, char *argv[])
{

    if (argc < 3) {
	printf("\nUsage: image_steg inputfilename outputfilename\n");
	exit(1);
    }

    strcpy(fn1, argv[1]);
    strcpy(fn2, argv[2]);

    printf("\nInput file selected = %s\n", fn1);
    printf("\nOutput file selected = %s\n", fn2);

//open files
    if ((indata = fopen(fn1, "r")) == NULL) {
	printf("\nInput text file cannot be opened.\n");
	exit(1);
    }
//open output file
    if ((outdata = fopen(fn2, "wb")) == NULL) {
	printf("\nOutput svg file cannot be opened.\n");
	exit(1);
    }
//Seed random number generator
    srand((unsigned) time(NULL));

// Print svg headers
//printf(" <svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n");
    fprintf(outdata,
	    " <svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n");

/*
Basic SVG rectangle code:
  <rect width="25" height="25" style="fill:rgb(0,0,255);stroke-width:0;stroke:rgb(0,0,0)" />
<rect width="25" height="25" x="0" y="0" fill = "rgb(255,0,0)" />
</svg>
SVG Coordinate system:
the origin (x,y) = (0,0) is at the top left corner.  Positive x goes to the right, positive y-goes down
*/

//Print background

    strcat(st_format,
	   "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"");
    strcat(st_format,
	   " fill=\"white\" stroke=\"black\" stroke-width=\"2\" />\n");
//printf("%s",st_format);
    fprintf(outdata, st_format, 0, 0, image_dimension, image_dimension);

//read text file and make rectangles

    x = 0;
    y = 0;
    size = 0;

    ch = fgetc(indata);
    if (ch != EOF)
	ch = uppercase(ch);
    do {
//eliminate all but uppercase from output
	if ((ch > 64) && (ch < 91)) {
//draw rectangles
	    strcpy(st_format, "");
	    strcat(st_format,
		   "<rect width=\"%d\" height=\"%d\" x=\"%d\" y=\"%d\" fill=\"rgb(%d,%d,%d)\" />\n");
//printf("%s",st_format);
	    intensity = ch;
//ASCII codes: A=65,Z=90
	    if ((ch > 64) && (ch < 91)) {
		size++;
//stretch range
		intensity = ch - 65;
		intensity = 255 * ((float) intensity / 26);
//printf("%d>>>%d",ch,intensity);
	    }
	    fprintf(outdata, st_format, width, height, x, y, intensity,
		    rand() % 255, rand() % 255);
	    x = x + width;
	    if (x > (1000 - width)) {
		x = 0;
		y = y + height;
	    }
	    if (y > (1000 - height)) {
		y = 0;
	    }
	}

	ch = fgetc(indata);
	if (ch == EOF) {
	    rewind(indata);
	    ch = fgetc(indata);
	}
	if (ch != EOF) {
	    ch = uppercase(ch);
	}
    }
    while (size <= bins);

// Print svg footers
//printf("</svg>");
    fprintf(outdata, "</svg>");

    fclose(outdata);

    printf("Done\n");
    return (0);
}
