home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <graphics.h>
- #include <io.h>
- #include <dos.h>
- #include <malloc.h>
- #include <string.h>
- #include <math.h>
-
-
- #define BUFFERSIZE 128*128
-
-
- void main(int argc, char **argv)
- {
- int i,j,k,l;
- FILE *in;
- unsigned char buffer[BUFFERSIZE];
- int filesize;
- int bpf; /* buffers per file */
- unsigned int histogram[256];
- int grdriver, grmode, grerror;
- float avg, temp;
- short disp[512];
- char ticks[40];
- int tw;
- int mousepresent, mousex, mousey;
- int oldmousex, oldmousey;
- union REGS regs;
- unsigned char save[18];
- int first = 1;
-
-
-
- /* Ensure proper syntax */
-
- if(argc != 2)
- {
- printf("Usage:: hist8 filename.\n\n");
- exit(1);
- }
-
-
- /* open input file */
-
- in = fopen(argv[1], "rb");
- if(in == NULL)
- {
- printf("Can not open file: %s.\n\n", argv[1]);
- exit(2);
- }
-
-
- /* Clear the histogram */
-
- for(i=0; i<256; i++)
- histogram[i] = 0;
-
-
- /* Determine the number of disk reads needed */
-
- filesize = filelength( fileno(in) );
- bpf = filesize / sizeof(buffer);
-
-
- /* Calculate the histogram */
-
- for(i=0; i<bpf; i++)
- {
- fread(buffer, sizeof(buffer), 1, in);
-
- for(j=0; j<BUFFERSIZE; j++)
- histogram[ buffer[j]]++;
- }
-
- fclose(in);
-
-
- /* check graphics hardware */
-
- grdriver = DETECTX;
- grmode = 0;
- detectgraph(&grdriver, &grmode);
- switch(grdriver)
- {
- case VESA256:
- case ATI256:
- case COMPAQ:
- case TSENG3256:
- case TSENG4256:
- case GENOA5:
- case GENOA6:
- case OAK:
- case PARADIS256:
- case TECMAR:
- case TRIDENT256:
- case VIDEO7:
- case VIDEO7II:
- break;
-
- default:
- printf("Unable to detect 256 color graphics adapter.\n");
- exit(5);
- break;
- }
-
-
- grmode = 1;
- initgraph(&grdriver, &grmode, "");
- grerror = graphresult();
- if(grerror)
- {
- closegraph();
- printf("Error %d initializing graphics mode.\n", grerror);
- exit(6);
- }
-
-
- /* Initialize mouse */
-
- regs.w.ax = 0;
- int386(0x33, ®s, ®s);
-
- if((short)regs.w.ax != -1)
- mousepresent = 0;
- else mousepresent = 1;
-
-
- /* Setup mouse range and turn on cursor */
-
- if(mousepresent)
- {
- regs.w.ax = 0x0007; /* horizontal range */
- regs.w.cx = 64;
- regs.w.dx = 578; /* 64 -> 578 */
- int386(0x33, ®s, ®s);
-
- regs.w.ax = 0x0008; /* vertical range */
- regs.w.cx = 40;
- regs.w.dx = 440; /* 40 -> 440 */
- int386(0x33, ®s, ®s);
-
- regs.w.ax = 0x0001; /* display cursor */
- int386(0x33, ®s, ®s);
-
- oldmousex = 0;
- oldmousey = 0;
- }
-
-
- /* get the average histogram value */
-
- avg = 0.0;
- j = 0;
-
- for(i=0; i<256; i++)
- {
- avg += (float)histogram[i];
- if(histogram[i] != 0)
- j++;
- }
-
- avg = avg/(float)j;
-
-
- /* setup display array */
-
- for(i=0; i<256; i++)
- {
- k = (int)floor( (double)(histogram[i] / avg * 200.0));
- if(k > 400)
- k = 400;
- disp[i*2] = k;
- disp[i*2 + 1] = k;
- }
-
-
- /* display background */
-
- setbkcolor(31); /* white */
- cleardevice();
-
- setcolor(0);
- rectangle(63,39, 640-64,480-39);
-
-
- /* display histogram */
-
- setcolor(1); /* blue */
- for(i=0; i<512; i++)
- line(64+i, 440, 64+i, 440 - disp[i]);
-
-
- /* display tick marks and title */
-
- setcolor(0);
- for(i=0; i<=8; i++)
- {
- sprintf(ticks, "%d", i*32);
- tw = textwidth(ticks);
- outtextxy(64*(i+1)-tw/2+1, 450, ticks);
- line(64*(i+1), 441, 64*(i+1), 444);
- }
-
- tw = textwidth(argv[1]);
- outtextxy(320-tw/2+1, 20, argv[1]);
- setfillstyle(SOLID_FILL, 31);
-
-
- /* wait for keystrike then exit */
-
- while(!kbhit())
- if(mousepresent)
- {
- regs.w.ax = 0x0003;
- int386(0x33, ®s, ®s);
- mousex = (int)regs.w.cx;
- mousey = (int)regs.w.dx;
- if((mousex != oldmousex) || (mousey != oldmousey))
- {
- if(!first)
- {
- for(i=-4; i<=4; i++)
- putpixel(oldmousex+i, oldmousey, save[i+4]);
- for(i=-4; i<=4; i++)
- putpixel(oldmousex, oldmousey+i, save[i+13]);
- }
- else first = 0;
-
- for(i=-4; i<=4; i++)
- save[i+4] = getpixel(mousex+i, mousey);
- for(i=-4; i<=4; i++)
- save[i+13] = getpixel(mousex, mousey+i);
- setcolor(0);
- line(mousex-4, mousey, mousex+4, mousey);
- line(mousex, mousey-4, mousex, mousey+4);
- oldmousex = mousex;
- oldmousey = mousey;
-
- bar(260,465, 450,475);
- sprintf(ticks, "Histogram value: %3d", (mousex-64)/2);
- outtextxy(260, 465, ticks);
- }
-
- }
-
-
- getch();
-
- closegraph();
- }
-