static PyObject * chunker_process(Chunker *c) { uint32_t sum, chunk_mask = c->chunk_mask; size_t n = 0, old_last, min_size = c->min_size, window_size = c->window_size; if(c->done) { if(c->bytes_read == c->bytes_yielded) PyErr_SetNone(PyExc_StopIteration); else PyErr_SetString(PyExc_Exception, "chunkifier byte count mismatch"); return NULL; } if(c->remaining <= window_size) { if(!chunker_fill(c)) { return NULL; } } if(c->remaining < window_size) { c->done = 1; if(c->remaining) { c->bytes_yielded += c->remaining; return PyMemoryView_FromMemory(c->data + c->position, c->remaining, PyBUF_READ); } else { if(c->bytes_read == c->bytes_yielded) PyErr_SetNone(PyExc_StopIteration); else PyErr_SetString(PyExc_Exception, "chunkifier byte count mismatch"); return NULL; } } sum = buzhash(c->data + c->position, window_size, c->table); while(c->remaining > c->window_size && ((sum & chunk_mask) || n < min_size)) { sum = buzhash_update(sum, c->data[c->position], c->data[c->position + window_size], window_size, c->table); c->position++; c->remaining--; n++; if(c->remaining <= window_size) { if(!chunker_fill(c)) { return NULL; } } } if(c->remaining <= window_size) { c->position += c->remaining; c->remaining = 0; } old_last = c->last; c->last = c->position; n = c->last - old_last; c->bytes_yielded += n; return PyMemoryView_FromMemory(c->data + old_last, n, PyBUF_READ); }
/* moderngl.core.glprocs(context) */ PyObject * meth_glprocs(PyObject * self, PyObject * context) { if (context->ob_type != Context_class) { // TODO: error return 0; } MGLContext * ctx = SLOT(context, MGLContext, Context_class_mglo); return PyMemoryView_FromMemory((char *)&ctx->gl, sizeof(ctx->gl), PyBUF_WRITE); }
// Here is where the real job is done static PyObject* ZBufferTo3D_recover(PyZBufferTo3D* self, PyObject* args) { Py_buffer img_buffer; int i, pixel, npixels; double z_b; float z_n, z_e; float x, y; int u, v; float * fbuffer; // Read the incomming data as a buffer. It is originally a bgl.Buffer object if (!PyArg_ParseTuple(args, "w*", &img_buffer)) return NULL; // check that there is no division by 0 if (self->width == 0) return NULL; fbuffer = (float *) img_buffer.buf; npixels = img_buffer.len / 4; // rgba (sizeof float = 4B) for (i = 0, pixel = 0; pixel < npixels; pixel++) { z_b = fbuffer[pixel]; if (z_b >= 1.0) { self->points[i] = nanf(""); self->points[i+1] = nanf(""); self->points[i+2] = nanf(""); i += 3; continue; // nothing seen within the far clipping } z_n = 2.0 * z_b - 1.0; z_e = 2.0 * self->near * self->far / (self->far + self->near - z_n * (self->far - self->near)); // The image we receive is stored as a single array of floats. // Pixel 0, 0 in the data is located at the bottom left, according to // the OpenGL conventions. // We need to convert this frame of reference to (u, v), starting at the // top left u = pixel % self->width; v = self->height - (pixel / self->width); // Use the intrinsic matrix of the camera view to get the 3D coordinates // corresponding to each pixel, with respect to the camera x = z_e * (u - self->u_0) / self->alpha_u; y = z_e * (v - self->v_0) / self->alpha_v; // Store the x, y, z coordinates in the buffer self->points[i] = x; self->points[i+1] = y; self->points[i+2] = z_e; i += 3; } // release the Python buffers PyBuffer_Release(&img_buffer); return PyMemoryView_FromMemory((char *)self->points, i*sizeof(float), PyBUF_READ); }
PyObject * PyContentEntry_getBuffer( PyContentEntry * contentEntry, PyObject * args ) { char * const data( const_cast<char *>( contentEntry->ptr->buffer->getBufferStart() ) ); std::size_t const size( contentEntry->ptr->buffer->getBufferSize() ); return PyMemoryView_FromMemory( data, size, PyBUF_READ ); }
PyObject *memory_bios(PyObject *self, PyObject *args) { return PyMemoryView_FromMemory((char*)bios, 0x4000, PyBUF_READ); }
PyObject *memory_io(PyObject *self, PyObject *args) { PyMemoryView_FromMemory((char*)ioMem, 0x400, PyBUF_WRITE); }
PyObject *memory_vram(PyObject *self, PyObject *args) { PyMemoryView_FromMemory((char*)vram, 0x20000, PyBUF_WRITE); }
PyObject *memory_pal(PyObject *self, PyObject *args) { PyMemoryView_FromMemory((char*)paletteRAM, 0x400, PyBUF_WRITE); }
PyObject *memory_wram(PyObject *self, PyObject *args) { return PyMemoryView_FromMemory((char*)workRAM, 0x40000, PyBUF_WRITE); }
PyObject *memory_iram(PyObject *self, PyObject *args) { return PyMemoryView_FromMemory((char*)internalRAM, 0x8000, PyBUF_WRITE); }
PyObject *memory_rom(PyObject *self, PyObject *args) { return PyMemoryView_FromMemory((char*)rom, 0x2000000, PyBUF_WRITE); }
static PyObject * chunker_process(Chunker *c) { uint32_t sum, chunk_mask = c->chunk_mask; size_t n = 0, old_last, min_size = c->min_size, window_size = c->window_size; if(c->done) { if(c->bytes_read == c->bytes_yielded) PyErr_SetNone(PyExc_StopIteration); else PyErr_SetString(PyExc_Exception, "chunkifier byte count mismatch"); return NULL; } while(c->remaining < min_size + window_size + 1 && !c->eof) { /* see assert in Chunker init */ if(!chunker_fill(c)) { return NULL; } } /* here we either are at eof ... */ if(c->eof) { c->done = 1; if(c->remaining) { c->bytes_yielded += c->remaining; return PyMemoryView_FromMemory((char *)(c->data + c->position), c->remaining, PyBUF_READ); } else { if(c->bytes_read == c->bytes_yielded) PyErr_SetNone(PyExc_StopIteration); else PyErr_SetString(PyExc_Exception, "chunkifier byte count mismatch"); return NULL; } } /* ... or we have at least min_size + window_size + 1 bytes remaining. * We do not want to "cut" a chunk smaller than min_size and the hash * window starts at the potential cutting place. */ c->position += min_size; c->remaining -= min_size; n += min_size; sum = buzhash(c->data + c->position, window_size, c->table); while(c->remaining > c->window_size && (sum & chunk_mask)) { sum = buzhash_update(sum, c->data[c->position], c->data[c->position + window_size], window_size, c->table); c->position++; c->remaining--; n++; if(c->remaining <= window_size) { if(!chunker_fill(c)) { return NULL; } } } if(c->remaining <= window_size) { c->position += c->remaining; c->remaining = 0; } old_last = c->last; c->last = c->position; n = c->last - old_last; c->bytes_yielded += n; return PyMemoryView_FromMemory((char *)(c->data + old_last), n, PyBUF_READ); }