int palconv(char *palfile) { uint8 palspace[1024], reds[256], greens[256], blues[256]; uint8 *p; FILE *fp; int j, ret; fp = fopen(palfile, "rb"); if (fp == NULL) { printf(" Error opening palette file %s\n", palfile); exit(1); } fread(reds, 1, 256, fp); fread(greens, 1, 256, fp); fread(blues, 1, 256, fp); fclose(fp); p = palspace; for (j = 0; j < 256; j++) { *p++ = reds[j]; *p++ = greens[j]; *p++ = blues[j]; } ret = DFR8setpalette(palspace); if (ret < 0) { printf(" Error: %d, in writing palette %s\n", ret, palfile); exit(1); } return (0); }
main() { uint8 colors[256*3]; int i; /* Initialize the image array */ static uint8 raster_data8[HEIGHT][WIDTH] = { 0, 1, 2, 3, 4, 50, 51, 52, 53, 54, 100, 101, 102, 103, 104, 150, 151, 152, 153, 154, 200, 201, 202, 203, 204, 251, 252, 253, 254, 255 }; static uint8 raster_data24[HEIGHT][WIDTH][PIXEL_DEPTH] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11, 12,13,14, 50,51,52, 53,54,55, 56,57,58, 59,60,61, 62,63,64, 100,101,102, 103,104,105, 106,107,108, 109,110,111, 112,113,114, 150,151,152, 153,154,155, 156,157,158, 159,160,161, 162,163,164, 200,201,202, 203,204,205, 206,207,208, 209,210,211, 212,213,214, 241,242,243, 244,245,246, 247,248,249, 250,251,252, 253,254,255 }; for(i=0; i<256; i++) { colors[i*3+0] = i; colors[i*3+1] = i; colors[i*3+2] = 255-i; } /* remove old HDF file, if it exists */ unlink(FILE_NAME); if(DFR8setpalette(colors)==FAIL) return 1; if(DFR8addimage(FILE_NAME, raster_data8, WIDTH, HEIGHT, 0)==FAIL) return 1; if(DF24addimage(FILE_NAME, raster_data24, WIDTH, HEIGHT)==FAIL) return 1; if(DF24addimage(FILE_NAME, raster_data24, WIDTH, HEIGHT)==FAIL) return 1; printf("%d 8-bit images and %d 24-bit images\n", DFR8nimages(FILE_NAME), DF24nimages(FILE_NAME)); printf("Success!\n"); return 0; }
int setPal(char *pal) { FILE *fp; char reds[HE_COLOR_SZ], greens[HE_COLOR_SZ], blues[HE_COLOR_SZ]; char palette[HE_PALETTE_SZ]; char *p; int i; if ((fp = fopen(pal, "r")) == NULL) { fprintf(stderr, "Error opening palette file: %s.\n", pal); return FAIL; } if (fread(reds, 1, HE_COLOR_SZ, fp) < HE_COLOR_SZ || fread(greens, 1, HE_COLOR_SZ, fp) < HE_COLOR_SZ || fread(blues, 1, HE_COLOR_SZ, fp) < HE_COLOR_SZ) { fprintf(stderr, "Error reading palette file: %s.\n", pal); return FAIL; } /* convert sun palette to hdf palette */ p = palette; for (i = 0; i < HE_COLOR_SZ; i++) { *p++ = reds[i]; *p++ = greens[i]; *p++ = blues[i]; } if (DFR8setpalette((uint8 *) palette) < 0) { fputs("Error setting palette.\n", stderr); return FAIL; } return HE_OK; }
static void SaveToHDF (char *name) { CGrafPtr theCGrafPort; WindowPtr theWindow; PixMapPtr thePixMap; char buffer[80]; GDHandle gd; GDPtr theGDevice; unsigned char *theImage,*srcPtr,*dstPtr,*baseAddr; Rect screen,window; unsigned long size; int rowBytes,row,col,i,j; PaletteHandle thePalette; unsigned char transTable[256],hdfPalette[768]; RGBColor theColor; theWindow = myWindow.theWindow; theCGrafPort = (CGrafPtr) theWindow; gd = (GDHandle) GetGDevice(); theGDevice = *gd; /* check frontwindow */ if (theWindow!=FrontWindow()) { OneButtonBox("window to save must be the front window","OK"); return; } /* check screen depth and mode */ thePixMap = *(theGDevice->gdPMap); rowBytes = thePixMap->rowBytes&0x1FFF; baseAddr = (unsigned char *) thePixMap->baseAddr; if ((thePixMap->pixelType!=0)||(thePixMap->pixelSize!=8)) { OneButtonBox("Sorry, 8 bits/pixel and chunky mode required","OK"); return; } /* compute window contents coordinates */ thePixMap = *(theGDevice->gdPMap); SetRect(&screen,thePixMap->bounds.left,thePixMap->bounds.top,thePixMap->bounds.right,thePixMap->bounds.bottom); thePixMap = *(theCGrafPort->portPixMap); SetRect(&window,-thePixMap->bounds.left,-thePixMap->bounds.top,theCGrafPort->portRect.right-16-thePixMap->bounds.left,theCGrafPort->portRect.bottom-16-thePixMap->bounds.top); SectRect(&window,&screen,&window); /* allocate image buffer */ size = ((unsigned long)(window.right-window.left))*((unsigned long)(window.bottom-window.top)); theImage = (unsigned char *) malloc(size); if (theImage==NULL) { OneButtonBox("Sorry, not enough memory for image buffer","OK"); return; } /* create translation table and palette */ thePalette = GetPalette(theWindow); for (i=0; i<256; i++) { GetEntryColor(thePalette,i,&theColor); hdfPalette[i*3] = (theColor.red)>>8; hdfPalette[i*3+1] = (theColor.green)>>8; hdfPalette[i*3+2] = (theColor.blue)>>8; j = Color2Index(&theColor); transTable[j] = (unsigned char) i; } /* copy image */ dstPtr = theImage; for (row=window.top; row<window.bottom; row++) { srcPtr = baseAddr+row*rowBytes+window.left; for (col=window.left; col<window.right; col++) *(dstPtr++) = transTable[*(srcPtr++)]; } /* set palette for subsequent image */ if (DFR8setpalette(hdfPalette)) OneButtonBox("setpalette failed","OK"); /* write image to disk */ if(DFR8putimage(name,theImage,window.right-window.left,window.bottom-window.top,DFTAG_RLE)) { free(theImage); OneButtonBox("Image not written correctly","OK"); return; } /* free memory */ free(theImage); return; }