POV-Ray : Newsgroups : povray.binaries.utilities : imprint.c Server Time
23 Dec 2024 18:50:01 EST (-0500)
  imprint.c (Message 1 to 1 of 1)  
From: Lewis Sellers
Subject: imprint.c
Date: 7 Nov 1998 19:16:47
Message: <3644E305.72D238FC@usit.net>
/*
this is the imprint.c prog which needs to be worked on a little more to
get it to work under gcc. struct align probs....
*/

#include <malloc.h>
//#include <alloc.h>
#include <stdio.h>
#include <dir.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
#include <math.h>
#include <bios.h>
#include <string.h>
#include <dpmi.h>

#define FALSE (1==0)
#define TRUE  (1==1)

#define FAILURE 0
#define SUCCESS 1

#ifdef __cplusplus
	 #define __CPPARGS ...
#else
	 #define __CPPARGS
#endif

typedef unsigned char byte;
typedef unsigned short int word;
typedef unsigned long dword;

//TGA
struct sstgah_struct {
	unsigned char CharacterIdentificationField;
	unsigned char ColorMapType;
	unsigned char ImageTypeCode; //always 2
	unsigned short int ColorMapOrigin __attribute__ ((packed));
	unsigned short int ColorMapLength;
	unsigned char ColorMapEntrySize; //we only do 24
	unsigned short int XOrigin __attribute__ ((packed));
	unsigned short int YOrigin;
	unsigned short int Width;
	unsigned short int Height;
	unsigned char ImagePixelSize; //24?
	unsigned char ImageDescriptorByte;
} tgah,tgah_i,tgah_struct;
char CIF[256];
struct ssbgr_struct {
	byte blue;
	byte green;
	byte red;
} *imprint, *image, *p, *bgr_struct;
unsigned long sizeof_imprint=0L;
unsigned long pplus=0L;

//prototypes
int getlines(void);
void readme(void);
int image_check(char *name);
int image_info(char *name);
int imprint_get(void);
int image_process(char *name);
void folder_process(void);
void info(void);
unsigned int  nonalloced();


////////////////////////////////////////////////////////////////////////////
//
/*
  unsigned long available_memory;
  unsigned long available_pages;
  unsigned long available_lockable_pages;
  unsigned long linear_space;
  unsigned long unlocked_pages;
  unsigned long available_physical_pages;
  unsigned long total_physical_pages;
  unsigned long free_linear_space;
  unsigned long max_pages_in_paging_file;
  unsigned long reserved[3];
*/
unsigned int  nonalloced()
{
	//_go32_dpmi_meminfo x;
	//__dpmi_get_free_memory_information((__dpmi_free_mem_info *)(x))
	//return x.largest_available_free_block_in_bytes;
	return 0;
}



////////////////////////////////////////////////////////////////////////////
//
//readme.txt
void readme(void)
{
	FILE *me;

	 //
	me=fopen("readme.txt","wt");
	fprintf(me,"\n");
	fprintf(me,"No arguments. README.TXT created. Please read for detailed
information on the usage of this program.'\n");
	fprintf(me,"--min\n");
	fprintf(me,"\n");
	fprintf(me,"Lewis A. Sellers: writer and contract Multimedia Website
Developer\n");
	fprintf(me,"mailto:lse### [at] usitnet (The Fourth Millennium
Foundation)\n");
	fprintf(me,"http://www.public.usit.net/lsellers/ &
http://www.intrafoundation.com\n");

fprintf(me,"http://brain-of-pooh.tech-soft.com/users/critters/bios/sellers_lewis.html\n");
	fprintf(me,"\n");
	fprintf(me,"You can bug the living bejesus out of me live on ICQ @
491461\n");
	fprintf(me,"(If I don't get back to you within a month, I'm out of
prozac in some dark corner somewhere screaming things quite
unintelligable but -- most curiously -- thick with a sumerian
accent.)\n");
	fprintf(me,"\n");
	fprintf(me,"'The comedy is over' -i pagliacci\n");
	fclose(me);

	//
	printf("No arguments. README.TXT created. Please read for detailed
information on the usage of this program.'\n");
	printf("--min\n");

	printf("\n");
	printf("Lewis A. Sellers: writer and contract Multimedia Website
Developer\n");
	printf("mailto:lse### [at] usitnet (The Fourth Millennium
Foundation)\n");
	printf("http://www.public.usit.net/lsellers/ &
http://www.intrafoundation.com\n");

printf("http://brain-of-pooh.tech-soft.com/users/critters/bios/sellers_lewis.html\n");
	printf("\n");
	printf("You can bug the living bejesus out of me live on ICQ @
491461\n");
	printf("(If I don't get back to you within a month, I'm out of prozac
in some dark corner somewhere screaming things quite unintelligable but
-- most curiously -- thick with a sumerian accent.)\n");
	printf("\n");
	printf("'The comedy is over' -i pagliacci\n");
}


////////////////////////////////////////////////////////////////////////////
//
//inpho
void info(void)
{
	int memory_size;

	clrscr();
	printf("IMPRINT 0.0.2 imaging utility by Lewis A. Sellers III.\n");
	printf("Last compiled %s %s. %d lines of C
code.\n",__DATE__,__TIME__,getlines());

	#ifdef __TURBOC__
		 printf("Compiler: Borland C++ version %x.%x. ",
		 (__TURBOC__)/256,(__TURBOC__%256));
	 #endif

//         printf("Memory:%lu. Stack:%u. ",farcoreleft(),stackavail());
/*
	 if(_8087)
		  printf("FPU:%d87.",_8087);
	 else
		  printf("No FPU.");
*/

	memory_size = biosmemory();  // returns value up to 640K
	printf(" %dK virtual RAM\n",memory_size);
/*
	printf(" _stklen %u",_stklen);
	printf(" _heaplen %u\n",_heaplen);
//	printf(" _ovrbuffer %u\n",_ovrbuffer);
*/

	#ifdef __TURBOC__
		printf(" farcoreleft %lu bytes",farcoreleft());
		printf(" coreleft %u bytes\n",coreleft());
	#endif

	printf(" remaining memory %u bytes\n", nonalloced() );

	printf(" tgah_struct (%u bytes)\n",sizeof(tgah_struct));
	printf(" bgr_struct (%u bytes)\n",sizeof(bgr_struct));
}


////////////////////////////////////////////////////////////////////////////
//
int image_check(char *name)
{
	FILE *tga;
	char ok=TRUE;

	//
	tga=fopen(name,"rb"); //open the file.
	if(tga==NULL) return FAILURE;
	fread(&tgah_i,sizeof(tgah_struct),1,tga); //get the header
	fclose(tga);

	//give us some info...
	if(tgah_i.ImageTypeCode!=2) ok=FALSE;
	if(tgah_i.ImagePixelSize!=24) ok=FALSE;
	if(tgah_i.ImageDescriptorByte!=32) ok=FALSE;

	return(ok);
}


////////////////////////////////////////////////////////////////////////////
//
//kjllkjm <-susy oct 30 '98
int image_info(char *name)
{
	 FILE *tga;
	 char ok=TRUE;

	 //open the file.
	 tga=fopen(name,"rb");
	 if(tga==NULL) return FAILURE;

	 //get the header
	 fread(&tgah_i,sizeof(tgah),1,tga);

	 if(tgah_i.CharacterIdentificationField!=2)
		 fread(&CIF,tgah.CharacterIdentificationField,1,tga);

	 //
	 fclose(tga);

	 //give us some info...
	 printf(" %s\n",name);

	 printf("  CharacterIdentificationField
%d\n",tgah_i.CharacterIdentificationField);
	 printf("  ColorMapType %d\n",tgah_i.ColorMapType);
	 printf("  ImageTypeCode %d\n",tgah_i.ImageTypeCode);
	 if(tgah_i.ImageTypeCode!=2) {
		  printf("   **must be type 2 - unmapped RGB\n");
		  ok=FALSE;
	 }
	 printf("  ColorMapOrigin %d\n",tgah_i.ColorMapOrigin);
	 printf("  ColorMapLength %d\n",tgah_i.ColorMapLength);
	 printf("  ColorMapEntrySize %d\n",tgah_i.ColorMapEntrySize);
	 printf("  XOrigin %d\n",tgah_i.XOrigin);
	 printf("  YOrigin %d\n",tgah_i.YOrigin);
	 printf("  Width %d\n",tgah_i.Width);
	 printf("  Height %d\n",tgah_i.Height);
	 printf("  ImagePixelSize %d\n",tgah_i.ImagePixelSize);
	 if(tgah_i.ImagePixelSize!=24) {
		  printf("   **must be 24 - 24 bit RGB\n");
		  ok=FALSE;
	 }
	 printf("  ImageDescriptorByte %d\n",tgah_i.ImageDescriptorByte);
	 if(tgah_i.ImageDescriptorByte!=32) {
		  printf("   **must be 32 - upper left origin\n");
		  ok=FALSE;
	 }
	if(tgah_i.CharacterIdentificationField>0) {
		printf("  CharacterIdentificationField:\n");
		printf("   '%s'\n",&CIF);
	}
	printf(" [%s]\n\n",name);

	if(ok)
		return(SUCCESS);
	else
		return(FAILURE);
}


////////////////////////////////////////////////////////////////////////////
//
int imprint_get()
{
	FILE *tga;
	int y;

	//open the file.
	tga=fopen("imprint.tga","rb");
	if(tga==NULL) {
		printf("imprint.tga not found. required for processing.\n");
		return FAILURE;
	}

	//get the header
	fread(&tgah,sizeof(tgah),1,tga);

	if(tgah.CharacterIdentificationField!=2)
		fread(&CIF,tgah.CharacterIdentificationField,1,tga);

	sizeof_imprint=(unsigned long)tgah.Width*(unsigned long)tgah.Height;
	pplus=(unsigned long)(tgah.Width*sizeof(bgr_struct));

	p=imprint;
	for(y=0;y<tgah.Height;y++) {
		fread(p,sizeof(bgr_struct),tgah.Width,tga);
		p+=pplus;
	}

	fclose(tga);

	printf("imprint.tga loaded (sizeof_imprint %lu, pplus
%lu)\n",sizeof_imprint,pplus);
	return(SUCCESS);
}


////////////////////////////////////////////////////////////////////////////
//
//test ----------
void imprint_test(void)
{
	FILE *tga;
	int y;
	//int x;
	//long i;

	/*
	i=0;
	for(y=0;y<tgah.Height;y++) {
		for(x=0;x<tgah.Width;x++) {
			imprint[i].red=(byte)(255-imprint[i].red);
			imprint[i].green=(byte)(255-imprint[i].green);
			imprint[i].blue=(byte)(255-imprint[i].blue);
			if(i<2000) i++;
		}
	}
	*/

	tga=fopen("imprint2.tga","wb");
	fwrite(&tgah,sizeof(tgah_struct),1,tga);
	if(tgah.CharacterIdentificationField!=2)
		fwrite(&CIF,tgah.CharacterIdentificationField,1,tga);

	p=imprint;
	for(y=0;y<tgah.Height;y++) {
		fwrite(p,sizeof(bgr_struct),tgah.Width,tga);
		p+=pplus;
	}

	fclose(tga);
}


////////////////////////////////////////////////////////////////////////////
//
int image_process(char *name)
{
	FILE *tga;
	//int x;
	int y;
	//long i;

	//open the file.
	tga=fopen(name,"rb");
	if(tga==NULL) return FAILURE;

	//get the header
	fread(&tgah,sizeof(tgah_struct),1,tga);

	if(tgah.CharacterIdentificationField!=2)
		 fread(&CIF,tgah.CharacterIdentificationField,1,tga);

	p=image;
	for(y=0;y<tgah.Height;y++) {
		fread(p,(unsigned int)sizeof(bgr_struct),tgah.Width,tga);
		p+=pplus;
	}

	fclose(tga);


/*
	//take imprint image and alpha to this tga
	i=0;
	for(y=0;y<tgah.Height;y++) {
		for(x=0;x<tgah.Width;x++) {
			if( (imprint[i].red==0u) & (imprint[i].green==0u) &
(imprint[i].blue==0u)) {
				image[i].red=imprint[i].red;
				image[i].green=imprint[i].green;
				image[i].blue=imprint[i].blue;
			}
			i++;
		}
	}
*/

	//
	tga=fopen(name,"wb");
	fwrite(&tgah,sizeof(tgah_struct),1,tga);
	if(tgah.CharacterIdentificationField!=2)
		fwrite(&CIF,tgah.CharacterIdentificationField,1,tga);

	p=image;
	for(y=0;y<tgah.Height;y++) {
		fwrite(p,(unsigned int)sizeof(bgr_struct),tgah.Width,tga);
		p+=pplus;
	}

	fclose(tga);


	printf("%s processed...\n",name);
	return(SUCCESS);
}


////////////////////////////////////////////////////////////////////////////
//
void folder_process(void)
{
	int no,done;
	struct ffblk dirlist;

	no=0;
	done=findfirst("*.TGA",&dirlist,0);
	while (done==0) {
		if( (strcmp(dirlist.ff_name,"IMPRINT.TGA")==0) |
			(strcmp(dirlist.ff_name,"IMPRINT2.TGA")==0) ) {
			printf("skip %s\n",dirlist.ff_name);
		}
		else {
			if(image_check(dirlist.ff_name)==FAILURE)
				image_info(dirlist.ff_name);
			else
				image_process(dirlist.ff_name);
		}
		no=no+1;
		done=findnext(&dirlist);
	}
	printf("\n%d TGA files.\n\n",no+1);
}




////////////////////////////////////////////////////////////////////////////
//
int main(int argc,char *argv[])
{
	int n;
	info();

	//
	if(argc>=2) {
		printf("argc=%d\n",argc);
		for(n=1;n<argc;n++) {
			printf(" argv=%d %s\n",n,argv[n]);
		}
		readme();
		exit(SUCCESS);
	}


	//
	imprint=(void *)malloc(340*240*sizeof(bgr_struct));
	if(imprint==NULL) {
		printf("Not enough memory for imprint buffer (%u bytes remaining)\n",
nonalloced() );
		exit(FAILURE);
	}
	printf("imprint buffer allocated (%u bytes remaining)\n", nonalloced()
); // zzzzzzmngfv()); <--susy nov 7
	printf("Address of allocated block is: %p\n", imprint);

	image=(void *)malloc(340*240*sizeof(bgr_struct));
	if(image==NULL) {
		printf("Not enough memory for image buffer (%u bytes remaining)\n",
nonalloced() );
		exit(FAILURE);
	}
	printf("image buffer allocated (%u bytes remaining)\n", nonalloced() );
	printf("Address of allocated block is: %p\n", image);

	//
	imprint_get();
	imprint_test();
	folder_process();


//     getch();
	//
	free(image);
	free(imprint);

	return(SUCCESS);
}


////////////////////////////////////////////////////////////////////////////
//
//cute trick of mine for single file programs
int getlines() { return __LINE__+1; }



-- 
Lewis A. Sellers: writer and contract Multimedia Website Developer
mailto:lse### [at] usitnet (The Fourth Millennium Foundation)
http://www.public.usit.net/lsellers/ & http://www.intrafoundation.com
http://brain-of-pooh.tech-soft.com/users/critters/bios/sellers_lewis.html

You can bug the living bejesus out of me live on ICQ @ 491461
(If I don't get back to you within a month, I'm out of prozac in some
dark corner somewhere screaming things quite unintelligable but -- most
curiously -- thick with a sumerian accent.)

"The comedy is over" -i pagliacci


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.