PDA

View Full Version : load images



ceca
12-06-2003, 03:34 PM
Hello,
my program was writen in VC++.When I build it I don't have any errors,but I can't execute it.Actually,when I load pictures,I get theirs size in pixels,but I can't see them!!!! I must load lines on picture,but I can't do it.I don't know where is the problem.
Still,when I debug it,I have break point ahead first line and others.
The code is:
int main(int argc,char **argv)
{
GLint windW=DEFAULT_WINDOW_WIDTH, windH=DEFAULT_WINDOW_HEIGHT;
void createMenus(void);

glutInit(&argc, argv);

/* display modes: 24 BIT, double buffer mode */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windW,windH);
glutCreateWindow("Morphing Line Tool");

/* clear the display */
glClear(GL_COLOR_BUFFER_BIT);

/* set default attributes */
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glLineWidth(DEFAULT_LINE_WIDTH);

/* window redisplay and reshape event callbacks */
glutDisplayFunc(display);
glutReshapeFunc(reshape);


/* create pulldown menus */
createMenus();

/* setup mouse handler callbacks */
glutMotionFunc(mouseMotion);//?ovdje debug jvlja greske
glutMouseFunc(mouseButton);

glutIdleFunc(display);

/* turn over control to GLUT */
glutMainLoop();

return 0;
}

void createMenus(void)
{
int morphMenu;

morphMenu = glutCreateMenu(selectOption);
glutAddMenuEntry("Set Background Color Image 1", '1');
glutAddMenuEntry("Set Background Color Image 2", '2');
glutAddMenuEntry("Set P value", 'p');
glutAddMenuEntry("Set A value", 'a');
glutAddMenuEntry("Set B value", 'b');
glutAddMenuEntry("Set Number of Frames to Render", 'r');
glutAddMenuEntry("Start Morph", 'm');


/* main menu */
glutCreateMenu(selectOption);
glutAddMenuEntry("Clear Screen", 'c');
glutAddMenuEntry("Load Line File", 'l');
glutAddMenuEntry("Load .bmp File", 'f');
glutAddMenuEntry("Save Line File", 's');
glutAddSubMenu("Morph ...", morphMenu);
glutAddMenuEntry("Exit", 'e');
/* attach the menus to right mouse button */
glutAttachMenu(GLUT_RIGHT_BUTTON);
}


This is main code,only.
If somebody knows where is the problem,please help me.
thanks

mikael_aronsson
12-08-2003, 03:39 AM
This doesn't help much, the code you posted is OpenGL/glut code only, there is nothing about the code you use to load the images, and it is big chance that the problem is in there.

Mikael

ceca
12-08-2003, 02:38 PM
My program is very big.It is constants by some cpp files.In morph.cpp I have loadImages.It look likes this:
// Comments: Includes the morph function, the end point interpolation
function, input and output of files and parameters, and the line
tools.
//
#include "const.h"
#include "funcs.h"

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vec3.h"
#include "bitmap.h"
#include <GL/gl.h>


// Global Variables
static float A_V = 0.2;
static float B_V = 1.5;
static float P_V = 0.0;

static int BG_1R = 255;
static int BG_1G = 255;
static int BG_1B = 255;

static int BG_2R = 255;
static int BG_2G = 255;
static int BG_2B = 255;

static float STEPS =.1;

static float time = 0.0;
Cpicture bmp1, bmp2, output;
static int imageLoaded = 0;
pointType p[500];
static int totalPoints=0;


// Functions for Line Tool and Morphing

void addPoint(int x, int y) {//ova je referencirano u func.h pod shape.c kojeg nemam!!!!
int r=0,g=0,b=0;
if(x < DEFAULT_WINDOW_WIDTH/2) {

if(totalPoints%2 == 0)
r=1;
else
b=1;

p[totalPoints].x = x;
p[totalPoints].y = y;
p[totalPoints].r = r;
p[totalPoints].g = g;
p[totalPoints].b = b;
p[totalPoints].initialized = 1;
glColor3f(r, g, b);
glVertex2f(x,y);

p[totalPoints+2].x = x + DEFAULT_WINDOW_WIDTH/2;
p[totalPoints+2].y = y;
p[totalPoints+2].r = r;
p[totalPoints+2].g = g;
p[totalPoints+2].b = b;
p[totalPoints+2].initialized=1;
glColor3f(r, g, b);
glVertex2f(x+DEFAULT_WINDOW_WIDTH/2,y);
totalPoints++;

if(totalPoints %2 == 0)
totalPoints += 2;
}

else {

if(totalPoints%2 == 0)
r=1;
else
b=1;

p[totalPoints].x = x-DEFAULT_WINDOW_WIDTH/2;
p[totalPoints].y = y;
p[totalPoints].r = r;
p[totalPoints].g = g;
p[totalPoints].b = b;
p[totalPoints].initialized = 1;
glColor3f(r, g, b);
glVertex2f(x-DEFAULT_WINDOW_WIDTH/2,y);

p[totalPoints+2].x = x;
p[totalPoints+2].y = y;
p[totalPoints+2].r = r;
p[totalPoints+2].g = g;
p[totalPoints+2].b = b;
p[totalPoints+2].initialized=1;
glColor3f(r, g, b);
glVertex2f(x,y);
totalPoints++;

if(totalPoints %2 == 0)
totalPoints += 2;

}

}


void drawPolygonLines (void) {
int i;

for(i=0; i < totalPoints+2; i+=2) {

if(p[i].initialized && p[i+1].initialized) {
glBegin(GL_LINES);
glColor3f(p[i].r, p[i].g, p[i].b);
glVertex2f(p[i].x, p[i].y);
glColor3f(p[i+1].r, p[i+1].g, p[i+1].b);
glVertex2f(p[i+1].x, p[i+1].y);
glEnd();
}
}
}

void loadImages(void) {
char file_name[100];
imageLoaded++;

fprintf(stderr, "Input a file name: ");
scanf("%s", file_name);

if(imageLoaded > 2) {
cout <<"There can only be two images loaded at a time.\n";
cout <<"Would you like to discard current images (y or n)?\n";// ovo se izvrsi
char input = 'j';
cin >> input;
while(input != 'y' && input != 'n') {
cout << "Please enter y or n.\n";
cin >> input;
}
if(input == 'y') {
cout <<"Which image would you like to discard (1,2, or 3=both)?\n";
int input = 0;
cin >> input;
while(input < 1 &#0124; &#0124; input > 4) {
cout << "Please enter 1,2 or 3: ";//izvrsava se
cin >> input;
}
if(input == 2) {
imageLoaded=2;
cout << "Image " << input << " replaced with " << file_name<< endl;
}
else if(input == 1) {
cout << "Image " << input << " replaced with " << file_name<<endl;
bmp1.LoadBMP(file_name);
imageLoaded=3;
}
else {
cout << "Images 1 and 2 discarded." << endl;
cout << file_name << " is new Image 1." << endl;
imageLoaded=1;
}
}
}
if(imageLoaded == 1) {
bmp1.LoadBMP(file_name);

}
else if(imageLoaded == 2) {
bmp2.LoadBMP(file_name);
}
if(imageLoaded > 1) {
if(bmp1.height != bmp2.height &#0124; &#0124; bmp1.width != bmp2.width) {
cout << "Images must be same size.\n";
cout << "The first image had height="<<bmp1.height<<" and width=" << bmp1.width <<".\n";
cout << "The second image had height="<<bmp2.height<<" and width=" << bmp2.width <<".\n";
cout <<"Which image would you like to discard (1,2, or 3=both)?\n";
int input = 0;
cin >> input;
while(input < 1 &#0124; &#0124; input > 4) {
cout << "Please enter 1,2 or 3: ";
cin >> input;
}
if(input == 2 &#0124; &#0124; input == 1) {
imageLoaded=1;
cout << "Image " << input << " discarded." << endl;
if(input == 1)
bmp1 = bmp2;
}
else {
cout << "Images 1 and 2 discarded." << endl;
imageLoaded=0;
}

}
}
}


void drawImage(void) {
if(imageLoaded)
bmp1.Display(0,bmp1.height);

if(imageLoaded > 1)
bmp2.Display(DEFAULT_WINDOW_WIDTH/2,bmp2.height);
}


void selectNearestPoint(int x, int y) {
float distance, dx, dy, closeDist = 1000.0;
int i;
int closestPoint = 0;

for(i=0; i<totalPoints; i++) {
dx = x - p[i].x;
dy = y - p[i].y;
dx *= dx;
dy *= dy;
distance = sqrt(dx + dy); //pythagorean theorem
if(distance < closeDist) {
closeDist = distance;
closestPoint = i;
}
}
p[closestPoint].x = x;
p[closestPoint].y = y;
}

void loadLineFile(void) {
float val;
FILE *ifp;
int i=0;//i=0 bilo
int s=0;
char file_name[100] = "simple";

fprintf(stderr, "Input a file name: ");//izvrsava se
scanf("%s", &file_name);
for(i=0; i<totalPoints; i++)
p[i].initialized = 0;

ifp=fopen(file_name, "r");
i = 0;
while(fscanf(ifp, "%f", &val) == 1) {
p[i].initialized = 1;
switch(s) {
case 0:
p[i].x = val; //adjust to grid
break;
case 1:
p[i].y = val; //flip to normal coords from opengl
break;
case 2:
p[i].r = val;
break;
case 3:
p[i].g = val;
break;
case 4:
p[i].b = val;
break;
}
s++;

if(s==5) {
i++;
s=0;
}
}
fclose(ifp);
totalPoints = i;
cout << "Total Points = " << totalPoints << endl;
}


void saveFile(void) {
char file_name[100];
int i;
FILE *ofp;

fprintf(stderr, "\nInput a file name: ");
scanf("%s", file_name);
ofp = fopen(file_name, "w");

for(i=0; i<totalPoints; i++) {
float r=0,b=0;
if(i%2 == 0) r=1;
else b =1;

fprintf(ofp, "%f %f %f %f %f\n",
p[i].x, p[i].y, //adjust to grid
r, p[i].g,b);
}
fclose(ofp);

}


void clearAllPoints(void) {
int i = 0;
glClear(GL_COLOR_BUFFER_BIT);
for(i=0; i<totalPoints; i++)
p[i].initialized = 0;
totalPoints = 0;
}


void drawAxis(void) {
glBegin(GL_LINES);
glColor3f(0, 1, 0);
glVertex2f(DEFAULT_WINDOW_WIDTH/2, -10000);
glVertex2f(DEFAULT_WINDOW_WIDTH/2, 10000);
glEnd();
}


void initInterp(void) {
for(int i=0; i<totalPoints; i+=4) {
p[i].xo = p[i].x;
p[i].yo = p[i].y;
p[i+1].xo = p[i+1].x;
p[i+1].yo = p[i+1].y;
p[i+2].xo = p[i+2].x;
p[i+2].yo = p[i+2].y;
p[i+3].xo = p[i+3].x;
p[i+3].yo = p[i+3].y;
}

}

void interpEP(void) {
int i;

for(i=0; i<totalPoints; i+=4) {
p[i].x = ((time)*p[i].xo + (1-time)*(p[i+2].xo-DEFAULT_WINDOW_WIDTH/2));
p[i].y = ((time)*p[i].yo + (1-time)*p[i+2].yo);
p[i+1].x = ((time)*p[i+1].xo + (1-time)*(p[i+3].xo-DEFAULT_WINDOW_WIDTH/2));
p[i+1].y = ((time)*p[i+1].yo + (1-time)*p[i+3].yo);

}

}


void morph(void) {

if(time >= 1.0)
exit(0);
else
time = time + STEPS;

interpEP();
cout << "Time = " << time << endl;
int k=0,x,y,x2,y2, w, h;
float u,v,u2, v2;
float weightsum, dist, weight, weightsum2, weight2, dist2;
bool outside=false;

w = bmp1.width;
h = bmp1.height;
output = bmp1;
for(int j=0; j<w; j++) {
for(int i=0; i<h; i++) {

Vec3 DSUM(0,0,0);
Vec3 DSUM2(0,0,0);
weightsum = 0;
weightsum2 = 0;
Vec3 X(j, i, 0);
Vec3 Xp(0,0,0);
Vec3 Xp2(0,0,0);

for(k=0; k<totalPoints; k+=4) {

Vec3 Q(p[k+2].xo-DEFAULT_WINDOW_WIDTH/2, h-p[k+2].yo-1, 0);
Vec3 P(p[k+3].xo-DEFAULT_WINDOW_WIDTH/2, h-p[k+3].yo-1, 0);
Vec3 Qp(p[k].x, h-p[k].y-1, 0);
Vec3 Pp(p[k+1].x, h-p[k+1].y-1, 0);
Vec3 D(0,0,0);

Vec3 D2(0,0,0);
Vec3 Qp2(p[k].x, h-p[k].y-1, 0);
Vec3 Pp2(p[k+1].x, h-p[k+1].y-1, 0);
Vec3 Q2(p[k].xo, h-p[k].yo-1, 0);
Vec3 P2(p[k+1].xo, h-p[k+1].yo-1, 0);


u = dot((X-P), (Q-P))/((Q-P).length()*(Q-P).length());

u2 = dot((X-P2), (Q2-P2))/((Q2-P2).length()*(Q2-P2).length());

v = dot((X-P), (Q-P).perpendicular())/(Q-P).length();

v2 = dot((X-P2), (Q2-P2).perpendicular())/(Q2-P2).length();

Xp = Pp + (Qp - Pp)*u + ((Qp-Pp).perpendicular()*v)/(Qp-Pp).length();

Xp2 = Pp2 + (Qp2- Pp2)*u2 + ((Qp2-Pp2).perpendicular()*v2)/(Qp2-Pp2).length();

D = (Xp - X);
D2 = (Xp2 - X);


if(u >= 1)
dist = (Q-X).length();
else if(u <= 0)
dist = (P-X).length();
else
dist = abs(v);

if(u2 >= 1)
dist2 = (Q2-X).length();
else if(u2 <= 0)
dist2 = (P2-X).length();
else
dist2 = abs(v2);


weight = pow((Q-P).length(), P_V)/(A_V + dist);
weight = pow(weight, B_V);


DSUM += D * weight;
weightsum += weight;

weight2 = pow((Q2-P2).length(), P_V)/(A_V + dist2);
weight2 = pow(weight2, B_V);

DSUM2 += D2 * weight2;
weightsum2 += weight2;

}

Xp = X + DSUM/weightsum;

Xp2 = X + DSUM2/weightsum2;

if(Xp.x() - floor(Xp.x()) >= .5)
x = ceil(Xp.x());
else
x = floor(Xp.x());


// Since new coordinate is a float need to round off
if(Xp.y() - floor(Xp.y()) >=.5)
y = ceil(Xp.y());
else
y = floor(Xp.y());

if(Xp2.x() - floor(Xp2.x()) >= .5)
x2 = ceil(Xp2.x());
else
x2 = floor(Xp2.x());

if(Xp2.y() - floor(Xp2.y()) >=.5)
y2 = ceil(Xp2.y());
else
y2 = floor(Xp2.y());


// Need to check to make sure that mapping of coordinate
// is to a location on the picture.
bool out1 = false;
bool out2 = false;
if(y >= bmp1.height &#0124; &#0124; y < 0) {
//y = i;
out1 = true;
}

if(y2 >=bmp2.height &#0124; &#0124; y2 < 0) {
//y2 = i;
out2=true;
}
if(x >= bmp1.width &#0124; &#0124; x < 0) {
//x = j;
out1 = true;
}
if(x2 >= bmp2.width &#0124; &#0124; x2 < 0) {
//x2 = j;
out2=true;
}


int r,g,b,r1,g1,b1,r2,g2,b2;
r1 = bmp1.picture[(y*w+x)*3+0];
g1 = bmp1.picture[(y*w+x)*3+1];
b1 = bmp1.picture[(y*w+x)*3+2];
r2 = bmp2.picture[(y2*w+x2)*3+0];
g2 = bmp2.picture[(y2*w+x2)*3+1];
b2 = bmp2.picture[(y2*w+x2)*3+2];


// Need to display different colors depending on whether
// one of the generated coordinates is outside the picture
// area, both are, or neither. The substituted color is the
// background color.

if(!out1 && !out2) {
// cross-dissolve
r = (1-time)*r1 + time*r2;
g = (1-time)*g1 + time*g2;
b = (1-time)*b1 + time*b2;
}
else {
if(out1 && !out2) {
r = (1-time)*BG_1R + time*r2;
g = (1-time)*BG_1G + time*g2;
b = (1-time)*BG_1B + time*b2;
}
else if(out2 && !out1) {
r = r1*(1-time) + time*BG_2R;
g = g1*(1-time) + time*BG_2G;
b = b1*(1-time) + time*BG_2B;
}
else {
r=(1-time)*BG_1R + time*BG_2R;
g=(1-time)*BG_1G + time*BG_2G;
b=(1-time)*BG_1B + time*BG_2B;
}
}

output.picture[(i*w+j)*3+2] = b;
output.picture[(i*w+j)*3+1] = g;
output.picture[(i*w+j)*3+0] = r;

}

}

char out_name[15] = "output00.bmp";
char change1 = '0';
char change2='0';
int t;
t = time*100;
change1 += t%10;
t = t/10;
change2 += t;

if(time>.99) {
change1=change2='9';
stopMorph();
}
out_name[7] = change1;
out_name[6] = change2;

output.Write_Bmp(out_name);
output.Display(DEFAULT_WINDOW_WIDTH/2-w/2,DEFAULT_WINDOW_HEIGHT/2+h/2);
cout << out_name << " created." << endl;
if(time>.99)
cout << "Morph is complete." << endl;
}


void setBG1(void) {
char method = 'j';
int in;
do {
cout << "Would you like to set a color manually or auto-detect the best color.\n";
cout << "Enter 'm' for manual, 'a' for auto-detect: ";
cin >> method;
} while(method != 'm' && method != 'a');

if(method == 'a')
autodetect(1);
else {

do {
cout << "Specify a red value from 0 to 255 for image 1.";
cin >> in;
} while(in<0 &#0124; &#0124; in>255);
BG_1R = in;

do {
cout << "Specify a green value from 0 to 255 for image 1.";
cin >> in;
} while(in<0 &#0124; &#0124; in>255);
BG_1G = in;

do {
cout << "Specify a blue value from 0 to 255 for image 1.";
cin >> in;
} while(in<0 &#0124; &#0124; in>255);
BG_1B = in;
}
}

void setBG2(void) {
char method = 'j';

int in;
do {
cout << "Would you like to set a color manually or auto-detect the best color.\n";
cout << "Enter 'm' for manual, 'a' for auto-detect: ";
cin >> method;
} while(method != 'm' && method != 'a');

if(method == 'a')
autodetect(2);
else {
do {
cout << "Specify a red value from 0 to 255 for image 2.";
cin >> in;
} while(in<0 &#0124; &#0124; in>255);
BG_2R = in;

do {
cout << "Specify a green value from 0 to 255 for image 2.";
cin >> in;
} while(in<0 &#0124; &#0124; in>255);
BG_2G = in;

do {
cout << "Specify a blue value from 0 to 255 for image 2.";
cin >> in;
} while(in<0 &#0124; &#0124; in>255);
BG_2B = in;
}
}

void setPV(void) {
float input;
do {
cout << "Specify a P Value from 0.0 to 1.0: " << endl;
cin >> input;
} while(input < 0 &#0124; &#0124; input > 1);
P_V = input;
}

void setAV(void) {
float input;
do {
cout << "Specify a A Value from 0.0 to 1.0: " << endl;
cin >> input;
} while(input < 0 &#0124; &#0124; input > 1);
A_V = input;
}

void setBV(void) {
float input;
do {
cout << "Specify a B Value from 0.5 to 2.0: " << endl;
cin >> input;
} while(input < 0.5 &#0124; &#0124; input > 2);
B_V = input;
}

void autodetect(int img) {
int R[256], G[256], B[256];
Cpicture pic;
if((img==1 && imageLoaded == 1) &#0124; &#0124; (img==2 && imageLoaded == 2)) {
if(img==1) pic = bmp1;
else pic = bmp2;
int w = pic.width;
int h = pic.height;

for(int i=0; i<256; i++) {
R[i] = 0;
G[i] = 0;
B[i] = 0;
}

for(i=0; i<h; i++) {
for(int j=0; j<w; j++) {
R[pic.picture[((i*w+j)*3+0)]]++;
G[pic.picture[((i*w+j)*3+1)]]++;
B[pic.picture[((i*w+j)*3+2)]]++;
}
}

int rc=0, br=0, gc=0, bg=0, bc=0, bb=0;

for(i=0; i<256;i++) {
if(R[i] > rc) {
rc = R[i];
br = i;
}
if(G[i] > gc) {
gc = G[i];
bg = i;
}
if(B[i] > bc) {
bc = B[i] > bc;
bb = i;
}
}

cout << "The program has automatically detected the RGB color.\n";
cout << "(R = " << br << ", G = " << bg << ", B = " << bb << ")";
cout << " for image " << img <<"." << endl;;
if(img==1) {
BG_1R = br;
BG_1G = bg;
BG_1B = bb;
}
else {
BG_2R = br;
BG_2G = bg;
BG_2B = bb;
}

}
else {
cout << "Image must be loaded first."<<endl;
}
}

void setSTEPS(void) {
int input;
do {
cout << "How many frames do you want to render from 2 to 100." << endl;
cin >> input;
} while(input < 2 &#0124; &#0124; input > 100);
STEPS = (1.0/(float)input);
}


Originally posted by mikael_aronsson:
This doesn't help much, the code you posted is OpenGL/glut code only, there is nothing about the code you use to load the images, and it is big chance that the problem is in there.

Mikael

soconne
12-08-2003, 10:04 PM
Why don't you just post your code for loading the image only. Then it would be easier to find the problem.

ceca
12-09-2003, 02:52 PM
int Cpicture::LoadBMP(const char *file) {

BMP1 BMPFILEHEADER;

BMP2 BMPINFOHEADER;

FILE *fp;

long index;

int x,type; // type: 0=OS/2, 1=Windows

if((fp=fopen(file,"rb"))==NULL) {

return 1;// Error opening file

}

fread(&BMPFILEHEADER,14/*sizeof(BMPFILEHEADER)*/,1,fp); // <------note 1

fread(&BMPINFOHEADER.biSize,sizeof(BMPINFOHEADER.biSize), 1,fp);

if(BMPINFOHEADER.biSize==40) { // Windows

fread(&BMPINFOHEADER.biWidth,36,1,fp); // 36 = 40 - 4

width=BMPINFOHEADER.biWidth;

height=BMPINFOHEADER.biHeight;

type=1;

}

if((BMPINFOHEADER.biSize!=40)&&(BMPINFOHEADER.biSize!=12))

return 5; // Unknown BMP type or corrupted header...



//Only Handles 24 Bit bitmaps

if (BMPINFOHEADER.biBitCount == 24){

if(Create_Picture(width*height*3)){

fclose(fp);

return 4; // Not enough memory for palette

}

for(index=0;index<height;index++)

for(x=0;x<width;x++) {

picture[(index*width+x)*3+2]=fgetc(fp);

picture[(index*width+x)*3+1]=fgetc(fp);

picture[(index*width+x)*3+0]=fgetc(fp);

}



}



file_header = BMPFILEHEADER;

bitmap_header = BMPINFOHEADER;

cout << "Width = " << width << ", height = " << height << endl;

return 0;



}


Originally posted by oconnellseanm:
Why don't you just post your code for loading the image only. Then it would be easier to find the problem.