LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-25-2010, 05:57 PM   #1
fpsasm
LQ Newbie
 
Registered: Feb 2010
Location: UK, Leeds
Distribution: PCLinuxOS, Fedora Core, SuSE,ubuntu
Posts: 20

Rep: Reputation: 1
Calculations, High resolution mandelbrot (working code)


Hello, I am piecing a Mandelbrot generator up, I have one working to produce a 1,500 x 1,500 picture (which is fairly accurate).

I would like to increase the resolution over 100,000 x 100,000 + (into the millions x millions).

Could anyone give me some pointers in how I can achieve this? What kind of buffer sizes do I need?

(note this code doesnt have any memory allocations yet)

here is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <math.h>

//define size
#define wd 1500
#define hg 1500

void perr(char *);

main(){
double x,y;
double xstart, xstep, xend, ystart, ystep, yend;
double z, zi, newz, newzi;
double colour;

int iter;
char pic[hg][wd][3];
/* Counters */
int hgCounter,wdCounter,iterCounter;
int inset;
int fd;
char buffer [4096];

/* set up real and imaginary axis */
xstart = -2;
xend = 2;

ystart = -2;
yend = 2;

/* request number of iterations */
printf("Enter amount of iterations:");
if(scanf("%d",&iter)<1){
perr("ERROR, invalid input\n");
}

/* mapping points to pixels */
xstep = (xend - xstart)/wd;
ystep = (yend - ystart)/hg;

/* main loop */
x = xstart;
y = ystart;

for(hgCounter=0; hgCounter<hg; hgCounter++){
printf("Calculating line: %d\n", hgCounter);

for(wdCounter=0; wdCounter<wd; wdCounter++){
z = 0;
zi = 0;
inset = 1;

for(iterCounter=0; iterCounter<iter; iterCounter++){
/* quadratic where (a+bi)^2 */
newz = (z*z) - (zi*zi) + x;
newzi = 2*z*zi +y;
z = newz;
zi = newzi;
if(((z*z)+(zi*zi))>4){
inset = 0;
colour = iterCounter;
iterCounter = iter;
}
}
if(inset){
pic[hgCounter][wdCounter][0] = 0;
pic[hgCounter][wdCounter][1] = 0;
pic[hgCounter][wdCounter][2] = 0;
}else{
pic[hgCounter][wdCounter][0] = (colour+1)/iter*255;
pic[hgCounter][wdCounter][1] = (colour+1)/iter*128;
pic[hgCounter][wdCounter][2] = (colour+1)/iter*128;
}

x += xstep;
}
y += ystep;
x = xstart;
}

if((fd = open("mbrot.tga", O_RDWR+O_CREAT, 0)) == -1){
perr("ERROR, opening file\n");
}

buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 2; //colour map type
buffer[8] = 0;
buffer[9] = 0;
buffer[10] = 0;
buffer[11] = 0;
buffer[12] = (wd & 0x00FF);
buffer[13] = (wd & 0xFF00)>>8;
buffer[14] = (hg & 0x00FF);
buffer[15] = (hg & 0xFF00)>>8;
buffer[16] = 24;
buffer[17] = 0;
write(fd, buffer, 18);
write(fd, pic, wd*hg*3);
close(fd);
}


void perr(char *msg){
perror(msg);
exit(0);
}
The program outputs the Mandelbrot as a .tga file. I calculated the image on fixed axis of -2 to 2 (on the x and y axis). Image produced: http://i656.photobucket.com/albums/u...g?t=1267142207


Thanks for the help,
Matthew
 
Old 02-25-2010, 07:52 PM   #2
John VV
Guru
 
Registered: Aug 2005
Posts: 12,694

Rep: Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687
first
DO YOU have any exp. working with images in this size range
Quote:

I would like to increase the resolution over 100,000 x 100,000 + (into the millions x millions).
i do working with images that are 32768x16384, 65536x32768 and even a 128k map 131072 x 65536 pix.
a 32k 8 bit rgb image is 1.5 Gig
a 8 bit rgb 64 k image is 6 Gig
a 128 k image rgb will be over 24 gig in size

CAN YOU work with ONE image of this size??????

Gimp WILL not open a 100k image .
 
1 members found this post helpful.
Old 02-25-2010, 08:06 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
A single image of that size would be very demanding of memory. Have you considered creating several smaller images that can be joined together?
 
1 members found this post helpful.
Old 02-25-2010, 11:03 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 167Reputation: 167
A 256 color image of that size is 9 GB.
Who has that kind of ram.
 
1 members found this post helpful.
Old 02-26-2010, 01:40 AM   #5
fpsasm
LQ Newbie
 
Registered: Feb 2010
Location: UK, Leeds
Distribution: PCLinuxOS, Fedora Core, SuSE,ubuntu
Posts: 20

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by John VV View Post
first
DO YOU have any exp. working with images in this size range

i do working with images that are 32768x16384, 65536x32768 and even a 128k map 131072 x 65536 pix.
a 32k 8 bit rgb image is 1.5 Gig
a 8 bit rgb 64 k image is 6 Gig
a 128 k image rgb will be over 24 gig in size

CAN YOU work with ONE image of this size??????

Gimp WILL not open a 100k image .
I had a 150MB file ,(colour png file 15,000x 10,000) image open on Fireworks. If I didn't have a program to open a file that big, I could always make one. ;P

Quote:
Originally Posted by smeezekitty View Post
A 256 color image of that size is 9 GB.
Who has that kind of ram.
Well 12GB isnt alot of memory anymore, now you can buy 1GB for about 15 (DDR2). "gamers" now have 6-16GB of ram (totally overkill though)



Quote:
Originally Posted by graemef View Post
A single image of that size would be very demanding of memory. Have you considered creating several smaller images that can be joined together?
No i Havent, Thinking about it, thats the easiest way of doing it, simple loop and scale changes.

Thanks for your feed back.

Cheers,
Matthew
 
Old 02-26-2010, 02:17 AM   #6
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
May I ask where this knowledge is useful? How did you become familiar with this subject?
 
Old 02-26-2010, 02:24 AM   #7
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
[slightly offtopic]
I'd like to know too, if it's actually "useful" as well, but even if it isn't really "useful" it sure does generate some neat-o images. When I first saw the thread title, it remembered me back to my C-64 days, where I had entered a Mandelbrot Set image generator program in assembly, from out of Compute! magazine, which you could start from the world view (as shown in the OP's first image attachment) and you could select areas with cross-hairs and zoom in and rebuild the image. After about 3 or 4 zooms, I could watch each raster line being drawn, literally almost pixel by pixel, it was so frikkin slow but that was a 1 MHz processor. I used to let it run overnight, and in the morning, turn on the monitor to see my new image LOL. Really cool pictures.

Sorry for being off-topic!
[/slightly offtopic]

Sasha
 
2 members found this post helpful.
Old 02-26-2010, 02:41 AM   #8
fpsasm
LQ Newbie
 
Registered: Feb 2010
Location: UK, Leeds
Distribution: PCLinuxOS, Fedora Core, SuSE,ubuntu
Posts: 20

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Dogs View Post
May I ask where this knowledge is useful? How did you become familiar with this subject?
First of all, with most new discovery there is no real useful application at that time.

However, because of the amount of mathematical work done on fractals (which a mandelbrot is) we can upscale a pixelated picture to a highly accurate one. Check this out:Fractal Image Compression

People asked Faradays ' What is so important about this, all your doing is playing with magnets and wire? '

There is a good video up on youtube (it's old, I think its 10 years old). http://www.youtube.com/watch?v=qB8m85p7GsU



Chaos theory, you might think that this doesn't apply to everyday life, but actually, it is used alot. For example, the best microwaves will use chaos theory to try to heat the object evenly.

normally, we discover something, play around with it, try to apply it to life, and it's either a flop or not :P
 
Old 02-26-2010, 04:42 AM   #9
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Any sane mandelbrot generator has a fixed image size and you can zoom in and out, you will never find anyone crazy enough to try to display a huge high-resolution image of mandelblot, and have no clue why such a thing would be desired or useful or possible. It isn't possible, it would require too much RAM as people have stated earlier.
 
1 members found this post helpful.
Old 02-26-2010, 06:10 AM   #10
fpsasm
LQ Newbie
 
Registered: Feb 2010
Location: UK, Leeds
Distribution: PCLinuxOS, Fedora Core, SuSE,ubuntu
Posts: 20

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by H_TeXMeX_H View Post
Any sane mandelbrot generator has a fixed image size and you can zoom in and out, you will never find anyone crazy enough to try to display a huge high-resolution image of mandelblot, and have no clue why such a thing would be desired or useful or possible. It isn't possible, it would require too much RAM as people have stated earlier.
The reason I want to have a huge res picture, is because I do.. I want a computer to contiunously compute a huge "picture" of a mandelbrot, and on my PS3, I want to make a zoomable high-iteration mandelbrot.

anyways.. thanks for all the help
 
Old 02-26-2010, 06:24 AM   #11
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 582

Rep: Reputation: 129Reputation: 129
I believe fractint can also create huge high resolution images and the source is available so have a look at that.
 
Old 02-26-2010, 06:48 AM   #12
fpsasm
LQ Newbie
 
Registered: Feb 2010
Location: UK, Leeds
Distribution: PCLinuxOS, Fedora Core, SuSE,ubuntu
Posts: 20

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by whizje View Post
I believe fractint can also create huge high resolution images and the source is available so have a look at that.
cheers, but
(a) i want to do this, to learn about multi-core/smp/ppe/spe programming.
(b) its fun
(c) it looks cool

:P

thanks tho,
matthew
 
Old 02-26-2010, 04:01 PM   #13
John VV
Guru
 
Registered: Aug 2005
Posts: 12,694

Rep: Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687Reputation: 1687
just a warning working with 6 to 24+ Gig. images is NOT easy

an example

using "imagemagick" a tilling opp.( cut into tiles )
cut a 16384x8192 image into 512 px tiles will take about 3 to 4 hours

the same opp. using the " vips" image lib will take 5 Min.
there is a VERY BIG deference in image libs
Gimp,Imagemagick, DevIl , Vips ,IM, CImg.
they all work differently

PS you might want to look at using CImg
there is a tutorial for this
CImg_demo.cpp
Code:
// Item : Mandelbrot/Julia Explorer
//----------------------------------
void* item_mandelbrot_explorer() {

  // Define image canvas and corresponding display window.
  CImg<unsigned char> img(800,600,1,3,0);
  CImgDisplay disp(img);

  // Start main explorer loop.
  double julia_r = 0, julia_i = 0;
  for (bool endflag = false, fractal_type = false, smooth = false, show_help = true; !endflag;) {
    bool stopflag = false;
    double xmin, xmax, ymin, ymax;

    // Init default upper-left/lower-right coordinates of the fractal set.
    if (fractal_type) { xmin = -1.5; xmax = 1.5; ymin = -1.5; ymax = 1.5; julia_r = 0.317; julia_i = 0.029; }
    else { xmin = -2.25; xmax = 1.0; ymin = -1.5; ymax = 1.5; julia_r = julia_i = 0; }

    // Create random palette for displaying the fractal set.
    const CImg<unsigned char> palette =
      CImg<unsigned char>(256,1,1,3,16+120).noise(119,1).resize(1024,1,1,3,3).fillC(0,0,0,0,0,0);

    // Enter event loop for the current fractal set.
    for (unsigned int maxiter = 64; !stopflag; ) {

      // Draw Mandelbrot or Julia fractal set on the image.
      img.resize(disp.resize().set_title("[#7] - %s Set : (%g,%g)-(%g,%g), %s = (%g,%g) (%u iter.)",
                                         fractal_type?"Julia":"Mandelbrot",xmin,ymin,xmax,ymax,
                                         fractal_type?"c":"z0",julia_r,julia_i,maxiter)).
        fill(0).draw_mandelbrot(palette,1,xmin,ymin,xmax,ymax,maxiter,smooth,fractal_type,julia_r,julia_i);

      // Display help if necessary.
      if (show_help) {
        const unsigned char white[] = { 255, 255, 255 };
        static CImg<unsigned char>
          help = CImg<unsigned char>().draw_text(0,0,"\n"
                                                 "  Use mouse to zoom on desired region.  \n"
                                                 "  H             Show/Hide help  \n"
                                                 "  PAD 1...9       Fractal navigation  \n"
                                                 "  PAD +/-       Zoom/Unzoom  \n"
                                                 "  SPACE         Set/Disable color smoothing  \n"
                                                 "  ENTER         Switch Mandelbrot/Julia sets  \n"
                                                 "  Arrows        Change set parameterization  \n"
                                                 "  Page UP/DOWN  Add/Reduce iteration numbers  \n\n",
                                                 white);
        help.draw_rectangle(2,2,help.width() - 3,help.height() - 3,white,1,~0U);
        img.draw_image(img.width() - help.width(),help,0.7f);
      }

      // Get rectangular shape from the user to define the zoomed region.
      const CImg<int> selection = img.get_select(disp,2,0);
      const int xs0 = selection[0], ys0 = selection[1], xs1 = selection[3], ys1 = selection[4];

      // If the user has selected a region with the mouse, then zoom-in !
      if (xs0>=0 && ys0>=0 && xs1>=0 && ys1>=0) {
        const double dx =(xmax - xmin)/img.width(), dy =(ymax - ymin)/img.height();
        const int dsmax = (ys1 - ys0)/2, xs = (xs0 + xs1)/2, ys = (ys0 + ys1)/2;

        // If the region is too small (point) then reset the fractal set position and zoom.
        if (dsmax<5) stopflag = true;
        xmin+=(xs - dsmax*dy/dx)*dx;
        ymin+=(ys - dsmax)*dy;
        xmax-=(img.width() - xs - dsmax*dy/dx)*dx;
        ymax-=(img.height() - ys - dsmax)*dy;
      }

      // Also, test if a key has been pressed.
      // (moving in the fractal set can be done, using keyboard).
      switch (disp.key()) {

        // Show/hide help.
      case cimg::keyH: show_help = !show_help; break;

        // Switch between Julia/Mandelbrot sets.
      case cimg::keyENTER: fractal_type = !fractal_type; stopflag = true; break;

        // Enable/disable smoothed colors.
      case cimg::keySPACE: smooth = !smooth; break;

        // Change fractal set parameters.
      case cimg::keyARROWLEFT: julia_r-=fractal_type?0.001f:0.05f; break;
      case cimg::keyARROWRIGHT: julia_r+=fractal_type?0.001f:0.05f; break;
      case cimg::keyARROWUP: julia_i+=fractal_type?0.001f:0.05f; break;
      case cimg::keyARROWDOWN: julia_i-=fractal_type?0.001f:0.05f; break;

        // Add/remove iterations.
      case cimg::keyPAGEDOWN: maxiter-=32; break;
      case cimg::keyPAGEUP: maxiter+=16; break;

        // Move left, right, up and down in the fractal set.
      case cimg::keyPAD4: { const double delta = (xmax - xmin)/10; xmin-=delta; xmax-=delta; } break;
      case cimg::keyPAD6: { const double delta = (xmax - xmin)/10; xmin+=delta; xmax+=delta; } break;
      case cimg::keyPAD8: { const double delta = (ymax - ymin)/10; ymin-=delta; ymax-=delta; } break;
      case cimg::keyPAD2: { const double delta = (ymax - ymin)/10; ymin+=delta; ymax+=delta; } break;

        // Allow to zoom in/out in the fractal set.
      case cimg::keyPADADD: {
        const double xc = 0.5*(xmin + xmax), yc = 0.5*(ymin + ymax), dx = (xmax - xmin)*0.85/2, dy = (ymax - ymin)*0.85/2;
        xmin = xc - dx; ymin = yc - dy; xmax = xc + dx; ymax = yc + dy;
      } break;
      case cimg::keyPADSUB:
        const double xc = 0.5*(xmin + xmax), yc = 0.5*(ymin + ymax), dx = (xmax - xmin)*1.15/2, dy = (ymax - ymin)*1.15/2;
        xmin = xc - dx; ymin = yc - dy; xmax = xc + dx; ymax = yc + dy;
        break;
      }

      // Do a simple test to check if more/less iterations are necessary for the next step.
      const float value = img.get_norm().get_histogram(256,0,255)(0)*3;
      if (value>img.size()/6.0f) maxiter+=16;
      if (maxiter>1024) maxiter = 1024;
      if (value<img.size()/10.0f) maxiter-=4;
      if (maxiter<32) maxiter = 32;

      // Check if the user want to quit the explorer.
      if (disp.is_closed() || disp.is_keyQ() || disp.is_keyESC()) stopflag = endflag = true;
    }
  }
  return 0;
}

Last edited by John VV; 02-26-2010 at 04:05 PM.
 
1 members found this post helpful.
  


Reply

Tags
image, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
High Resolution Timers sachin_damle Linux - Kernel 1 07-28-2008 09:29 AM
High Resolution Timers sachin_damle Linux - General 1 07-23-2008 03:15 PM
High resolution clock addy86 Programming 4 09-03-2005 11:26 PM
Can't get high resolution in Slackware Algernon Linux - Newbie 7 08-06-2004 02:24 PM
Drawing Mandelbrot in Redhat hamster Programming 0 06-04-2003 06:08 PM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration