static void tracing_data_header(void) { char buf[20]; /* just guessing this is someone's birthday.. ;) */ buf[0] = 23; buf[1] = 8; buf[2] = 68; memcpy(buf + 3, "tracing", 7); write_or_die(buf, 10); write_or_die(VERSION, strlen(VERSION) + 1); /* save endian */ if (bigendian()) buf[0] = 1; else buf[0] = 0; write_or_die(buf, 1); /* save size of long */ buf[0] = sizeof(long); write_or_die(buf, 1); /* save page_size */ page_size = sysconf(_SC_PAGESIZE); write_or_die(&page_size, 4); }
/* unfortunately, you can not stat debugfs or proc files for size */ static void record_file(const char *file, size_t hdr_sz) { unsigned long long size = 0; char buf[BUFSIZ], *sizep; off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR); int r, fd; fd = open(file, O_RDONLY); if (fd < 0) die("Can't read '%s'", file); /* put in zeros for file size, then fill true size later */ if (hdr_sz) write_or_die(&size, hdr_sz); do { r = read(fd, buf, BUFSIZ); if (r > 0) { size += r; write_or_die(buf, r); } } while (r > 0); close(fd); /* ugh, handle big-endian hdr_size == 4 */ sizep = (char*)&size; if (bigendian()) sizep += sizeof(u64) - hdr_sz; if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) die("writing to %s", output_file); }
bool BmpFileHeader::read_header(FILE* fd) { if (!fread(fd, &magic) || !fread(fd, &fsize) || !fread(fd, &res1) || !fread(fd, &res2) || !fread(fd, &offset)) { return false; } if (bigendian()) swap_endian(); return true; }
bool BmpFileHeader::write_header(FILE* fd) { if (bigendian()) swap_endian(); if (!fwrite(fd, &magic) || !fwrite(fd, &fsize) || !fwrite(fd, &res1) || !fwrite(fd, &res2) || !fwrite(fd, &offset)) { return false; } return true; }
int read_tracing_data(int fd, struct list_head *pattrs) { char buf[BUFSIZ]; struct tracepoint_path *tps = get_tracepoints_path(pattrs); /* * What? No tracepoints? No sense writing anything here, bail out. */ if (tps == NULL) return -1; output_fd = fd; buf[0] = 23; buf[1] = 8; buf[2] = 68; memcpy(buf + 3, "tracing", 7); write_or_die(buf, 10); write_or_die(VERSION, strlen(VERSION) + 1); /* save endian */ if (bigendian()) buf[0] = 1; else buf[0] = 0; write_or_die(buf, 1); /* save size of long */ buf[0] = sizeof(long); write_or_die(buf, 1); /* save page_size */ page_size = sysconf(_SC_PAGESIZE); write_or_die(&page_size, 4); read_header_files(); read_ftrace_files(tps); read_event_files(tps); read_proc_kallsyms(); #ifdef CONFIG_DEBUG_PRINTK read_ftrace_printk(); #else read_ftrace_; #endif return 0; }
bool DibInformationHeader::read_header(FILE* fd) { if (!fread(fd, &size)) return false; if (size == WINDOWS_V3 || size == WINDOWS_V4 || size == WINDOWS_V5) { if (!fread(fd, &width) || !fread(fd, &height) || !fread(fd, &cplanes) || !fread(fd, &bpp) || !fread(fd, &compression) || !fread(fd, &isize) || !fread(fd, &hres) || !fread(fd, &vres) || !fread(fd, &cpalete) || !fread(fd, &important)) { return false; } if (size == WINDOWS_V4 || size == WINDOWS_V5) { if (!fread(fd, &red_mask) || !fread(fd, &blue_mask) || !fread(fd, &green_mask) || !fread(fd, &alpha_mask) || !fread(fd, &cs_type) || !fread(fd, &red_x) || !fread(fd, &red_y) || !fread(fd, &red_z) || !fread(fd, &green_x) || !fread(fd, &green_y) || !fread(fd, &green_z) || !fread(fd, &blue_x) || !fread(fd, &blue_y) || !fread(fd, &blue_z) || !fread(fd, &gamma_x) || !fread(fd, &gamma_y) || !fread(fd, &gamma_z)) { return false; } } if (size == WINDOWS_V5) { if (!fread(fd, &intent) || !fread(fd, &profile_data) || !fread(fd, &profile_size) || !fread(fd, &reserved)) { return false; } } } else if (size == OS2_V1) { // some of theses fields are smaller then in WINDOWS_Vx headers, // so we use hardcoded counts width = 0; height = 0; if (!fread(fd, &width, 2) || !fread(fd, &height, 2) || !fread(fd, &cplanes) || !fread(fd, &bpp)) { return false; } } if (bigendian()) swap_endian(); return true; }
void serial_int64(uint8_t * * const ptr, const int64_t v) { if (bigendian()) { memcpy(*ptr, &v, sizeof(int64_t)); } else { int i; uint8_t rv[sizeof(int64_t)]; uint8_t *pv = (uint8_t *) &v; for (i = 0; i < 8; i++) { rv[i] = pv[7 - i]; } memcpy(*ptr, &rv, sizeof(int64_t)); } *ptr += sizeof(int64_t); }
bool DibInformationHeader::write_header(FILE* fd) { if (bigendian()) swap_endian(); if (!fwrite(fd, &size) || !fwrite(fd, &width) || !fwrite(fd, &height) || !fwrite(fd, &cplanes) || !fwrite(fd, &bpp) || !fwrite(fd, &compression) || !fwrite(fd, &isize) || !fwrite(fd, &hres) || !fwrite(fd, &vres) || !fwrite(fd, &cpalete) || !fwrite(fd, &important)) { return false; } return (true); }
/* unfortunately, you can not stat debugfs or proc files for size */ static int record_file(const char *file, ssize_t hdr_sz) { unsigned long long size = 0; char buf[BUFSIZ], *sizep; off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR); int r, fd; int err = -EIO; fd = open(file, O_RDONLY); if (fd < 0) { pr_debug("Can't read '%s'", file); return -errno; } /* put in zeros for file size, then fill true size later */ if (hdr_sz) { if (write(output_fd, &size, hdr_sz) != hdr_sz) goto out; } do { r = read(fd, buf, BUFSIZ); if (r > 0) { size += r; if (write(output_fd, buf, r) != r) goto out; } } while (r > 0); /* ugh, handle big-endian hdr_size == 4 */ sizep = (char*)&size; if (bigendian()) sizep += sizeof(u64) - hdr_sz; if (hdr_sz && pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0) { pr_debug("writing file size failed\n"); goto out; } err = 0; out: close(fd); return err; }
float64_t unserial_float64(uint8_t * * const ptr) { float64_t v; if (bigendian()) { memcpy(&v, *ptr, sizeof(float64_t)); } else { int i; uint8_t rv[sizeof(float64_t)]; uint8_t *pv = (uint8_t *) &v; memcpy(&v, *ptr, sizeof(float64_t)); for (i = 0; i < 8; i++) { rv[i] = pv[7 - i]; } memcpy(&v, &rv, sizeof(float64_t)); } *ptr += sizeof(float64_t); return v; }
static int tracing_data_header(void) { char buf[20]; ssize_t size; /* just guessing this is someone's birthday.. ;) */ buf[0] = 23; buf[1] = 8; buf[2] = 68; memcpy(buf + 3, "tracing", 7); if (write(output_fd, buf, 10) != 10) return -1; size = strlen(VERSION) + 1; if (write(output_fd, VERSION, size) != size) return -1; /* save endian */ if (bigendian()) buf[0] = 1; else buf[0] = 0; if (write(output_fd, buf, 1) != 1) return -1; /* save size of long */ buf[0] = sizeof(long); if (write(output_fd, buf, 1) != 1) return -1; /* save page_size */ if (write(output_fd, &page_size, 4) != 4) return -1; return 0; }
ssize_t trace_report(int fd, struct trace_event *tevent, bool __repipe) { char buf[BUFSIZ]; char test[] = { 23, 8, 68 }; char *version; int show_version = 0; int show_funcs = 0; int show_printk = 0; ssize_t size = -1; int file_bigendian; int host_bigendian; int file_long_size; int file_page_size; struct tep_handle *pevent = NULL; int err; repipe = __repipe; input_fd = fd; if (do_read(buf, 3) < 0) return -1; if (memcmp(buf, test, 3) != 0) { pr_debug("no trace data in the file"); return -1; } if (do_read(buf, 7) < 0) return -1; if (memcmp(buf, "tracing", 7) != 0) { pr_debug("not a trace file (missing 'tracing' tag)"); return -1; } version = read_string(); if (version == NULL) return -1; if (show_version) printf("version = %s\n", version); if (do_read(buf, 1) < 0) { free(version); return -1; } file_bigendian = buf[0]; host_bigendian = bigendian(); if (trace_event__init(tevent)) { pr_debug("trace_event__init failed"); goto out; } pevent = tevent->pevent; tep_set_flag(pevent, TEP_NSEC_OUTPUT); tep_set_file_bigendian(pevent, file_bigendian); tep_set_host_bigendian(pevent, host_bigendian); if (do_read(buf, 1) < 0) goto out; file_long_size = buf[0]; file_page_size = read4(pevent); if (!file_page_size) goto out; tep_set_long_size(pevent, file_long_size); tep_set_page_size(pevent, file_page_size); err = read_header_files(pevent); if (err) goto out; err = read_ftrace_files(pevent); if (err) goto out; err = read_event_files(pevent); if (err) goto out; err = read_proc_kallsyms(pevent); if (err) goto out; err = read_ftrace_printk(pevent); if (err) goto out; if (atof(version) >= 0.6) { err = read_saved_cmdline(pevent); if (err) goto out; } size = trace_data_size; repipe = false; if (show_funcs) { tep_print_funcs(pevent); } else if (show_printk) { tep_print_printk(pevent); } pevent = NULL; out: if (pevent) trace_event__cleanup(tevent); free(version); return size; }
int main(int argc,char * argv[]) { int b1,b2,b3 = 0; FILE * fi = stdin; if(argc > 1) { fi = fopen(argv[1],"rb"); } if(argc > 2) { type = argv[2]; char * valid[] = {"LE" ,"BE" ,"UCS-2" ,"UCS-2-LE" ,"UCS-2-BE" ,"BIN" ,"UTF-16" ,"UTF-16-LE" ,"UTF-16-BE" ,"ASCII" ,"ISO" ,"UTF-8" ,"UTF-8-BOM" }; int i; for(i = sizeof(valid)/sizeof(valid[0]);--i >= 0;) if(!strcmp(type,valid[i])) break; if(i < 0) { printf("second argument must be one of:\n"); for(i = 0;i < sizeof(valid)/sizeof(valid[0]);++i) printf("%s\n",valid[i]); return 1; } } if(!fi) { printf("Cannot open input %s\n",argv[1]); return 1; } makeDuples(); b1 = fgetc(fi); b2 = fgetc(fi); if(b1 == 0xff && b2 == 0xfe) { if(type[0]) { le = true; if(!strcmp(type,"LE")) result = 1; } else { printf("Byte order: little endian\n"); } littleendian(fi); } else if(b1 == 0xfe && b2 == 0xff) { bigendian(fi,true); } else if(b1 == 0 || b2 == 0) // No BOM, but still looks like UTF-16 { // assume big endian surrogate((b1 << 8) + b2); bigendian(fi,false); } else { FILE * ftempInput = tmpfile(); b3 = fgetc(fi); if(b3 == 0)// No BOM, but still looks like UTF-16 { // assume big endian surrogate((b1 << 8) + b2); surrogate((b3 << 8) + fgetc(fi)); bigendian(fi,false); } else { if(b1 == 0xEF && b2 == 0xBB && b3 == 0xBF) // BOM found, probably UTF8 { ;// remove BOM } else { fputc(b1,ftempInput); fputc(b2,ftempInput); fputc(b3,ftempInput); b1 = b2 = b3 = 0; } int k; bool zeroFound = false; while((k = fgetc(fi)) != EOF) { if(k == 0) zeroFound = true; fputc(k,ftempInput); } rewind(ftempInput); if(zeroFound) { if(b1 && b2 && b3) { surrogate((b1 << 8) + b2); surrogate((b3 << 8) + fgetc(ftempInput)); } bigendian(ftempInput,false); } else { bool bom = false; if(b1 && b2 && b3) { if(type[0]) { bom = true; } else printf("BOM found, but not UTF-16. (UTF-8 file created in Windows?)\n"); } if(UTF8(ftempInput)) { utf8 = true; if(ascii) { if(type[0]) { if( !bom && (!strcmp(type,"ASCII") || !strcmp(type,"ISO")) || !strcmp(type,"UTF-8") || bom && !strcmp(type,"UTF-8-BOM") ) result = 1; } else { printf("encoding: ASCII (subset of UTF-8 and all ISO-8859 encodings)\n"); } } else { if(type[0]) { if(!strcmp(type,"UTF-8") || bom && !strcmp(type,"UTF-8-BOM")) result = 1; } else { printf("encoding: UTF-8\n"); } } } else { int c = 0; while((k = fgetc(ftempInput)) != EOF) getbyte(k); if(type[0]) { if(!bom && (!strcmp(type,"ASCII") || !strcmp(type,"ISO"))) result = 1; } else { printf("encoding: 8-bits\n"); } } } } } if(fi != stdin) fclose(fi); if(!type[0]) fprintf(stderr,"%s",report()); if(!utf8 && ascii) if(!type[0]) printf("File could have been encoded in ASCII!\n"); deleteDuples(); if(type[0]) { printf("[%d]\t%s\n",result,argv[1]); } return 0; }
int main(int argc,char * argv[]) { int b1,b2,b3 = 0; FILE * fi = stdin; FILE * fo = stdout; if(argc > 1) { fi = fopen(argv[1],"rb"); if(argc > 2) fo = fopen(argv[2],"wb"); } if(!fi || !fo) return 1; makeDuples(); b1 = fgetc(fi); b2 = fgetc(fi); if(b1 == 0xff && b2 == 0xfe) littleendian(fi,fo); else if(b1 == 0xfe && b2 == 0xff) bigendian(fi,fo); else if(b1 == 0 || b2 == 0) // No BOM, but still looks like UTF-16 { // assume big endian surrogate((b1 << 8) + b2,fo); bigendian(fi,fo); } else { FILE * ftempInput = tmpfile(); b3 = fgetc(fi); if(b3 == 0)// No BOM, but still looks like UTF-16 { // assume big endian surrogate((b1 << 8) + b2,fo); surrogate((b3 << 8) + fgetc(fi),fo); bigendian(fi,fo); } else { if(b1 == 0xEF && b2 == 0xBB && b3 == 0xBF) // BOM found, probably UTF8 ; // remove BOM else { fputc(b1,ftempInput); fputc(b2,ftempInput); fputc(b3,ftempInput); b1 = b2 = b3 = 0; } int k; bool zeroFound = false; while((k = fgetc(fi)) != EOF) { if(k == 0) zeroFound = true; fputc(k,ftempInput); } rewind(ftempInput); if(zeroFound) { if(b1 && b2 && b3) { surrogate((b1 << 8) + b2,fo); surrogate((b3 << 8) + fgetc(ftempInput),fo); } bigendian(ftempInput,fo); } else if(!UTF8(ftempInput,fo)) { rewind(ftempInput); if(b1 && b2 && b3) // "BOM" found, but not in UTF8 file! { // write "BOM" fputc(b1,fo); fputc(b2,fo); fputc(b3,fo); } copy(ftempInput,fo); } } } if(fi != stdin) fclose(fi); if(fo != stdout) fclose(fo); fprintf(stderr,"%s",report()); deleteDuples(); return 0; }
/* Returns an XImage structure containing the string rendered in the font. This XImage will be 32 bits per pixel, 8 each per R, G, and B, with the extra byte set to 0xFF. Foreground and background are GL-style color specifiers: 4 floats from 0.0-1.0. */ XImage * text_to_ximage (Screen *screen, Visual *visual, const char *font, const char *text_lines, GLfloat *texture_fg, GLfloat *texture_bg) { Display *dpy = DisplayOfScreen (screen); int width, height; XFontStruct *f; Pixmap bitmap; f = XLoadQueryFont(dpy, font); if (!f) { f = XLoadQueryFont(dpy, "fixed"); if (f) fprintf (stderr, "%s: unable to load font \"%s\"; using \"fixed\".\n", progname, font); else { fprintf (stderr, "%s: unable to load fonts \"%s\" or \"fixed\"!\n", progname, font); exit (1); } } /* Parse the text, and render it to `bitmap' */ { char *text, *text2, *line, *token; int lines; XCharStruct overall; XGCValues gcv; GC gc; int margin = 2; int fg = 1; int bg = 0; int xoff, yoff; text = strdup (text_lines); while (*text && (text[strlen(text)-1] == '\r' || text[strlen(text)-1] == '\n')) text[strlen(text)-1] = 0; text2 = strdup (text); memset(&overall, 0, sizeof(overall)); token = text; lines = 0; while ((line = strtok (token, "\r\n"))) { XCharStruct o2; int ascent, descent, direction; token = 0; XTextExtents (f, line, strlen(line), &direction, &ascent, &descent, &o2); overall.lbearing = MAX(overall.lbearing, o2.lbearing); overall.rbearing = MAX(overall.rbearing, o2.rbearing); lines++; } free (text); text = 0; width = overall.lbearing + overall.rbearing + margin + margin + 1; height = ((f->ascent + f->descent) * lines) + margin + margin; /* GL texture sizes must be powers of two. */ { int w2 = to_pow2(width); int h2 = to_pow2(height); xoff = (w2 - width) / 2; yoff = (h2 - height) / 2; width = w2; height = h2; } bitmap = XCreatePixmap(dpy, RootWindowOfScreen (screen), width, height, 1); gcv.font = f->fid; gcv.foreground = bg; gc = XCreateGC (dpy, bitmap, (GCFont | GCForeground), &gcv); XFillRectangle(dpy, bitmap, gc, 0, 0, width, height); XSetForeground(dpy, gc, fg); token = text2; lines = 0; while ((line = strtok(token, "\r\n"))) { XCharStruct o2; int ascent, descent, direction; token = 0; XTextExtents(f, line, strlen(line), &direction, &ascent, &descent, &o2); XDrawString(dpy, bitmap, gc, overall.lbearing + margin + xoff, ((f->ascent * (lines + 1)) + (f->descent * lines) + margin + yoff), line, strlen(line)); lines++; } free(text2); XUnloadFont(dpy, f->fid); XFree((XPointer) f); XFreeGC(dpy, gc); } /* Convert the server-side Pixmap to a client-side GL-ordered XImage. */ { XImage *ximage1, *ximage2; unsigned long fg, bg; int x, y; ximage1 = XGetImage (dpy, bitmap, 0, 0, width, height, ~0L, ZPixmap); XFreePixmap(dpy, bitmap); ximage2 = XCreateImage (dpy, visual, 32, ZPixmap, 0, 0, width, height, 32, 0); ximage2->data = (char *) malloc (height * ximage2->bytes_per_line); /* Translate the 1-bit image to a deep image: first figure out what the colors are. */ { int rpos, gpos, bpos, apos; /* bitfield positions */ /* Note that unlike X, which is endianness-agnostic (since any XImage can have its own specific bit ordering, with the server reversing things as necessary) OpenGL pretends everything is client-side, so we need to pack things in the right order for the client machine. */ #if 0 /* #### Cherub says that the little-endian case must be taken on MacOSX, or else the colors/alpha are the wrong way around. How can that be the case? */ if (bigendian()) rpos = 24, gpos = 16, bpos = 8, apos = 0; else #endif rpos = 0, gpos = 8, bpos = 16, apos = 24; fg = (((unsigned long) (texture_fg[0] * 255.0) << rpos) | ((unsigned long) (texture_fg[1] * 255.0) << gpos) | ((unsigned long) (texture_fg[2] * 255.0) << bpos) | ((unsigned long) (texture_fg[3] * 255.0) << apos)); bg = (((unsigned long) (texture_bg[0] * 255.0) << rpos) | ((unsigned long) (texture_bg[1] * 255.0) << gpos) | ((unsigned long) (texture_bg[2] * 255.0) << bpos) | ((unsigned long) (texture_bg[3] * 255.0) << apos)); } for (y = 0; y < height; y++) { int y2 = (height-1-y); /* Texture maps are upside down. */ for (x = 0; x < width; x++) XPutPixel (ximage2, x, y, XGetPixel (ximage1, x, y2) ? fg : bg); } XDestroyImage (ximage1); #if 0 for (y = 0; y < height; y++) { int y2 = (height-1-y); /* Texture maps are upside down. */ for (x = 0; x < width; x++) fputc ((XGetPixel (ximage2, x, y2) == fg ? '#' : ' '), stdout); fputc ('\n', stdout); } fputc ('\n', stdout); #endif /* 0 */ return ximage2; } }
ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe) { char buf[BUFSIZ]; char test[] = { 23, 8, 68 }; char *version; int show_version = 0; int show_funcs = 0; int show_printk = 0; ssize_t size = -1; int file_bigendian; int host_bigendian; int file_long_size; int file_page_size; struct pevent *pevent; int err; *ppevent = NULL; repipe = __repipe; input_fd = fd; if (do_read(buf, 3) < 0) return -1; if (memcmp(buf, test, 3) != 0) { pr_debug("no trace data in the file"); return -1; } if (do_read(buf, 7) < 0) return -1; if (memcmp(buf, "tracing", 7) != 0) { pr_debug("not a trace file (missing 'tracing' tag)"); return -1; } version = read_string(); if (version == NULL) return -1; if (show_version) printf("version = %s\n", version); free(version); if (do_read(buf, 1) < 0) return -1; file_bigendian = buf[0]; host_bigendian = bigendian(); pevent = read_trace_init(file_bigendian, host_bigendian); if (pevent == NULL) { pr_debug("read_trace_init failed"); goto out; } if (do_read(buf, 1) < 0) goto out; file_long_size = buf[0]; file_page_size = read4(pevent); if (!file_page_size) goto out; pevent_set_long_size(pevent, file_long_size); pevent_set_page_size(pevent, file_page_size); err = read_header_files(pevent); if (err) goto out; err = read_ftrace_files(pevent); if (err) goto out; err = read_event_files(pevent); if (err) goto out; err = read_proc_kallsyms(pevent); if (err) goto out; err = read_ftrace_printk(pevent); if (err) goto out; size = trace_data_size; repipe = false; if (show_funcs) { pevent_print_funcs(pevent); } else if (show_printk) { pevent_print_printk(pevent); } *ppevent = pevent; pevent = NULL; out: if (pevent) pevent_free(pevent); return size; }
static void load_textures (ModeInfo *mi, Bool flip_p) { matrix_configuration *mp = &mps[MI_SCREEN(mi)]; XImage *xi; int x, y; int cw, ch; int orig_w, orig_h; /* The Matrix XPM is 512x598 -- but GL texture sizes must be powers of 2. So we waste some padding rows to round up. */ xi = xpm_to_ximage (matrix3_xpm); orig_w = xi->width; orig_h = xi->height; mp->real_char_rows = CHAR_ROWS; spank_image (mp, xi); if (xi->height != 512 && xi->height != 1024) { xi->height = (xi->height < 512 ? 512 : 1024); xi->data = realloc (xi->data, xi->height * xi->bytes_per_line); if (!xi->data) { fprintf(stderr, "%s: out of memory\n", progname); exit(1); } } if (xi->width != 512) abort(); if (xi->height != 512 && xi->height != 1024) abort(); /* char size in pixels */ cw = orig_w / CHAR_COLS; ch = orig_h / CHAR_ROWS; /* char size in ratio of final (padded) texture size */ mp->tex_char_width = (GLfloat) cw / xi->width; mp->tex_char_height = (GLfloat) ch / xi->height; /* Flip each character's bits horizontally -- we could also just do this by reversing the texture coordinates on the quads, but on some systems that slows things down a lot. */ if (flip_p) { int xx, col; unsigned long buf[100]; for (y = 0; y < xi->height; y++) for (col = 0, xx = 0; col < CHAR_COLS; col++, xx += cw) { for (x = 0; x < cw; x++) buf[x] = XGetPixel (xi, xx+x, y); for (x = 0; x < cw; x++) XPutPixel (xi, xx+x, y, buf[cw-x-1]); } } /* The pixmap is a color image with no transparency. Set the texture's alpha to be the green channel, and set the green channel to be 100%. */ { int rpos, gpos, bpos, apos; /* bitfield positions */ #if 0 /* #### Cherub says that the little-endian case must be taken on MacOSX, or else the colors/alpha are the wrong way around. How can that be the case? */ if (bigendian()) rpos = 24, gpos = 16, bpos = 8, apos = 0; else #endif rpos = 0, gpos = 8, bpos = 16, apos = 24; for (y = 0; y < xi->height; y++) for (x = 0; x < xi->width; x++) { unsigned long p = XGetPixel (xi, x, y); unsigned char r = (p >> rpos) & 0xFF; unsigned char g = (p >> gpos) & 0xFF; unsigned char b = (p >> bpos) & 0xFF; unsigned char a = g; g = 0xFF; p = (r << rpos) | (g << gpos) | (b << bpos) | (a << apos); XPutPixel (xi, x, y, p); } } /* Now load the texture into GL. */ clear_gl_error(); glGenTextures (1, &mp->texture); glPixelStorei (GL_UNPACK_ALIGNMENT, 4); glPixelStorei (GL_UNPACK_ROW_LENGTH, xi->width); glBindTexture (GL_TEXTURE_2D, mp->texture); check_gl_error ("texture init"); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, xi->width, xi->height, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, xi->data); { char buf[255]; sprintf (buf, "creating %dx%d texture:", xi->width, xi->height); check_gl_error (buf); } glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* I'd expect CLAMP to be the thing to do here, but oddly, we get a faint solid green border around the texture if it is *not* REPEAT! */ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glPixelStorei (GL_UNPACK_ALIGNMENT, 1); check_gl_error ("texture param"); XDestroyImage (xi); }
void main(void) { printf("bigendian = %d\n", bigendian()); }
/* Renders a scaled, cropped version of the RGBA XImage onto the window. */ static void draw_image (Display *dpy, Window window, Visual *v, GC gc, int w, int h, int depth, XImage *in) { XImage *out; int x, y, w2, h2, xoff, yoff; double xs, ys, s; unsigned long crpos=0, cgpos=0, cbpos=0, capos=0; /* bitfield positions */ unsigned long srpos=0, sgpos=0, sbpos=0; unsigned long srmsk=0, sgmsk=0, sbmsk=0; unsigned long srsiz=0, sgsiz=0, sbsiz=0; # ifdef HAVE_JWXYZ // BlackPixel has alpha: 0xFF000000. unsigned long black = BlackPixelOfScreen (DefaultScreenOfDisplay (dpy)); #else unsigned long black = 0; # endif xs = in->width / (double) w; ys = in->height / (double) h; s = (xs > ys ? ys : xs); w2 = in->width / s; h2 = in->height / s; xoff = (w - w2) / 2; yoff = (h - h2) / 2; /* Create a new image in the depth and bit-order of the server. */ out = XCreateImage (dpy, v, depth, ZPixmap, 0, 0, w, h, 8, 0); out->bitmap_bit_order = in->bitmap_bit_order; out->byte_order = in->byte_order; out->bitmap_bit_order = BitmapBitOrder (dpy); out->byte_order = ImageByteOrder (dpy); out->data = (char *) malloc (out->height * out->bytes_per_line); if (!out->data) abort(); /* Find the server's color masks. We could cache this and just do it once, but it's a small number of instructions compared to the per-pixel operations happening next. */ srmsk = out->red_mask; sgmsk = out->green_mask; sbmsk = out->blue_mask; if (!(srmsk && sgmsk && sbmsk)) abort(); /* No server color masks? */ decode_mask (srmsk, &srpos, &srsiz); decode_mask (sgmsk, &sgpos, &sgsiz); decode_mask (sbmsk, &sbpos, &sbsiz); /* 'in' is RGBA in client endianness. Convert to what the server wants. */ if (bigendian()) crpos = 24, cgpos = 16, cbpos = 8, capos = 0; else crpos = 0, cgpos = 8, cbpos = 16, capos = 24; /* Iterate the destination rectangle and pull in the corresponding scaled and cropped source pixel, or black. Nearest-neighbor is fine. */ for (y = 0; y < out->height; y++) { int iy = (out->height - y - yoff - 1) * s; for (x = 0; x < out->width; x++) { int ix = (x - xoff) * s; unsigned long p = (ix >= 0 && ix < in->width && iy >= 0 && iy < in->height ? XGetPixel (in, ix, iy) : black); /* unsigned char a = (p >> capos) & 0xFF; */ unsigned char b = (p >> cbpos) & 0xFF; unsigned char g = (p >> cgpos) & 0xFF; unsigned char r = (p >> crpos) & 0xFF; XPutPixel (out, x, y, ((r << srpos) | (g << sgpos) | (b << sbpos) | black)); } } XPutImage (dpy, window, gc, out, 0, 0, 0, 0, out->width, out->height); XDestroyImage (out); }
int StreamBase::get32(void) { return bigendian() ? getBE32() : getLE32(); }
int StreamBase::get16(void) { return bigendian() ? getBE16() : getLE16(); }