+//#501 string() readpicture (DP_CSQC_READWRITEPICTURE)
+extern cvar_t cl_readpicture_force;
+static void VM_CL_ReadPicture (void)
+{
+ const char *name;
+ unsigned char *data;
+ unsigned char *buf;
+ int size;
+ int i;
+ cachepic_t *pic;
+
+ VM_SAFEPARMCOUNT(0, VM_CL_ReadPicture);
+
+ name = MSG_ReadString();
+ size = MSG_ReadShort();
+
+ // check if a texture of that name exists
+ // if yes, it is used and the data is discarded
+ // if not, the (low quality) data is used to build a new texture, whose name will get returned
+
+ pic = Draw_CachePic_Flags (name, CACHEPICFLAG_NOTPERSISTENT);
+
+ if(size)
+ {
+ if(pic->tex == r_texture_notexture)
+ pic->tex = NULL; // don't overwrite the notexture by Draw_NewPic
+ if(pic->tex && !cl_readpicture_force.integer)
+ {
+ // texture found and loaded
+ // skip over the jpeg as we don't need it
+ for(i = 0; i < size; ++i)
+ MSG_ReadByte();
+ }
+ else
+ {
+ // texture not found
+ // use the attached jpeg as texture
+ buf = Mem_Alloc(tempmempool, size);
+ MSG_ReadBytes(size, buf);
+ data = JPEG_LoadImage_BGRA(buf, size);
+ Mem_Free(buf);
+ Draw_NewPic(name, image_width, image_height, false, data);
+ Mem_Free(data);
+ }
+ }
+
+ PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(name);
+}
+