struct bitmap_file read_bitmap(char *filename) { FILE *fp = fopen(filename, "r"); if (!fp) { perror(filename); exit(1); } struct bitmap_file image; image.fh = read_file_header(fp); image.ih = read_info_header(fp); image.cont_bytes = image.ih->header_size - sizeof(struct info_header); image.ih_cont = read_info_cont(fp, image, image.cont_bytes); int gap_size = image.fh->offset - sizeof(struct file_header) - sizeof(struct info_header) - image.cont_bytes; if (gap_size) { fseek(fp, gap_size, 1); } image.pixels = read_pixels(fp, image); fclose(fp); return image; }
int main (int argc, char *argv[]) { if(/*argc != 2 &&*/ argc != 5) { fprintf(stderr, "Wrong argument count!\n"); return 1; } long tmp = strtol(argv[1], (char **)NULL, 10); if(tmp < 0 || tmp > 255) { fprintf(stderr, "Incorrect red mask value!\n"); exit(1); } char redmask = (char) tmp; tmp = strtol(argv[2], (char **)NULL, 10); if(tmp < 0 || tmp > 255) { fprintf(stderr, "Incorrect green mask value!\n"); exit(1); } char greenmask = (char) tmp; tmp = strtol(argv[3], (char **)NULL, 10); if(tmp < 0 || tmp > 255) { fprintf(stderr, "Incorrect blue mask value!\n"); exit(1); } char bluemask = (char) tmp; FILE *fp = fopen(argv[4], "r"); if(!fp) { perror(argv[4]); exit(1); } read_headers(fp); struct pixel *original = read_pixels(fp); fclose(fp); char buff[256]; snprintf(buff, 256, "%s%s", argv[2], ".game"); fp = fopen(buff, "w"); if(!fp) { perror(buff); exit(1); } struct pixel *modified = game(original, redmask, greenmask, bluemask); /*write_file(fp, modified);*/ return 0; }
int xyz_read(image_t* image, FILE* input) { uchar* data = NULL; ulong length = 0; int status = XYZ_NO_ERROR; if ((status = read_header(image, input, &length))); else if ((status = decompress(input, &data, &length))); else if ((status = read_pixels(image, data, length))); if (data) free(data); return status; }
/** * Extract a "*.spr" file data into a 'bitmap' structure * @param bmp Pointer to a bitmap structure * @param filedata Pointer to the file data * @return The source pointer incremented */ static char * bitmap_extract (bitmap * bmp, char *filedata) { char *ptr8; Sint32 *ptr32; ptr8 = filedata; /* * read the pixel data (8 bits per pixel) */ /* 32-bit access */ ptr32 = (Sint32 *) (ptr8); /* number of pixels */ bmp->numof_pixels = little_endian_to_int (ptr32++); bmp->img = memory_allocation (bmp->numof_pixels * bytes_per_pixel); if (bmp->img == NULL) { return NULL; } /* 8-bit access */ ptr8 = (char *) ptr32; ptr8 = read_pixels (bmp->numof_pixels, ptr8, bmp->img); /* * read offsets and repeat values */ /* 32-bit access */ ptr32 = (Sint32 *) (ptr8); /* size of the table in bytes */ bmp->nbr_data_comp = little_endian_to_int (ptr32++); bmp->compress = memory_allocation (bmp->nbr_data_comp * 2); if (bmp->compress == NULL) { return NULL; } /* 8-bit access */ ptr8 = (char *) ptr32; ptr8 = read_compress (bmp->nbr_data_comp, ptr8, bmp->compress); return ptr8; }
/*! ** read_ibc this function read a binary file ibc ** then it stocks it to the structure ** @param ppm ** @param filename */ s_ppm *read_ibc(s_ppm *ppm, char *filename) { int fd; char buff[20]; ppm = malloc(sizeof (s_ppm)); if ((fd = open(filename, O_RDONLY, 0666)) == -1) exit(1); read(fd, buff, 4); if (strncmp(buff, "IBCL", 4)) exit(1); read(fd, &(ppm->lambda), sizeof (float)); read(fd, &(ppm->largeur), sizeof (short)); read(fd, &(ppm->hauteur), sizeof (short)); read(fd, &(ppm->learned), sizeof (short)); ppm->size = ppm->largeur * ppm->hauteur; ppm->pixel = malloc(ppm->size * sizeof (s_pixel)); read_pixels(ppm, fd); close(fd); return (ppm); }
char loadmap(char *file) { t_head head; t_bmp bmp; int ret; int fd; t_loop loop; if ((fd = open(file, O_RDONLY)) < 0 || (ret = read(fd, &head, sizeof(t_head))) < 0 || (ret = read(fd, &bmp, sizeof(t_bmp))) < 0 || lseek(fd, head.offset, SEEK_SET) < 0 || read_pixels(&loop, fd, bmp.height, bmp.width) || (loop.win = bunny_start(bmp.width, bmp.height, 0, file)) == NULL) return (1); bunny_set_key_response(my_esc); bunny_set_loop_main_function(main_loop); bunny_loop(loop.win, 24, &loop); bunny_delete_clipable(&loop.pix->clipable); bunny_stop(loop.win); close(fd); return (0); }
static int scan_img(struct gl843_device *dev, struct gl843_image *img, int timeout) { int ret, i; uint8_t *buf; DBG(DBG_info, "scanning %d lines for calibration\n", img->height); if (img->height < 1) { DBG(DBG_error0, "BUG: height = %d. Must be >= 1.\n", img->height); return 0; } CHK_MEM(init_line_buffer(dev, img->stride)); CHK(write_reg(dev, GL843_LINCNT, img->height)); CHK(write_reg(dev, GL843_SCAN, 1)); CHK(write_reg(dev, GL843_MOVE, 255)); CHK(wait_for_pixels(dev)); buf = img->data; for (i = 0; i < img->height; i++) { /* FIXME: Check number of bytes received. */ CHK(read_pixels(dev, buf, img->stride, img->bpp, timeout)); buf += img->stride; } CHK(write_reg(dev, GL843_SCAN, 0)); CHK(write_reg(dev, GL843_CLRLNCNT, 1)); ret = 0; chk_failed: return ret; chk_mem_failed: ret = LIBUSB_ERROR_NO_MEM; goto chk_failed; }
struct mbmp * mbmp_file(const char *file) { FILE *fp; size_t read; BITMAPFILEHEADER h1 = {0}; long filesize; DWORD dw; fp = fopen(file, "r"); if (fp == NULL) { goto err; } read = fread(&h1, sizeof(h1), 1, fp); if (read != 1) { goto err; } #if 0 printf("bfType: 0x%x (\"%c%c\")\n", h1.bfType, h1.bfType&0xff, (h1.bfType>>8)&0xff); printf("bfSize: %d\n", h1.bfSize); printf("bfReserved1: %d\n", h1.bfReserved1); printf("bfReserved2: %d\n", h1.bfReserved2); printf("bfOffBits: %d\n", h1.bfOffBits); #endif if (h1.bfType != BM) { goto err; } fseek(fp, 0, SEEK_END); if (h1.bfSize != ftell(fp)) { goto err; } if (h1.bfReserved1 != 0 || h1.bfReserved1 != 0) { goto err; } fseek(fp, sizeof(h1), SEEK_SET); read = fread(&dw, sizeof(dw), 1, fp); if (read != 1) { goto err; } #if 0 printf("size: %d\n", dw); #endif switch (dw) { case sizeof(BITMAPINFOHEADER): { struct mbmp *p; BITMAPINFOHEADER h2; fseek(fp, sizeof(h1), SEEK_SET); read = fread(&h2, sizeof(h2), 1, fp); if (read != 1) goto err; assert(h2.biSize == sizeof(h2)); assert(h2.biPlanes == 1); assert(h2.biBitCount == 1); assert(h2.biCompression == BI_RGB); assert(h2.biClrUsed == 2); #if 0 printf("biSize: %d\n", h2.biSize); printf("biWidth: %d\n", h2.biWidth); printf("biHeight: %d\n", h2.biHeight); printf("biPlanes: %d\n", h2.biPlanes); printf("biBitCount: %d\n", h2.biBitCount); printf("biCompression: %d\n", h2.biCompression); printf("biSizeImage: %d\n", h2.biSizeImage); printf("biXPelsPerMeter: %d\n", h2.biXPelsPerMeter); printf("biYPelsPerMeter: %d\n", h2.biYPelsPerMeter); printf("biClrUsed: %d\n", h2.biClrUsed); printf("biClrImportant: %d\n", h2.biClrImportant); #endif p = mbmp_new(h2.biWidth, h2.biHeight); assert(p); p->w = h2.biWidth; p->h = h2.biHeight; /* skip palette */ fseek(fp, sizeof(RGBQUAD)*h2.biClrUsed, SEEK_CUR); /* read pixels */ read = read_pixels(p, fp); assert(read); fclose(fp); return p; } break; case sizeof(BITMAPV4HEADER): case sizeof(BITMAPV5HEADER): /* not implemented */ goto err; default: goto err; } /* just close file */ err: fclose(fp); return NULL; }
/** * Convert from sprite filedata to 'image' stucture * Values from filedata are stored in little-endian * - x-coordinate, y-coordinate, width and height of the sprite * - List of the collision coordinates * - list of the collisions areas coordinates and areas sizes (width/height) * - list of the cannons coordinates and shots angles * - list of the pixels of the sprite without the transparent pixels (0 value) * - list of offsets and repeats values (compress table) for displaying sprite * * @param img Pointer to destination 'image' structure * @param file Pointer to source sprite filedata * @return Pointer to the end of source sprite filedata */ static char * image_extract (image * img, const char *file) { Uint32 i; Sint16 *dest; /* pointer to the file (8/16/32-bit access */ char *ptr8; Sint16 *ptr16; Sint32 *ptr32; /* * read coordinates and size of the sprite */ /* 16-bit access */ ptr16 = (Sint16 *) file; /* x and y coordinates of the centre of gravity */ img->x_gc = little_endian_to_short (ptr16++); img->y_gc = little_endian_to_short (ptr16++); /* image width and height */ img->w = little_endian_to_short (ptr16++); img->h = little_endian_to_short (ptr16++); /* * read zones and points of collision */ /* number of points of collision */ img->numof_collisions_points = little_endian_to_short (ptr16++); dest = &img->collisions_points[0][0]; for (i = 0; i < MAX_OF_COLLISION_POINTS * 2; i++) { *(dest++) = little_endian_to_short (ptr16++); } /* number of zones of collision */ img->numof_collisions_zones = little_endian_to_short (ptr16++); dest = &img->collisions_coords[0][0]; for (i = 0; i < MAX_OF_COLLISION_ZONES * 2; i++) { *(dest++) = little_endian_to_short (ptr16++); } dest = &img->collisions_sizes[0][0]; for (i = 0; i < MAX_OF_COLLISION_ZONES * 2; i++) { *(dest++) = little_endian_to_short (ptr16++); } /* * read origins of shots (location of the cannons) and angle shots */ /* number of cannons */ img->numof_cannons = little_endian_to_short (ptr16++); dest = &img->cannons_coords[0][0]; for (i = 0; i < MAX_OF_CANNONS * 2; i++) { *(dest++) = little_endian_to_short (ptr16++); } for (i = 0; i < MAX_OF_CANNONS; i++) { img->cannons_angles[i] = little_endian_to_short (ptr16++); } /* * read the pixel data (8 bits per pixel) */ /* 32-bit access */ ptr32 = (Sint32 *) (ptr16); /* number of pixels */ img->numof_pixels = little_endian_to_int (ptr32++); img->img = memory_allocation (img->numof_pixels * bytes_per_pixel); if (img->img == NULL) { return NULL; } /* 8-bit access */ ptr8 = (char *) ptr32; ptr8 = read_pixels (img->numof_pixels, ptr8, img->img); /* * read offsets and repeat values */ /* 32-bit access */ ptr32 = (Sint32 *) (ptr8); /* size of the table in bytes */ img->nbr_data_comp = little_endian_to_int (ptr32++); img->compress = memory_allocation (img->nbr_data_comp * 2); if (img->compress == NULL) { return NULL; } /* 8-bit access */ ptr8 = (char *) ptr32; ptr8 = read_compress (img->nbr_data_comp, ptr8, img->compress); return ptr8; }