
- Forum
- Programming Talk
- C and C++
- coding in c
coding in c
This is a discussion on coding in c within the C and C++ forums, part of the Programming Talk category; hi i want to know how can we open a image using c lang and also how can we extract ...
-
coding in c
hi i want to know how can we open a image using c lang and also how can we extract the pixel info of that image.I want coding for above one.
-
04-18-2011, 06:28 AM #2Code:
include"file.h" include"stdio.h" main() { FILE *fp; char ch; fp=fopen("c:\tcpp\pic1.jpeg","rb+"); if(fp == NULL) { printf("Error in opening the image"); fclose(fp); exit(0); } printf("Successfully opened the image file"); while((ch = fgetc(fp))!=EOF) { printf("%c",ch); } printf("\nWriting to o/p completed"); } // may work Change the file mode for getting pixel formats... for more info visit Wiki pages for image formats..
Last edited by admin; 04-19-2011 at 07:06 PM.
-
coding in c
hi thanks for u r rep. i had used that but in that code fopen is not accepting to open the image.so will u pls suggest me the other one.once again thanks for u r rep
-
04-20-2011, 07:38 AM #4
Hello Kavitha... fopen() is not working ?? u just pass the address of ur image file in this function!! it works...
and if u want more detail code try directx libs in ur code!!
Reply me as soon as possible! i will help u with it.
-
#include"stdio.h"
#include"file.h"
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("c:/kavi/Heart.jpg","rb+");
if(fp==NULL)
{
printf("error");
fclose(fp);
exit(0);
}
printf("success");
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
printf("writing");
getch();
}
this is the code that i want to execute.but it is getting error at # include "file.h" and also it is not accepting the img path.so what to do now.
-
04-25-2011, 07:36 AM #6
Its very nice of u that u tried it... now it seems to me that ur compiler do not contains file.h header file in the include folder or in lib folder!
try another compilers like Borland C etc... i will help u, dont worry i would write another code for u soon! Thanks for your patience.. if u like coding the graphics version then i would tell u more things.
-
04-25-2011, 08:29 AM #7
you need an incremental statement...each time it checks while((ch = fgetc(fp))!=EOF) it comes out true .!and it goes inside to print out the first pixel .. and repeats!ince images are binary files you have to know how to "interpret" the data in them after reading them. Luckily, for most formats you can find libraries that do it for you like libpng or libjpeg. Do u get wat i am saying?
-
hi i should thank u.u r so sweet every time replying for my ques.thanks for that.ha i did not get u completely for that condition which needs to be repeated.so if u pls tell it in prog then i can understand.u told that some lib may be missed so for that what can i do.i ma using code in borland turbo c and i want to extract pixel by pixel info also.pls rep me as early as possible.its urgent.THANKS once again for u r help.bye tc
-
04-26-2011, 02:41 PM #9
Hello Once again... the code i gave u just open the image file using rb flag!! I really fallen for ur dedication to the topic so i finally decided to give u the advance code for opening jpeg files.. But 1st i want u to read wiki pages for jpeg format!! and the code is in cpp, so do u want it or not! Reply soon!
-
04-26-2011, 03:59 PM #10
/* This is free to use and modify provided my name is included.
Whats my Name... Whats my Name... My Name is Sheila Oops not me
Swapnil Netankar Thats my Name
I hope u must have read all jpeg information
It may not be accurate and many not be suitable for any specific need.
Compiling instructions:
->gcc imgquality.c -o imgquality
Usage:Estimate the quality of the JPEG based on the quantization tables.
->imgquality file.jpg
You may list one or more jpeg files for analysis.
Use wisely this code free not the Programmer
So do not edit this header
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
int Debug=0;
#define Abs(x) ((x) < 0 ? -(x) : (x))
/*
Usage(): Display program usage.
*/
void Usage (char *Name)
{
fprintf(stderr,"Usage: %s file.jpg [file.jpg file.jpg...]\n",Name);
} /* Usage() */
/***************************************************
ReadJpegMarker(): Jpeg is a weird protocol...
I(Swap) was screwed with jpeg i hope u r also going for it! KaBOOM
There are markers in the stream.
Markers (1) begin with 0xff and (2) are not followed
by 0x00 or 0xff.
This function reads the next marker.
***************************************************/
int ReadJpegMarker (FILE *Fin)
{
int B1,B2;
long Pos;
long Len;
Pos = ftell(Fin); Len=0;
ReadAgainB1:
B1 = fgetc(Fin); Len++;
while(!feof(Fin) && (B1 != 0xff))
{
B1 = fgetc(Fin); Len++;
}
if (feof(Fin)) return(0);
ReadAgainB2:
B2 = fgetc(Fin); Len++;
if (B2 == 0xff) goto ReadAgainB2;
if (B2 == 0x00) goto ReadAgainB1;
return(B1*256+B2);
} /* ReadJpegMarker() */
int ProcessJPEG (FILE *Fin)
{
int Header[15];
int i;
int Type;
int Length;
float Diff=0; /* difference between quantization tables */
float QualityAvg[3] = {0,0,0}; /* allow up to 3 quantization tables */
float QualityF; /* quality as a float */
int QualityI; /* quality as an integer */
float Total;
float TotalNum;
int Precision,Index;
/* read the initial header */
for(i=0; i<2; i++)
{
Header[i] = fgetc(Fin);
}
/* Check the header */
/** JPEG header begins with "ffd8" **/
if ((Header[0] != 0xFF) || (Header[1] != 0xD8))
{
printf("ERROR: Not a supported JPEG format\n");
return(1); /* not a JPEG! */
}
/* Now, search for the quantization tables. */
while(!feof(Fin))
{
/* All "Type" markers begin with "FF" and are followed by anything
except 0x00 or 0xFF. (Very weird standard. Fuck u JPEG Ha ha hA LOL) */
Type = ReadJpegMarker(Fin);
if (Type==0)
return(0);
/* If it got here, then it is a quantization table, but validate the
length just to be sure. */
Length = fgetc(Fin) * 256 + fgetc(Fin); /* 2 bytes */
/** The length is always too long by 2 bytes. Weird standard. **/
Length = Length - 2;
if (Length < 0) Length=0;
if (Type != 0xffdb)
{
/* not a quantization table */
for(i=0; i<Length; i++) fgetc(Fin);
continue;
}
if (Length%65 != 0)
{
printf("ERROR: Wrong size for quantization table -- this contains %d bytes (%d bytes short or %d bytes long)\n",Length,65-Length%65,Length%65);
}
/* Process quantization tables */
printf("\nQuantization table\n");
/** precision is specified by the higher four bits and index is
specified by the lower four bits **/
while(Length > 0)
{
Precision = fgetc(Fin);
Length--;
Index = Precision & 0x0f;
Precision = (Precision & 0xf0) / 16;
printf(" Precision=%d; Table index=%d (%s)\n",Precision,Index,Index ? "chrominance":"luminance");
/* Quantization tables have 1 DC value and 63 AC values */
/** Average AC table values to estimate compression level **/
Total=0;
TotalNum=0;
while((Length > 0) && (TotalNum<64))
{
i = fgetc(Fin);
if (TotalNum!=0) Total += i; /* ignore first value */
Length--;
/* Show quantization table */
if (((int)TotalNum%8) == 0) printf(" ");
printf("%4d",i);
if (((int)TotalNum%8) == 7) printf("\n");
TotalNum++;
}
TotalNum--; /* we read 64 bytes, but only care about 63 values this is the punch */
if (Index < 3) /* Only track the first 3 quantization tables for more detail visit wiki pages*/
{
QualityAvg[Index] = 100.0-Total/TotalNum;
printf(" Estimated quality level = %5.2f%%\n",QualityAvg[Index]);
if (QualityAvg[Index] <= 0)
printf(" Quality too low; estimate may be incorrect.\n");
/* copy over the Q tables for initialization (in case Cr==Cb) */
for(i=Index+1; i<3; i++) QualityAvg[i]=QualityAvg[Index];
}
/*****
YO!!! COPY PASTED FROM WIKI... WIKI ROX!
Technically, JPEG uses YCrCb.
R = Y + (R-Y) = Y + Cr
G = Y - 0.51(R-Y) - 0.186(B-Y) = Y - 0.51Cr -0.186Cb
B = Y + (B-Y) = Y + Cb
and in reverse (YCrCb):
Luminance = Y = 0.299R + 0.587G + 0.114B
Cr = R-Y = R - (0.299R + 0.587G + 0.114B)
Cb = B-Y = B - (0.299R + 0.587G + 0.114B)
The quantization tables are based on Y and CrCb and
they mainly differ by a factor of 0.51 (from determining G).
Thus, we compute the difference using 1 - 0.51 = 0.49.
The results will be off by a fraction, but that's noise
considering all of the integer rounding.
YO!!! COPY PASTED FROM WIKI... WIKI ROX!
*****/
if (Index > 0)
{
/* Diff is a really rough estimate for converting YCrCb to RGB */
Diff = Abs(QualityAvg[0]-QualityAvg[1]) * 0.49;
Diff += Abs(QualityAvg[0]-QualityAvg[2]) * 0.49;
/* If you know that Cr==Cb and don't mind a little more error,
then you can take a short-cut and simply say
Diff = Abs(QualityAvg[0]-QualityAvg[1]); */
QualityF = (QualityAvg[0]+QualityAvg[1]+QualityAvg[2])/3.0 + Diff;
QualityI = (QualityF+0.5); /* round quality to int */
printf("Average quality: %5.2f%% (%d%%)\n",QualityF,QualityI);
} /* if all tables loaded */
} /* for each set of 65 bytes */
} /* while read file */
return(0);
} /* ProcessJPEG() */
int main(int argc, char *argv[])
{
int c;
FILE *Fin;
int rc;
/* one thing y i choose this lang its pretty hard to code in this!! Cpp has tons for libs!I m screwed
*/
/* process command lines */
opterr=0;
while ((c=getopt(argc,argv,"")) != -1)
{
switch (c)
{
default:
Usage(argv[0]);
exit(-1);
}
}
/* process each file */
if (argc - optind < 1)
{
Usage(argv[0]);
exit(-1);
}
for( ; optind < argc; optind++)
{
Fin = fopen(argv[optind],"rb");
if (!Fin)
{
fprintf(stderr,"ERROR: failed to open %s\n",argv[optind]);
continue;
}
printf("#File: %s\n",argv[optind]);
rc=ProcessJPEG(Fin);
fclose(Fin);
if (optind+1 < argc) printf("\n");
}
return(0);
} /* main() */
use it ok and tell me have got how to open the jpg file?? this code estimate the quality okay.. its advanced 1, coz the previous 1 i gave u wont worked for u. I recompiled it and YA it works well with Turbo C.
-
Sponsored Ads

Reply With Quote





