Changeset 8982

Show
Ignore:
Timestamp:
07/22/08 19:10:47
Author:
jblum
Message:

some enhancements

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gnuradio/branches/features/experimental-gui/plotter/waterfall_plotter.py

    r8979 r8982  
    2727 
    2828PADDING = 35, 60, 40, 60 #top, right, bottom, left 
     29RGBA_ARRAY = \ 
     30        [numpy.array((0, 2*i, 1 - 2*i, 0), numpy.uint8) for i in range(0, 128)] + \ 
     31        [numpy.array((2*i - 1, 2 - 2*i, 0, 0), numpy.uint8) for i in range(128, 256)] 
    2932 
    3033################################################## 
     
    4144                self._num_frames = 256 
    4245                self._frame_ptr = 0 
    43                 self._waterfall_buffer_bottom = GLuint(0) 
    44                 self._waterfall_buffer_top = GLuint(0) 
     46                self._buffer_init = False 
    4547 
    4648        def _gl_init(self): 
     
    4951                """ 
    5052                self._grid_compiled_list_id = glGenLists(1) 
     53                self._waterfall_buffer_top = GLuint(0) 
     54                self._waterfall_buffer_bottom = GLuint(0) 
     55                glGenBuffers(1, self._waterfall_buffer_top) 
     56                glGenBuffers(1, self._waterfall_buffer_bottom) 
     57                self._buffer_init = True 
    5158 
    5259        def draw(self): 
     
    9198                #draw top portion 
    9299                glPixelZoom(x_zoom, y_zoom) 
    93                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self._waterfall_buffer_top) 
     100                glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, self._waterfall_buffer_top) 
    94101                glDrawPixels(self._fft_size, self._num_frames-self._frame_ptr, GL_RGBA, GL_UNSIGNED_BYTE, None) 
    95102                #draw bottom portion 
    96103                glPixelZoom(x_zoom, -y_zoom) 
    97                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self._waterfall_buffer_bottom) 
     104                glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, self._waterfall_buffer_bottom) 
    98105                glDrawPixels(self._fft_size, self._frame_ptr, GL_RGBA, GL_UNSIGNED_BYTE, None) 
    99                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0) #unbind 
    100  
    101         def _to_rgba(self, sample): 
    102                 """! 
    103                 Convert the sample into rgba color data. 
    104                 @param sample a normalized floating point sample 
    105                 @return a color array rgba with component values between 0 and 1 
    106                 """ 
    107                 if sample < 0.5: 
    108                         colors = (0, 2*sample, 1 - 2*sample, 0) 
    109                 else: 
    110                         colors = (2*sample - 1, 2 - 2*sample, 0, 0) 
    111                 #color data 
    112                 return numpy.array(colors, numpy.float32) 
     106                glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0) #unbind 
    113107 
    114108        def plot_samples(self, samples, min, max): 
     
    123117                self.semaphore.acquire(True) 
    124118                #init or reinit the pixel buffer 
    125                 if len(samples) != self._fft_size: 
    126                         if self._fft_size is not None: 
    127                                 glDeleteBuffers(1, self._waterfall_buffer_top) 
    128                                 glDeleteBuffers(1, self._waterfall_buffer_bottom) 
     119                if self._buffer_init and len(samples) != self._fft_size: 
    129120                        self._fft_size = len(samples) 
    130                         glGenBuffers(1, self._waterfall_buffer_top) 
    131                         glGenBuffers(1, self._waterfall_buffer_bottom) 
    132121                        #initial data 
    133                         data = (chr(0) + chr(0) + chr(0) + chr(0)) * self._fft_size*self._num_frames 
    134                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self._waterfall_buffer_top) 
     122                        data = numpy.zeros(self._fft_size*self._num_frames*4, numpy.uint8).tostring() 
     123                        glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, self._waterfall_buffer_top) 
    135124                        glBufferData( 
    136                                 GL_PIXEL_UNPACK_BUFFER
     125                                GL_PIXEL_UNPACK_BUFFER_ARB
    137126                                len(data), data, 
    138                                 GL_DYNAMIC_DRAW, 
     127                                GL_STATIC_DRAW, 
    139128                        ) 
    140                         glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self._waterfall_buffer_bottom) 
     129                        glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, self._waterfall_buffer_bottom) 
    141130                        glBufferData( 
    142                                 GL_PIXEL_UNPACK_BUFFER
     131                                GL_PIXEL_UNPACK_BUFFER_ARB
    143132                                len(data), data, 
    144                                 GL_DYNAMIC_DRAW, 
     133                                GL_STATIC_DRAW, 
    145134                        ) 
    146135                #normalize the samples to min/max 
    147                 samples = (samples - min)/(max-min) 
    148                 samples = numpy.minimum(samples, 1) 
    149                 samples = numpy.maximum(samples, 0) 
    150                 data = numpy.array(255*numpy.concatenate(map(self._to_rgba, samples)), numpy.uint8).tostring() 
     136                samples = (samples - min)*float(255/(max-min)) 
     137                samples = numpy.minimum(samples, 255) #clip 
     138                samples = numpy.maximum(samples, 0) #clip 
     139                samples = numpy.array(samples, numpy.uint8) 
     140                data = numpy.array([RGBA_ARRAY[sample] for sample in samples]).tostring() 
    151141                #load the color data into the pixel buffers 
    152                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self._waterfall_buffer_top) 
     142                glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, self._waterfall_buffer_top) 
    153143                glBufferSubData( 
    154                         GL_PIXEL_UNPACK_BUFFER
     144                        GL_PIXEL_UNPACK_BUFFER_ARB
    155145                        (self._num_frames-self._frame_ptr-1)*self._fft_size*4, 
    156146                        len(data), data, 
    157147                ) 
    158                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, self._waterfall_buffer_bottom) 
     148                glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, self._waterfall_buffer_bottom) 
    159149                glBufferSubData( 
    160                         GL_PIXEL_UNPACK_BUFFER
     150                        GL_PIXEL_UNPACK_BUFFER_ARB
    161151                        self._frame_ptr*self._fft_size*4, 
    162152                        len(data), data, 
    163153                ) 
    164                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0) #unbind 
     154                glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0) #unbind 
    165155                #increment the pointer 
    166156                self._frame_ptr = (self._frame_ptr + 1)%self._num_frames