imagemap.cpp revision ab026a45b1869d884ee3f0af690c3879b76425e8
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include <stdlib.h>
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include "imagemap.h"
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include "io/sys.h"
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc/*#########################################################################
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc### G R A Y M A P
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#########################################################################*/
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcstatic void gSetPixel(GrayMap *me, int x, int y, unsigned long val)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc{
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (val>765)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc val = 765;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc unsigned long *pix = me->rows[y] + x;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc *pix = val;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc}
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcstatic unsigned long gGetPixel(GrayMap *me, int x, int y)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long *pix = me->rows[y] + x;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return *pix;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc}
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcstatic int gWritePPM(GrayMap *me, char *fileName)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc{
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (!fileName)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return FALSE;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc FILE *f = fopen(fileName, "wb");
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!f)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return FALSE;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh fprintf(f, "P6 %d %d 255\n", me->width, me->height);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh for (int y=0 ; y<me->height; y++)
f595a68a3b8953a12aa778c2abd7642df8da8c3ayz {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc for (int x=0 ; x<me->width ; x++)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz {
f595a68a3b8953a12aa778c2abd7642df8da8c3ayz unsigned long pix = me->getPixel(me, x, y) / 3;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned char pixb = (unsigned char) (pix & 0xff);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh fputc(pixb, f);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh fputc(pixb, f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz fputc(pixb, f);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini fclose(f);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return TRUE;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystemsstatic void gDestroy(GrayMap *me)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems{
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (me->pixels)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(me->pixels);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (me->rows)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(me->rows);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(me);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzGrayMap *GrayMapCreate(int width, int height)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini GrayMap *me = (GrayMap *)malloc(sizeof(GrayMap));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!me)
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini return NULL;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh /** methods **/
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->setPixel = gSetPixel;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->getPixel = gGetPixel;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->writePPM = gWritePPM;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->destroy = gDestroy;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /** fields **/
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->width = width;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->height = height;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->pixels = (unsigned long *)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini malloc(sizeof(unsigned long) * width * height);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->rows = (unsigned long **)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini malloc(sizeof(unsigned long *) * height);
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if (!me->pixels || !me->rows)
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh free(me);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long *row = me->pixels;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (int i=0 ; i<height ; i++)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->rows[i] = row;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini row += width;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return me;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini/*#########################################################################
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini### P A C K E D P I X E L M A P
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini#########################################################################*/
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
3fd94f8c011031b38162a1db3b554de4371c167fam
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic void ppSetPixel(PackedPixelMap *me, int x, int y, int r, int g, int b)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan{
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini unsigned long *pix = me->rows[y] + x;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan *pix = (((unsigned long)r)<<16 & 0xff0000L) |
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan (((unsigned long)g)<< 8 & 0x00ff00L) |
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan (((unsigned long)b) & 0x0000ffL);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystemsstatic void ppSetPixelLong(PackedPixelMap *me, int x, int y, unsigned long rgb)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini{
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems unsigned long *pix = me->rows[y] + x;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini *pix = rgb;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystemsstatic unsigned long ppGetPixel(PackedPixelMap *me, int x, int y)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long *pix = me->rows[y] + x;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return *pix;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystemsstatic int ppWritePPM(PackedPixelMap *me, char *fileName)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini{
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!fileName)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return FALSE;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh FILE *f = fopen(fileName, "wb");
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!f)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return FALSE;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan fprintf(f, "P6 %d %d 255\n", me->width, me->height);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan for (int y=0 ; y<me->height; y++)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan {
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini for (int x=0 ; x<me->width ; x++)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long rgb = me->getPixel(me, x, y);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned char r = (unsigned char) ((rgb>>16) & 0xff);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned char g = (unsigned char) ((rgb>> 8) & 0xff);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned char b = (unsigned char) ((rgb ) & 0xff);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz fputc(r, f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz fputc(g, f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz fputc(b, f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz fclose(f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return TRUE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void ppDestroy(PackedPixelMap *me)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (me->pixels)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(me->pixels);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (me->rows)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(me->rows);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(me);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzPackedPixelMap *PackedPixelMapCreate(int width, int height)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz PackedPixelMap *me = (PackedPixelMap *)malloc(sizeof(PackedPixelMap));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!me)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /** methods **/
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->setPixel = ppSetPixel;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->setPixelLong = ppSetPixelLong;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->getPixel = ppGetPixel;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->writePPM = ppWritePPM;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->destroy = ppDestroy;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /** fields **/
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->width = width;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->height = height;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->pixels = (unsigned long *)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz malloc(sizeof(unsigned long) * width * height);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->rows = (unsigned long **)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz malloc(sizeof(unsigned long *) * height);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!me->pixels)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(me);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long *row = me->pixels;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (int i=0 ; i<height ; i++)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz me->rows[i] = row;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz row += width;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return me;
3fd94f8c011031b38162a1db3b554de4371c167fam}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan/*#########################################################################
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan### R G B M A P
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan#########################################################################*/
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhanstatic void rSetPixel(RgbMap *me, int x, int y, int r, int g, int b)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan{
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan RGB *pix = me->rows[y] + x;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan pix->r = r;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan pix->g = g;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan pix->b = b;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhanstatic void rSetPixelRGB(RgbMap *me, int x, int y, RGB rgb)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan{
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan RGB *pix = me->rows[y] + x;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan *pix = rgb;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhanstatic RGB rGetPixel(RgbMap *me, int x, int y)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan{
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan RGB *pix = me->rows[y] + x;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return *pix;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhanstatic int rWritePPM(RgbMap *me, char *fileName)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan{
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!fileName)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return FALSE;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan FILE *f = fopen(fileName, "wb");
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!f)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return FALSE;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan fprintf(f, "P6 %d %d 255\n", me->width, me->height);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan for (int y=0 ; y<me->height; y++)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan for (int x=0 ; x<me->width ; x++)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan RGB rgb = me->getPixel(me, x, y);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan fputc(rgb.r, f);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan fputc(rgb.g, f);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini fputc(rgb.b, f);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan fclose(f);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return TRUE;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic void rDestroy(RgbMap *me)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan{
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (me->pixels)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(me->pixels);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (me->rows)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan free(me->rows);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(me);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini VaradhanRgbMap *RgbMapCreate(int width, int height)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini{
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini RgbMap *me = (RgbMap *)malloc(sizeof(RgbMap));
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!me)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return NULL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /** methods **/
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan me->setPixel = rSetPixel;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan me->setPixelRGB = rSetPixelRGB;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->getPixel = rGetPixel;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan me->writePPM = rWritePPM;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->destroy = rDestroy;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /** fields **/
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->width = width;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan me->height = height;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->pixels = (RGB *)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan malloc(sizeof(RGB) * width * height);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->rows = (RGB **)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan malloc(sizeof(RGB *) * height);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!me->pixels)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan free(me);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return NULL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan RGB *row = me->pixels;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan for (int i=0 ; i<height ; i++)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->rows[i] = row;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini row += width;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return me;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini/*#########################################################################
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini### I N D E X E D M A P
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini#########################################################################*/
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic void iSetPixel(IndexedMap *me, int x, int y, unsigned int index)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini{
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini unsigned int *pix = me->rows[y] + x;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *pix = index;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic unsigned int iGetPixel(IndexedMap *me, int x, int y)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned int *pix = me->rows[y] + x;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return *pix;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic RGB iGetPixelValue(IndexedMap *me, int x, int y)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned int *pix = me->rows[y] + x;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz RGB rgb = me->clut[((*pix)&0xff)];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return rgb;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int iWritePPM(IndexedMap *me, char *fileName)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!fileName)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return FALSE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz FILE *f = fopen(fileName, "wb");
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!f)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return FALSE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz fprintf(f, "P6 %d %d 255\n", me->width, me->height);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (int y=0 ; y<me->height; y++)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini for (int x=0 ; x<me->width ; x++)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz RGB rgb = me->getPixelValue(me, x, y);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz fputc(rgb.r, f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz fputc(rgb.g, f);
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini fputc(rgb.b, f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems fclose(f);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return TRUE;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dhstatic void iDestroy(IndexedMap *me)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems{
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (me->pixels)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(me->pixels);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (me->rows)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(me->rows);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems free(me);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun MicrosystemsIndexedMap *IndexedMapCreate(int width, int height)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini{
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini IndexedMap *me = (IndexedMap *)malloc(sizeof(IndexedMap));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!me)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return NULL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /** methods **/
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini me->setPixel = iSetPixel;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems me->getPixel = iGetPixel;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems me->getPixelValue = iGetPixelValue;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems me->writePPM = iWritePPM;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->destroy = iDestroy;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems /** fields **/
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->width = width;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->height = height;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini me->pixels = (unsigned int *)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini malloc(sizeof(unsigned int) * width * height);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems me->rows = (unsigned int **)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini malloc(sizeof(unsigned int *) * height);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!me->pixels)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(me);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return NULL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini unsigned int *row = me->pixels;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini for (int i=0 ; i<height ; i++)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->rows[i] = row;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini row += width;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems me->nrColors = 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini RGB rgb;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini rgb.r = rgb.g = rgb.b = 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini for (int i=0; i<256 ; i++)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini me->clut[i] = rgb;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return me;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini/*#########################################################################
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini### E N D O F F I L E
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini#########################################################################*/
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems