Danh cho Quang cao
VNP Community
Tags
This content has not been tagged yet
> Mọi người coi giúp mình cái code này với., C++  
Reply to this topicStart new topic
tst12346
post Jul 4 2008, 08:40 PM
Post #1


Junior Member
Group Icon

Group: Member
Posts: 1
Cash: 1k
Age: N/A
Joined: 4-July 08
Member No.: 111,392
Yahoo! Status: N/A



Mình đã post về chủ đề này 1 lần rồi nhưng chưa nhận được kết quả như mong muốn. Mình tiếp tục post tiếp một code nữa lên. Các bạn dùng TC dịch rồi giúp mình coi xem tại sao nhé. Cảm ơn nhiều.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <alloc.h>
#include <conio.h>

typedef struct {int rows; int cols; unsigned char* data;} sImage;


long thongtinanh(FILE*, long, int);
void copythongtinanh(FILE* inputFile, FILE* outputFile);
void copybangmau(FILE* inputFile, FILE* outputFile, int nColors);

void main(int argc, char* argv[])
{
FILE *bmpInput, *bmpOutput;
sImage anhgoc;
sImage anhbien;
unsigned int X, Y;
int I, J;
long sumX, sumY;
int nColors, SUM;
unsigned long vectorSize;
unsigned long fileSize;
int GX[3][3];
int GY[3][3];
unsigned char *pChar, someChar;
unsigned int row, col;

someChar = '0'; pChar = &someChar;

/* 3x3 GX Mat na Sobel.*/

GX[0][0] = -1; GX[0][1] = 0; GX[0][2] = 1;
GX[1][0] = -2; GX[1][1] = 0; GX[1][2] = 2;
GX[2][0] = -1; GX[2][1] = 0; GX[2][2] = 1;

/* 3x3 GY Mat na Sobel.*/

GY[0][0] = 1; GY[0][1] = 2; GY[0][2] = 1;
GY[1][0] = 0; GY[1][1] = 0; GY[1][2] = 0;
GY[2][0] = -1; GY[2][1] = -2; GY[2][2] = -1;

if(argc < 2)
{
printf("Su dung : %s bmpInput.bmp\n", argv[0]);
exit(0);
};
printf("Dang doc file %s\n", argv[1]);

/*-------INPUT & OUTPUT FILES-------*/

bmpInput = fopen(argv[1], "rb");
bmpOutput = fopen("xla.bmp", "wb");

/*---SET POINTER TO BEGINNING OF FILE----*/

fseek(bmpInput, 0L, SEEK_END);

/*-------GET INPUT BMP DATA--------*/

fileSize = thongtinanh(bmpInput, 2, 4);
anhgoc.cols = (int)thongtinanh(bmpInput, 18, 4);
anhgoc.rows = (int)thongtinanh(bmpInput, 22, 4);
anhbien.rows = anhgoc.rows;
anhbien.cols = anhgoc.cols;

/*--------PRINT DATA TO SCREEN----------*/

printf("Chieu rong : %d\n", anhgoc.cols);
printf("Chieu cao : %d\n", anhgoc.rows);
printf("Co anh : %lu\n", fileSize);
nColors = (int)thongtinanh(bmpInput, 46, 4);
printf("nColors : %d\n", nColors);

/*------ALLOCATE MEMORY FOR FILES--------*/

vectorSize = fileSize - (14+40+4*nColors);
printf("vectorSize : %lu\n", vectorSize);
anhbien.data =(unsigned char*)farmalloc(vectorSize*sizeof(unsigned char));
if(anhbien.data == NULL)
{
printf("Failed to malloc anhbien.data\n");
exit(0);
}
printf("%lu bytes malloc'ed for anhbien.data\n", vectorSize);

anhgoc.data =(unsigned char*)farmalloc(vectorSize*sizeof(unsigned char));
if(anhgoc.data == NULL)
{
printf("Failed to malloc anhgoc.data\n");
exit(0);
}
printf("%lu bytes malloc'ed for anhgoc.data\n", vectorSize);

/*------COPY HEADER AND COLOR TABLE---------*/

copythongtinanh(bmpInput, bmpOutput);
copybangmau(bmpInput, bmpOutput, nColors);
fseek(bmpInput, (14+40+4*nColors), SEEK_SET);
fseek(bmpOutput, (14+40+4*nColors), SEEK_SET);

/* Doc input.bmp luu thong tin toi anhgoc.data */

for(row=0; row<=anhgoc.rows-1; row++)
{
for(col=0; col<=anhgoc.cols-1; col++)
{
fread(pChar, sizeof(char), 1, bmpInput);
*(anhgoc.data + row*anhgoc.cols + col) = *pChar;
}
}

/*---------------------------------------------------
CAC PHEP TOAN SOBEL
---------------------------------------------------*/
for(Y=0; Y<=(anhgoc.rows-1); Y++)
{
for(X=0; X<=(anhgoc.cols-1); X++)
{
sumX = 0;
sumY = 0;

/* image boundaries */

if(Y==0 || Y==anhgoc.rows-1)
SUM = 0;
else if(X==0 || X==anhgoc.cols-1)
SUM = 0;
/* Convolution starts here */
else
{

/*-------X GRADIENT APPROXIMATION------*/

for(I=-1; I<=1; I++)
{
for(J=-1; J<=1; J++)
{
sumX = sumX + (int)( (*(anhgoc.data + X + I + (Y + J)*anhgoc.cols)) * GX[I+1][J+1]);
}
}

/*-------Y GRADIENT APPROXIMATION-------*/

for(I=-1; I<=1; I++)
{
for(J=-1; J<=1; J++)
{
sumY = sumY + (int)( (*(anhgoc.data + X + I + (Y + J)*anhgoc.cols)) * GY[I+1][J+1]);
}
}

/*---GRADIENT MAGNITUDE APPROXIMATION (Myler p.218)----*/

SUM = abs(sumX) + abs(sumY);
}
if(SUM>255) SUM=255;
if(SUM<0) SUM=0;
*(anhbien.data + X + Y*anhgoc.cols) = 255 - (unsigned char)(SUM);
fwrite((anhbien.data + X + Y*anhgoc.cols),sizeof(char),1,bmpOutput);
}
}

printf("Xet xla.bmp cho ket qua \n");
fclose(bmpInput);
fclose(bmpOutput);
farfree(anhbien.data); /* Ket thuc anhbien.data */
farfree(anhgoc.data); /* Ket thuc anhgoc.data */
getch();
}

/*----------GET IMAGE INFO SUBPROGRAM--------------*/

long thongtinanh(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char *ptrC;
long value = 0L;
unsigned char dummy;
int i;

dummy = '0';
ptrC = &dummy;

fseek(inputFile, offset, SEEK_SET);

for(i=1; i<=numberOfChars; i++)
{
fread(ptrC, sizeof(char), 1, inputFile);
value = (long)(value + (*ptrC)*(pow(256, (i-1)))); /* Tinh gia tri co ban tren cac byte them vao */
}
return(value);

} /* Ket thuc thongtinanh */

/*-------------COPIES HEADER AND INFO HEADER----------------*/

void copythongtinanh(FILE* inputFile, FILE* outputFile)
{
unsigned char *ptrC;
unsigned char dummy;
int i;

dummy = '0';
ptrC = &dummy;

fseek(inputFile, 0L, SEEK_SET);
fseek(outputFile, 0L, SEEK_SET);

for(i=0; i<=50; i++)
{
fread(ptrC, sizeof(char), 1, inputFile);
fwrite(ptrC, sizeof(char), 1, outputFile);
}

}

/*----------------COPIES COLOR TABLE-----------------------------*/

void copybangmau(FILE* inputFile, FILE* outputFile, int nColors)
{
unsigned char *ptrC;
unsigned char dummy;
int i;

dummy = '0';
ptrC = &dummy;

fseek(inputFile, 54L, SEEK_SET);
fseek(outputFile, 54L, SEEK_SET);

for(i=0; i<=(4*nColors); i++)
{
fread(ptrC, sizeof(char), 1, inputFile);
fwrite(ptrC, sizeof(char), 1, outputFile);
}

}
Go to the top of the page
 
+Quote Post
Game Online
Thiên Hà
post Jul 5 2008, 09:18 PM
Post #2


Yahoo! King
Group Icon

Group: Admin
Posts: 1,044
Cash: 95k
Age: 24
Joined: 19-June 06
From: vũ trụ
Member No.: 14,720
Yahoo! Status:



ngày trước mình học c++ không đời nào khai void main mà có đối trong hàm main cả. Thật sự là mình không biết mấy cái đối argc và argv là cái gì nữa. Cái mà bạn không làm được thì có nguyên nhân sau
- không biết thay thế argv thành đường dẫn thực sự (argv ở đây c++ nó không nhận ra được đường dẫn của cái ảnh thì lấy đâu ra mà nó đọc cái tấm ảnh đó lên). Bạn không hiểu argv là cái gì hết mà không chỉnh code. C++ không nhận argv thì argv=null. Ngay cái dòng
bmpInput = fopen(argv[1], "rb"); thì bmpINput =null lấy gì mà thực hiện tiếp. Nếu bạn thay argv thành đường dẫn đúng thì hãy test bằng đoạn code sau
CODE
bmpInput = fopen("c:\\anh.gif", "rb");//nhớ là 2 dấu \\ chứ không phai là 1 dấu \ đâu nhé
if(bmpInput==null) printf("file khong tim ra");
else printf("tim ra");

nếu nó hiện ra cau "tim ra" thì bạn hãy test lại hàm fseek hàm này lấy có rất nhiều đối ở phía cuối mỗi đối là 1 tác dụng khác nhau xem bạn đã làm đúng chưa. Code trên không ra được thì chỉ do dính ở 2 vấn đề mình đề cập. Có code là 1 chuyện xem code người ta chỉnh theo ý của mình được không đó lại là 1 chuyện khác
Go to the top of the page
 
+Quote Post
tst
post Jul 6 2008, 01:13 AM
Post #3


Yahoo! Baby
Group Icon

Group: Member
Posts: 44
Cash: 44k
Age: 21
Joined: 2-December 05
From: Hà Nội
Member No.: 9,861
Yahoo! Status:



cảm ơn bạn TH đã nhiệt tình giúp đỡ. Mình làm được rồi thực ra cái hàm argv và argc là hàm truyền tham số. Nếu chạy luôn trong TC thì ko có ra. Khi dịch chương trình bằng TC mình chỉ lấy link file exe của nó thôi sau đó qua Dos chạy file exe này và thực hiện nhiệm vụ mà code này cung cấp. Code này viết rất hay sử dụng hàm truyền tham số này rất hữu ích và tiện dụng.
Go to the top of the page
 
+Quote Post
BBCode:
HTML:
  Digg this topic · Save to del.icio.us · Slashdot It · Post to Technorati · Post to Furl · Submit to Reddit · Share on Facebook · Fark It · Googlize This Post · Add to ma.gnolia · Tag to Wink · Add to MyWeb · Add to Netscape
Reply to this topicStart new topic
1 User(s) are reading this topic [1 Guests and 0 Anonymous Users]
0 Members:

 


Collapse

> Links to this thread

Page             Date Hits
cach dung ham argc va argv trong C linux - Tm với Google 3rd November 2008 - 12:01 AM   1

This page's keywords
 

RSS Lo-Fi Version Time is now: 22nd November 2008 - 06:21 AM