Jump to content
  • Advertisement
Sign in to follow this  
neeti

image reading

This topic is 3716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi!! this is the part of a code on which i m working and image is read in .pcx format.if i already define rows and coloums then it reads the image but if i use this command (fscanf(f,"%d %d",&cols,&rows);)then it just stuck up.so plzz help me out wid dis code!! #include <stdio.h> #include <math.h> #include <alloc.h> #include <mem.h> #include <dos.h> #include <conio.h> #include <process.h> #include <time.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include "oct1.h" int SaveAsPCX(FILE *fimg, OctreeType *octree, int resx, int resy, double nrgbr, double nrgbg, double nrgbb, int npal, RGBType *colortable); int npal = 256; void main(int argc, char *argv[]) { double r, g, b; uint rows, cols; char fname[256]; double colormag; time_t tstart, tend; int nrgbr = 63, nrgbg = 63, nrgbb = 63; OctreeType *octree; RGBType color; FILE *f; char title[40]; char description[128]; ulong i; uint j; int n; RGBType palette[256]; ulong image_start; union REGS regs; int resx, resy; int px, py; int ii; int cols2, rows2; int k; int cli; int maxr=0, maxg=0, maxb=0; printf("Image file : "); scanf("%s",fname); if ((f = fopen(fname,"rb")) == NULL) { printf("%s not found.\n",fname); exit(1); } /* ** Read the image file header */ fgets(title,40,f); fgets(description,128,f); fscanf(f,"%d %d",&cols,&rows); fscanf(f,"%lf",&colormag); image_start = ftell(f); cols2 = cols/2; rows2 = rows/2; time(&tstart); /* ** Initialize the color octree */ octree = CreateOctNode(0); /* ** Loop through the image and store each unique color. */ for (i = 0L; i < (ulong)rows*(ulong)cols; i++) { /* ** Show progress... */ if ((i % (ulong)cols) == 0L) printf("%ld\r",i/cols); fscanf(f,"%lf %lf %lf",&r,&g,&b); color.r = (unsigned char)(r * nrgbr); color.g = (unsigned char)(g * nrgbg); color.b = (unsigned char)(b * nrgbb); if (color.r > nrgbr) color.r = nrgbr; if (color.g > nrgbg) color.g = nrgbg; if (color.b > nrgbb) color.b = nrgbb; /* ** Insert this color into the octree */ InsertTree(&octree, &color, 0); /* ** If there are too many colors in the tree as a result of this ** insert, reduce the octree */ while (TotalLeafNodes() > npal) { ReduceTree(); } } /* ** Make a pass through the completed octree to average down the ** rgb components. When done, 'n' contains the actual number of ** colors in the palette table. */ n = 0; MakePaletteTable(octree, palette, &n); /* ** How long did it take? */ time(&tend); printf("Processed %ld pixels per second\ninto %d quantized colors\n", ((long)rows*(long)cols)/(tend-tstart), n); j = 0; while (j != 3) { printf("Output to (1)monitor or (2).PCX file or (3) quit: "); scanf("%s",title); j = atoi(title); if (j == 2) { fseek(f,image_start,0); SaveAsPCX(f, octree, cols, rows, nrgbr, nrgbg, nrgbb, npal, palette); } } }

Share this post


Link to post
Share on other sites
Advertisement
If you must create an image loader your self instead of using an existing library, I'd recommend the true type targa format (.tga).
It's simple (if you ignore compression) and very flexible IMO.
Most applications doesn't save compressed tga's so you don't need to support that, although the RLE compression in tga is fairly simple to implement.

Here's plenty of information about image file formats.

BTW: pcx is an ancient file format.. don't use it IMO.

Share this post


Link to post
Share on other sites
Well, bitmap (BMP) is an easy format to load from, provided that the image is not compressed and so on. There are tons of tutorials about this, and a lot of code to look from. It is a simple file header with some informations, and then a plain sequence of triplets (in BGR format IIRC). Pixels are not layed out from top to bottom: the first pixels in the files belong to the last row, then the last-1, last-2 and so on.

Note however that there are many free libraries to load, save and edit images in different formats.
Just to name a few:
SDLImage toghether with SDL
DevIL
FreeImage
SOIL

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!