Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to simulate this source code with applet

ais_cikangais_cikang Member Posts: 8
dear everybody ,
I need some help. i was involve with one project. the project is about to make an e-tutor of image compression based on fractal tranform technique by using quadtree partitioning. What this project will do is build an applet that will simulate the image compression processing using fractal transform technique. I hope somebody will help me to solve my problem. Thak you. bye (the source code is below)

source code to compress an image :
/*

compress.c - Fractal Image Compression Demonstration Program

Copyright 1992 Lyman P. Hurd, Michael F. Barnsley
*/

#include
#include
#include

#include "fractal.h"

main(int argc,char **argv)
{
FILE *image_file,*fractal_file;
ImageHeader header;
Rectangle image,domain_block,range_block,flipped_range_block,
reduced_image;
unsigned long current_distance,minimum_distance,infinity;
Symmetry current_symmetry;
short current_range_x,current_range_y,domain_x,domain_y,current_shift;
Pixel domain_mean;
AffineMap best_map;

fprintf(stderr,"
Fractal Image Compression Demonstration - Version 1.0A
");
fprintf(stderr,"
Copyright (c) 1992 Lyman P. Hurd, Michael F. Barnsley
");

if (argc != 3)
{
fprintf(stderr,"
Usage: compress image_file fractal_file.
");
exit(1);
}

/* Step 0: Allocate memory for blocks */

domain_block.width=range_block.width=
flipped_range_block.width=DB_SIDE;
domain_block.height=range_block.height=
flipped_range_block.height=DB_SIDE;
domain_block.length=range_block.length=
flipped_range_block.length=DB_SIDE*DB_SIDE;

domain_block.pixel =
(Pixel *) calloc(DB_SIDE*DB_SIDE,sizeof(Pixel));
range_block.pixel =
(Pixel *) calloc(DB_SIDE*DB_SIDE,sizeof(Pixel));
flipped_range_block.pixel =
(Pixel *) calloc(DB_SIDE*DB_SIDE,sizeof(Pixel));

infinity=255*255*DB_SIDE*DB_SIDE;

/* Step 1: Input Image */

if (NULL == (image_file=fopen(argv[1],"rb")))
{
fprintf(stderr,"Unable to open file %s.
",
argv[1]);
exit(1);
}

if (NULL == (fractal_file=fopen(argv[2],"wb")))
{
fprintf(stderr,"Unable to open file %s.
",
argv[2]);
exit(1);
}

read_tga_header(image_file,&header);

fprintf(stderr,"Width %d Height %d
",header.width,header.height);

if (1!=fwrite(&header,sizeof(ImageHeader),1,fractal_file))
{
fprintf(stderr,"Error writing header to fractal file %s.
",
argv[2]);
exit(1);
}

image.width=header.width;
image.height=header.height;
image.length=header.width*header.height;

image.pixel = (Pixel *) calloc(image.length,
sizeof(Pixel));

reduced_image.width=header.width/2;
reduced_image.height=header.height/2;
reduced_image.length=header.width*header.height/4;

reduced_image.pixel = (Pixel *) calloc(reduced_image.length,
sizeof(Pixel));

if (NULL == image.pixel)
{
fprintf(stderr,"Unable to allocate %ld bytes for image buffer.
",
image.length);
exit(2);
}

if (image.length!=fread(image.pixel,
sizeof(Pixel),image.length,image_file))
{
fprintf(stderr,"Error reading header from image file %s.
",
argv[1]);
exit(1);
}

fclose(image_file);

/* rescale (contract) image in spatial and intensity directions */

reduce_image(&image,&reduced_image);

/* MAIN LOOP */

for (domain_y=0;domain_y
#include
#include
#include "fractal.h"

#define DEFAULT_ITERATES 16

main(int argc,char **argv)
{
AffineMap *affine_map_array,*map_ptr;
FILE *image_file,*fractal_file,*initial_file;
ImageHeader header,initial_header;
short iterate,arg_offset=0,iterates=DEFAULT_ITERATES;
long number_of_maps,i;
short domain_x,domain_y;
Rectangle image,reduced_image,range_block,transformed_range_block;

fprintf(stderr,
"
Fractal Image Decompression Demonstration - Version 1.0A
");
fprintf(stderr,
"
Copyright (c) 1992 Lyman P. Hurd, Michael F. Barnsley
");

if ((argc < 3)||(argc>5))
{
fprintf(stderr,
"
Usage: decompress [num_iterates] [initial_image] fractal_file image_file.
");
exit(1);
}

if (argc==4)
{
iterates=atoi(argv[1]);
arg_offset=1;
}

if (argc==5)
{
iterates=atoi(argv[1]);
initial_file=fopen(argv[2],"rb");
arg_offset=2;
}

/* Read in affine maps and header information. */

if (NULL == (fractal_file=fopen(argv[arg_offset+1],"rb")))
{
fprintf(stderr,"Unable to open fractal file %s.
",
argv[arg_offset+1]);
exit(1);
}

if (NULL == (image_file=fopen(argv[arg_offset+2],"wb")))
{
fprintf(stderr,"Unable to open image file %s.
",
argv[arg_offset+2]);
exit(1);
}

if (1!=fread(&header,sizeof(ImageHeader),1,fractal_file))
{
fprintf(stderr,"Error reading header from fractal file %s.
",
argv[arg_offset+1]);
exit(1);
}

write_tga_header(image_file,&header);

number_of_maps = header.width*header.height/(DB_SIDE*DB_SIDE);
affine_map_array = (AffineMap *)
calloc(number_of_maps,sizeof(AffineMap));

if (number_of_maps!=
fread(affine_map_array,sizeof(AffineMap),number_of_maps,fractal_file))
{
fprintf(stderr,"Error reading data from %s.
",argv[arg_offset+1]);
exit(1);
}
fclose(fractal_file);

image.width = header.width;
image.height = header.height;
image.length = header.width*header.height;
image.pixel = (Pixel *) calloc(image.length,sizeof(Pixel));

reduced_image.width = header.width/2;
reduced_image.height = header.height/2;
reduced_image.length = header.width*header.height/4;
reduced_image.pixel = (Pixel *)
calloc(reduced_image.length,sizeof(Pixel));

range_block.width = DB_SIDE;
range_block.height = DB_SIDE;
range_block.length = DB_SIDE*DB_SIDE;
range_block.pixel = (Pixel *) calloc(range_block.length,sizeof(Pixel));

transformed_range_block.width = DB_SIDE;
transformed_range_block.height = DB_SIDE;
transformed_range_block.length = DB_SIDE*DB_SIDE;
transformed_range_block.pixel =
(Pixel *) calloc(transformed_range_block.length,sizeof(Pixel));

/* Set source buffer to a predetermined starting condition. */

if (argc<5)
{
for (i=0;i<image.length;i++)
image.pixel[i]=ARBITRARY_PIXEL_VALUE;
}
else
{
read_tga_header(initial_file,&initial_header);
fread(image.pixel,image.length,sizeof(Pixel),initial_file);
}

/* Loop for a prescribed number of iterations. */

for (iterate=0;iterate<iterates;iterate++)
{

reduce_image(&image,&reduced_image);

/* Loop over domain blocks. */

for (domain_y=0,map_ptr=affine_map_array;
domain_y<image.height;domain_y+=DB_SIDE)
for (domain_x=0;domain_x<image.width;domain_x+=DB_SIDE,map_ptr++)
{

/* Extract range block. */

copy_rectangle(&reduced_image,map_ptr->range_x,
map_ptr->range_y,&range_block,0,0,
DB_SIDE,DB_SIDE);

intensity_shift(&range_block,map_ptr->shift);

/* Apply indicated symmetry. */

flip(&range_block,&transformed_range_block,
map_ptr->symmetry);

/* Insert transformed block into image. */

copy_rectangle(&transformed_range_block,0,0,&image,
domain_x,domain_y,DB_SIDE,DB_SIDE);
}
}

if (image.length != fwrite(image.pixel,sizeof(Pixel),image.length,
image_file))
{
fprintf(stderr,"Error writing data to %s.
",argv[arg_offset+2]);
exit(1);
}

free(affine_map_array);
free(image.pixel);
free(reduced_image.pixel);
fclose(image_file);
return(0);
}


Comments

  • IllcoIllco Member Posts: 382
    This is C++ source code. The first step would be to convert it to Java and that includes making it object-oriented. An applet uses the default class Applet, and no main method. The main method code is replaced by code in the init() method.
  • ais_cikangais_cikang Member Posts: 8

    1stly tq for replying my mssg. perhaps i can do it, however i was new with java. So, what is the first step i should do and so on.Any help is greatly appreciated.
  • IllcoIllco Member Posts: 382
    Well, I think it will keep you busy for a while. First, let's note that if you want to do it the nice way (and learn Java well), you should redesign such that important concepts in the problem become classes in the program. You already did some with Symmetry and Pixel. Those will definitely become classes, but other parts might too.

    Furthermore, you need to get rid of the pointers. Java uses only references and basic types. Benefit: you don't need to allocate memory explicitly and certainly not release it (Java will do that automatically).

    If you want to do it the ugly way, create one big class and stuff all your code in the main method, just like you did in the C program. I don't know who wrote fractal.h, but since you're using quite a lot from it you will probably need to rewrite that part as well. Along with all other non-standard libs you want to use. On the other hand, Java provides a lot of standard functionality, so you should check that out.

    Writing all this, it comes to my mind that quite some Java knowledge is involved in doing this (e.g. knowing what is there, knowing what is allowed, etc), so I think you'd better stick to the C code. Why do you want an applet (so maybe I can give you a better idea...)?
  • ais_cikangais_cikang Member Posts: 8

    Thanks 4 your kindness. I want an applet because of I need to simulate that process of source code (.c). It's a project of mine named e-tutor of image compression based on fractal technique. Any better idea? I greatly appreciated if there any better idea from you.

  • chamsterchamster Member Posts: 662
    : So, what is the first step i should do and so on.Any help is greatly appreciated.

    The first step i'd recommend would be to create a simple "Yo, World!" applet just to get the idea of what it looks like. Since you're obviously familiar with programming learning enough Java to complete you task will be a short issue.

    So i'd suggest that you start making an applet :-)



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • IllcoIllco Member Posts: 382
    :
    : Thanks 4 your kindness. I want an applet because of I need to simulate that process of source code (.c). It's a project of mine named e-tutor of image compression based on fractal technique. Any better idea? I greatly appreciated if there any better idea from you.
    :
    :

    No, I think you chose the right thing. But it still is kinda tricky to port it to Java I think. With C you might try Microsoft's DCOM (which essentially is .NET I think). This is for Distributed Component Object Model, for supplying web applications. But I have no knowledge of DCOM, so I can't help you there.
  • ais_cikangais_cikang Member Posts: 8

    That ok guys. You guys have guide me do a wright step. i need 2 try what have you guy told me. thanks anyway 4 your kindness.
  • Chris BrownChris Brown USAMember Posts: 4,496 ✭✭

    ___ \ http://forcoder.org \ free ebooks and video tutorials about / Ruby, Scratch, Perl, C, Delphi, C#, R, Python, JavaScript, C++, MATLAB, Swift, Assembly, Objective-C, Visual Basic, PL/SQL, PHP, Go, Visual Basic .NET, Java Scheme, LabVIEW, Lua, Scala, VBScript, F#, COBOL, D, Alice, Logo, Kotlin, Prolog, Crystal, Lisp, ML, ABAP, FoxPro, Awk, Rust, Dart, Ada, Julia, Hack, SAS, Fortran, Clojure, Bash, Apex, Erlang, Transact-SQL / ___________

Sign In or Register to comment.