+
+void csr_write_filerr( const char *path )
+{
+ log_error( "File write error (No such file or directory): '%s'\n", path );
+}
+
+void csr_write_txt( char const *path, const char *name, csr_target *rt )
+{
+ FILE *write_ptr;
+
+ write_ptr = fopen( path, "w" );
+
+ if( write_ptr )
+ {
+ fprintf( write_ptr, "\"%s\"\n\{\n", name );
+ fprintf( write_ptr, "\t\"material\" \"overviews/%s\"\n", name );
+ fprintf( write_ptr, "\t\"pos_x\" \"%.8f\"\n", rt->bounds[0][0] );
+ fprintf( write_ptr, "\t\"pos_y\" \"%.8f\"\n", rt->bounds[0][1] );
+ fprintf( write_ptr, "\t\"scale\" \"%.8f\"\n", rt->scale / (float)rt->x );
+ fprintf( write_ptr, "}\n" );
+
+ fclose( write_ptr );
+ }
+ else
+ {
+ csr_write_filerr( path );
+ }
+}
+
+// ALWAYS RGB32
+void csr_rt_save_c32f( csr_target *rt, const char *path, u32 offset )
+{
+ float *image = (float *)csr_malloc( rt->x*rt->y*3*sizeof(float) );
+
+ float contrib = 1.f/(float)rt->num_samples;
+
+ for( int i = 0; i < rt->x*rt->y; i ++ )
+ {
+ void *src = rt->colour + offset + i * rt->num_samples * rt->shader->stride;
+ float *dst = image + i*3;
+
+ v3_zero( dst );
+ for( int k = 0; k < rt->num_samples; k ++ )
+ {
+ v3_muladds( dst, (float *)(src + k*rt->shader->stride), contrib, dst );
+ }
+ }
+
+ if( !csr_32f_write( path, rt->x, rt->y, image ) )
+ csr_write_filerr( path );
+
+ free( image );
+}
+
+// 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 i = 0; i < rt->x*rt->y; i ++ )
+ {
+ void *src = rt->colour + offset + i * rt->num_samples * rt->shader->stride;
+ u8 *dst = image + i*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];
+ }
+
+ if( !csr_tga_write( path, rt->x, rt->y, nc, image ) )
+ csr_write_filerr( path );
+
+ free( image );
+}
+
+#endif