Tk Source Code

Attachment Details
Login
Overview

Artifact ID: 22676c7d8e9fc5495e4467b18908cea48c5bcc670dc9363f39768c9989334f87
Ticket: 4af5ca1921122de63f37a99beefb2f5dbef72518
Date: 2025-07-25 12:36:55
User: marc_culler
Artifact Attached: 0c8048ff1a83afb373f274c8015abe3e5e9571a40251fd6415425bff2c51af79
Filename:testimage.c
Description:This is the version of testimage.c that I was using in my tests.
Content:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
#include <tcl.h>
#include <tk.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/X.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>

/* Variables globales */
static int width = 800;
static int height = 400;

static inline double get_time_ms(void) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
}

static int TestImageUpdateCmd(ClientData clientData, Tcl_Interp *interp, 
                             int objc, Tcl_Obj *const objv[]) {
    double start, end;
    int iterations = 100;
    int i, x, y;
    double total_time = 0.0;
    unsigned char *data;
    
    if (objc > 2) {
        Tcl_WrongNumArgs(interp, 1, objv, "?iterations?");
        return TCL_ERROR;
    }
    
    if (Tcl_GetIntFromObj(interp, objv[1], &iterations) != TCL_OK) {
        return TCL_ERROR;
    }

    Tk_Window tkwin = Tk_MainWindow(interp);
    if (tkwin == NULL) {
        Tcl_SetResult(interp, "No main window", TCL_STATIC);
        return TCL_ERROR;
    }

    Tk_Window targetWindow = Tk_NameToWindow(interp, ".f", tkwin);

    if (targetWindow == NULL) {
        Tcl_SetResult(interp, "No '.f' frame", TCL_STATIC);
        return TCL_ERROR;
    }

    Tk_MakeWindowExist(targetWindow);

    Window window    = Tk_WindowId(targetWindow);
    Display *display = Tk_Display(targetWindow);

    if (window == None) {
        Tcl_SetResult(interp, "Window not yet created", TCL_STATIC);
        return TCL_ERROR;
    }

    XGCValues gcValues;
    gcValues.graphics_exposures = False;
    GC gc = Tk_GetGC(targetWindow, GCGraphicsExposures, &gcValues);

    data = (unsigned char *)malloc(width * height * 4);
    if (data == NULL) {
        Tcl_SetResult(interp, "Memory allocation failed", TCL_STATIC);
        return TCL_ERROR;
    }
    
    for (y = 0; y < height; y++) {
        for (x = 0; x < width; x++) {
            int offset = (y * width + x) * 4;
            data[offset+0] = (x & 0xFF);
            data[offset+1] = (y & 0xFF);
            data[offset+2] = ((x + y) & 0xFF);
            data[offset+3] = 255;
        }
    }

    XImage *ximage = XCreateImage(
      display, 
      Tk_Visual(targetWindow),
      Tk_Depth(targetWindow),
      ZPixmap,    
      0,          
      (char*)data,
      width,
      height,
      32,
      width * 4
    );
    

    for (i = 0; i < iterations; i++) {
      int status = 0;
        start = get_time_ms();
	status = XPutImage(display, window, gc, ximage, 0, 0, 0, 0,
			   width, height); 
	end = get_time_ms();
        if (status) {
	  fprintf(stderr, "status: %d\n", status);
	}
        //XFlush(display);
        
        double elapsed = end - start;
        total_time += elapsed;

        if (i < 10 || i % 10 == 0) {
            printf("Iteration %d: %.3f ms\n", i, elapsed);
        }
    }
    
    double avg_time = total_time / iterations;
    printf("\nAverage time: %.3f ms\n", avg_time);
    
    char result[256];
    sprintf(result, "%.3f", avg_time);
    Tcl_SetResult(interp, result, TCL_VOLATILE);
    
    return TCL_OK;
}

#ifdef __cplusplus
extern "C" {
#endif

DLLEXPORT int Imagetest_Init(Tcl_Interp *interp) {
    if (Tcl_InitStubs(interp, "8.6-", 0) == NULL) {
        return TCL_ERROR;
    }
    
    if (Tk_InitStubs(interp, "8.6-", 0) == NULL) {
        return TCL_ERROR;
    }

    Tcl_CreateObjCommand(interp, "test_image_update", TestImageUpdateCmd, NULL, NULL);
    
    if (Tcl_PkgProvide(interp, "imagetest", "1.0") != TCL_OK) {
        return TCL_ERROR;
    }
    
    return TCL_OK;
}

#ifdef __cplusplus
}
#endif