+// Save floating point buffer to tga. Must be in range (0-1)
+// Offset and stride are in bytes
+void csr_rt_save_tga( csr_target *rt, const char *path, u32 offset, u32 nc )
+{
+ u8 *image = (u8 *)csr_malloc( rt->x*rt->y * 4 );
+
+ float contrib = 255.f/(float)rt->num_samples;
+
+ for( int y = 0; y < rt->y; y ++ )
+ {
+ for( int x = 0; x < rt->x; x ++ )
+ {
+ u32 pixel_index = (y*rt->x + x);
+
+ void *src = rt->colour + offset + pixel_index * rt->num_samples * rt->shader->stride;
+ u8 *dst = image + pixel_index*4;
+
+ v4f accum = { 0.f, 0.f, 0.f, 0.f };
+
+ for( int k = 0; k < rt->num_samples; k ++ )
+ {
+ float *src_sample = (float *)(src + k*rt->shader->stride);
+
+ for( int j = 0; j < nc; j ++ )
+ {
+ accum[ j ] += src_sample[ j ] * contrib;
+ }
+ }
+
+ // TODO: Clamp this value
+ dst[0] = accum[0];
+ dst[1] = accum[1];
+ dst[2] = accum[2];
+ dst[3] = accum[3];
+ }
+ }
+
+ csr_tga_write( path, rt->x, rt->y, nc, image );
+ free( image );
+}
+