📄 mpe_graphics.c
字号:
/*@ MPE_Draw_line - Draws a line on an X11 display Input Parameters:+ handle - MPE graphics handle . x1,y_1 - pixel position of one end of the line to draw. Coordinates are upper-left origin (standard X11). x2,y_2 - pixel position of the other end of the line to draw. Coordinates are upper-left origin (standard X11)- color - Color `index` value. See 'MPE_MakeColorArray'. By default, the colors 'MPE_WHITE', 'MPE_BLACK', 'MPE_RED', 'MPE_YELLOW', 'MPE_GREEN', 'MPE_CYAN', 'MPE_BLUE', 'MPE_MAGENTA', 'MPE_AQUAMARINE', 'MPE_FORESTGREEN', 'MPE_ORANGE', 'MPE_VIOLET', 'MPE_BROWN', 'MPE_PINK', 'MPE_CORAL' and 'MPE_GRAY' are defined..N XGRAPHICS_FORTRAN@*/int MPE_Draw_line( handle, x1, y_1, x2, y_2, color )MPE_XGraph handle;int x1, y_1, x2, y_2;MPE_Color color;{ if (handle->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; }#ifndef MPE_NOMPI if (handle->is_collective) { } else#endif { XBSetPixVal( handle->xwin, handle->xwin->cmapping[color] ); XDrawLine( handle->xwin->disp, handle->xwin->win, handle->xwin->gc.set, x1, y_1, x2, y_2 ); } return MPE_SUCCESS;}/*@ MPE_Fill_rectangle - Draws a filled rectangle on an X11 display Input Parameters:+ handle - MPE graphics handle . x,y - pixel position of the upper left (low coordinate) corner of the rectangle to draw.. w,h - width and height of the rectangle- color - Color `index` value. See 'MPE_MakeColorArray'. By default, the colors 'MPE_WHITE', 'MPE_BLACK', 'MPE_RED', 'MPE_YELLOW', 'MPE_GREEN', 'MPE_CYAN', 'MPE_BLUE', 'MPE_MAGENTA', 'MPE_AQUAMARINE', 'MPE_FORESTGREEN', 'MPE_ORANGE', 'MPE_VIOLET', 'MPE_BROWN', 'MPE_PINK', 'MPE_CORAL' and 'MPE_GRAY' are defined. Notes: This uses the X11 definition of width and height, so you may want to add 1 to both of them..N XGRAPHICS_FORTRAN@*/int MPE_Fill_rectangle( handle, x, y, w, h, color )MPE_XGraph handle;int x, y, w, h;MPE_Color color;{ if (handle->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; }#ifndef MPE_NOMPI if (handle->is_collective) { } else#endif { XBSetPixVal( handle->xwin, handle->xwin->cmapping[color] ); XFillRectangle( handle->xwin->disp, handle->xwin->win, handle->xwin->gc.set, x, y, w, h ); } return MPE_SUCCESS;}/*@ MPE_Update - Updates an X11 display Input Parameter:. handle - MPE graphics handle. Note: Only after an 'MPE_Update' can you count on seeing the results of MPE drawing routines. This is caused by the buffering of graphics requests for improved performance..N XGRAPHICS_FORTRAN@*/int MPE_Update( handle )MPE_XGraph handle;{ if (handle->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; }#ifndef MPE_NOMPI if (handle->is_collective) { } else#endif { XFlush( handle->xwin->disp ); } if (handle->capture_file) {#ifdef HAVE_SYSTEM /* Place into a file */ char cmdbuf[1024]; if ((handle->capture_num % handle->capture_freq) == 0) { /* This will need to be configured for the location of xwd ... */ sprintf( cmdbuf, "%sxwd -display %s -id %ld > %s%.3d.xwd\n", "/usr/local/X11R5/bin/", handle->display_name, (long) handle->xwin->win, handle->capture_file, handle->capture_cnt++ ); system( cmdbuf ); } handle->capture_num++;#else fprintf( stderr, "Could not call system routine for file capture\n" );#endif } return MPE_SUCCESS;}/* MPE_Create_contour - *//* MPE_Draw_contour - *//*@ MPE_Close_graphics - Closes an X11 graphics device Input Parameter:. handle - MPE graphics handle. Return Values:+ MPE_ERR_BAD_ARGS - 'handle' parameter is bad- MPE_SUCCESS - success.N XGRAPHICS_FORTRAN@*/int MPE_Close_graphics( handle )MPE_XGraph *handle;{ if (!handle || !*handle || (*handle)->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; } XBWinDestroy( (*handle)->xwin ); FREE( *handle ); *handle = 0; return MPE_SUCCESS;}/*@ MPE_Make_color_array - Makes an array of color indices Input Parameters:+ handle - MPE graphics handle. nc - Number of colors Output Parameter:- array - Array of color indices Notes: The new colors for a uniform distribution in hue space and replace the existing colors `except` for 'MPE_WHITE' and 'MPE_BLACK'..N XGRAPHICS_FORTRAN@*/int MPE_Make_color_array( handle, ncolors, array )MPE_XGraph handle;int ncolors;MPE_Color array[];{ int i; PixVal white; if (handle->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; } XBUniformHues( handle->xwin, ncolors + 2 ); /* XBUniformHues creates a spectrum like: BLACK red->yellow->blue->purple WHITE We want something like: WHITE BLACK red->yellow->blue->purple So I shifted things around a little. */ /* here's the original code: */ /* for (i=0; i<ncolors; i++) array[i] = handle->xwin->cmapping[i + 2]; */ /* here's something that works: */ white = handle->xwin->cmapping[ncolors+1]; for (i=0; i<ncolors; i++) { array[i] = (MPE_Color)(i+2); handle->xwin->cmapping[ncolors+1-i] = handle->xwin->cmapping[ncolors-i]; } handle->xwin->cmapping[MPE_BLACK] = handle->xwin->cmapping[0]; handle->xwin->cmapping[MPE_WHITE] = white; return MPE_SUCCESS;}/*@ MPE_Num_colors - Gets the number of available colors Input Parameter:. handle - MPE graphics handle Output Parameter:. nc - Number of colors available on the display..N XGRAPHICS_FORTRAN@*/int MPE_Num_colors( handle, nc )MPE_XGraph handle;int *nc;{ if (handle->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; } *nc = handle->xwin->maxcolors; return MPE_SUCCESS;}/*@ MPE_Draw_circle - Draws a circle Input Parameters:+ graph - MPE graphics handle. centerx - horizontal center point of the circle. centery - vertical center point of the circle. radius - radius of the circle- color - color of the circle.N XGRAPHICS_FORTRAN@*/int MPE_Draw_circle( graph, centerx, centery, radius, color )MPE_XGraph graph;int centerx, centery, radius;MPE_Color color;{ int returnVal; if (graph->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; }#if DEBUG fprintf( stdout, "Drawing at (%d,%d) with radius %d and color %d\n", centerx, centery, radius, color ); fflush( stdout );#endif XBSetPixVal( graph->xwin, graph->xwin->cmapping[color] ); XDrawArc( graph->xwin->disp, XBDrawable(graph->xwin), graph->xwin->gc.set, centerx-radius, centery-radius, radius*2, radius*2, 0, 360*64 ); returnVal = 0; return MPE_Xerror( returnVal, "MPE_DrawCircle" );}/*@ MPE_Fill_circle - Fills a circle Input Parameters:+ graph - MPE graphics handle. centerx - horizontal center point of the circle. centery - vertical center point of the circle. radius - radius of the circle- color - color of the circle.N XGRAPHICS_FORTRAN@*/int MPE_Fill_circle( graph, centerx, centery, radius, color )MPE_XGraph graph;int centerx, centery, radius;MPE_Color color;{ int returnVal; if (graph->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; } XBSetPixVal( graph->xwin, graph->xwin->cmapping[color] ); XFillArc( graph->xwin->disp, XBDrawable(graph->xwin), graph->xwin->gc.set, centerx-radius, centery-radius, radius*2, radius*2, 0, 360*64 ); returnVal = 0; return MPE_Xerror( returnVal, "MPE_FillCircle" );}/*@ MPE_Draw_string - Draw a text string Input Parameters:+ graph - MPE graphics handle. x - x-coordinate of the origin of the string. y - y-coordinate of the origin of the string. color - color of the text- string - text string to be drawn.N XGRAPHICS_FORTRAN Additional Notes for Fortran Interface : The trailing blanks in Fortran CHARACTER string argument will be ignored.@*/int MPE_Draw_string( graph, x, y, color, string )MPE_XGraph graph;int x, y;MPE_Color color;char *string;{ int returnVal; if (graph->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; } printf("color = %d, string = %s\n",(int) color, string); XBSetPixVal( graph->xwin, graph->xwin->cmapping[color] ); returnVal = XDrawString( graph->xwin->disp, XBDrawable(graph->xwin), graph->xwin->gc.set, x, y, string, strlen(string) );/* from mail returnVal = XDrawString( graph->xwin->disp, graph->xwin->win, graph->xwin->gc.set, x, y, string, strlen(string) );*/ return MPE_Xerror( returnVal, "MPE_DrawString" );}/*@ MPE_Draw_logic - Sets logical operation for laying down new pixels Input Parameters:+ graph - MPE graphics handle- function - integer specifying one of the following:.n 'MPE_LOGIC_COPY' - no logic, just copy the pixel.n 'MPE_LOGIC_XOR' - xor the new pixel with the existing one and many more... see 'mpe_graphics.h'.N XGRAPHICS_FORTRAN@*/int MPE_Draw_logic( graph, function )MPE_XGraph graph;int function;{ int returnVal; if (graph->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; } XSetFunction( graph->xwin->disp, graph->xwin->gc.set, function ); returnVal = 0; MPE_Xerror( returnVal, "MPE_DrawLogic" ); return returnVal;}/*@ MPE_Line_thickness - Sets thickness of lines Input Parameters:+ graph - MPE graphics handle- thickness - integer specifying how many pixels wide lines should be.N XGRAPHICS_FORTRAN@*/int MPE_Line_thickness( graph, thickness )MPE_XGraph graph;int thickness;{ XGCValues gcChanges; gcChanges.line_width = thickness; XChangeGC( graph->xwin->disp, graph->xwin->gc.set, GCLineWidth, &gcChanges ); return MPE_SUCCESS;}int MPE_Draw_dashes( graph, dashlen )MPE_XGraph graph;int dashlen;{ XGCValues gcChanges; if (dashlen) { gcChanges.line_style = LineDoubleDash; gcChanges.dashes = dashlen; gcChanges.dash_offset = 0; XChangeGC( graph->xwin->disp, graph->xwin->gc.set, GCDashOffset | GCDashList | GCLineStyle, &gcChanges ); } else { gcChanges.line_style = 0; XChangeGC( graph->xwin->disp, graph->xwin->gc.set, GCLineStyle, &gcChanges ); } return 0;}int MPE_Dash_offset( graph, offset )MPE_XGraph graph;int offset;{ XGCValues gcChanges; gcChanges.dash_offset = offset; XChangeGC( graph->xwin->disp, graph->xwin->gc.set, GCDashOffset, &gcChanges ); return 0;}/*@ MPE_Add_RGB_color - Adds a color to the colormap given its RGB values Input Parameters:+ graph - MPE graphics handle- red, green, blue - color levels from 0 to 65535 Output Parameter:. mapping - index of the new color Return Values:+ -1 - maxcolors too large (equal to numcolors). MPE_SUCCESS - successful- mapping - index of the new color Notes: This call adds a color cell to X11''s color table, increments maxcolors (the index), and writes it to the mapping parameter..N XGRAPHICS_FORTRAN@*/int MPE_Add_RGB_color( graph, red, green, blue, mapping )MPE_XGraph graph;int red, green, blue;MPE_Color *mapping;{ XColor colordef; if (graph->Cookie != MPE_G_COOKIE) { fprintf( stderr, "Handle argument is incorrect or corrupted\n" ); return MPE_ERR_BAD_ARGS; } if (graph->xwin->maxcolors == graph->xwin->numcolors) return -1; colordef.red = red; colordef.green = green; colordef.blue = blue; colordef.flags = DoRed | DoGreen | DoBlue; if (!XAllocColor( graph->xwin->disp, graph->xwin->cmap, &colordef )) return -1; graph->xwin->cmapping[graph->xwin->maxcolors] = colordef.pixel; *mapping = (MPE_Color) (graph->xwin->maxcolors++); if (graph->xwin->maxcolors == 256) graph->xwin->maxcolors = 255; return MPE_SUCCESS;}int MPE_Xerror( returnVal, functionName )int returnVal;char *functionName;{ if (returnVal) { switch (returnVal) { case BadAccess: fprintf( stderr, "'BadAccess' error in call to %s\n", functionName ); return returnVal; case BadAlloc: fprintf( stderr, "'BadAlloc' error in call to %s\n", functionName ); return returnVal; case BadColor: fprintf( stderr, "'BadColor' error in call to %s\n", functionName ); return returnVal; case BadDrawable: fprintf( stderr, "'BadDrawable' error in call to %s\n", functionName ); return returnVal; case BadGC: fprintf( stderr, "'BadGC' error in call to %s\n", functionName ); return returnVal; case BadMatch: fprintf( stderr, "'BadMatch' error in call to %s\n", functionName ); return returnVal; case BadValue: fprintf( stderr, "'BadValue' error in call to %s\n", functionName ); return returnVal; default:/* fprintf( stderr, "Unknown error %d in call to %s\n", returnVal, functionName );*/ return returnVal; } } else { return MPE_SUCCESS; }}static xpand_list_Int *Int_CreateList(initialLen)int initialLen;{ xpand_list_Int *tempPtr; if (initialLen < 1) { initialLen = 10; } tempPtr = (xpand_list_Int *) malloc(sizeof(xpand_list_Int)); if (tempPtr) { tempPtr->list = (int *) malloc(sizeof(int) * initialLen); if (!tempPtr->list) { return 0; } tempPtr->nused = 0; tempPtr->size = initialLen; } else { fprintf( stderr, "Could not allocate memory for expanding list\n"); } return tempPtr;}static int Int_AddItem(listPtr, newItem)xpand_list_Int *listPtr;int newItem;{ int *tmp; if (listPtr->nused == listPtr->size) { if (listPtr->size < 1) listPtr->size = 1; listPtr->size *= 2; tmp = (int *) malloc( sizeof(int) * listPtr->size); if (!tmp) { fprintf( stderr, "Ran out of memory packing pixels.\n" ); return 1; } else { memcpy( tmp, listPtr->list, sizeof(int) * listPtr->size / 2 ); free( listPtr->list ); listPtr->list = tmp; } } listPtr->list[(listPtr->nused)++] = newItem; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -